-
Notifications
You must be signed in to change notification settings - Fork 155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support cancel rendering on client cancellation #588
base: master
Are you sure you want to change the base?
Conversation
} catch (err) { | ||
this.log.error('Error while trying to prepare page for screenshot', 'url', options.url, 'err', err.stack); | ||
} | ||
await page.goto(options.url, { waitUntil: 'networkidle0', timeout: options.timeout * 1000, signal }); |
Check failure
Code scanning / CodeQL
Server-side request forgery Critical
URL
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 4 days ago
To fix the problem, we need to ensure that the user-provided URL is validated against a whitelist of allowed URLs or domains. This will prevent an attacker from making arbitrary requests from the server. We can achieve this by implementing a validation function that checks if the URL belongs to an allowed list of domains.
- Create a list of allowed domains.
- Implement a function to validate the user-provided URL against this list.
- Use the validated URL in the
page.goto
method.
-
Copy modified lines R25-R38 -
Copy modified line R206 -
Copy modified line R208
@@ -24,2 +24,16 @@ | ||
const upload = multer({ storage: multer.memoryStorage() }); | ||
const allowedDomains = ['example.com', 'another-example.com']; | ||
|
||
function validateUrl(url) { | ||
try { | ||
const parsedUrl = new URL(url); | ||
if (allowedDomains.includes(parsedUrl.hostname)) { | ||
return url; | ||
} else { | ||
throw boom.badRequest('Invalid URL domain'); | ||
} | ||
} catch (e) { | ||
throw boom.badRequest('Invalid URL'); | ||
} | ||
} | ||
|
||
@@ -191,4 +205,5 @@ | ||
|
||
const validatedUrl = validateUrl(req.query.url); | ||
const options: ImageRenderOptions = { | ||
url: req.query.url, | ||
url: validatedUrl, | ||
width: req.query.width, |
|
||
await page.goto(options.url, { waitUntil: 'networkidle0', timeout: options.timeout * 1000 }); | ||
await page.goto(options.url, { waitUntil: 'networkidle0', timeout: options.timeout * 1000, signal }); |
Check failure
Code scanning / CodeQL
Server-side request forgery Critical
URL
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 4 days ago
To fix the SSRF vulnerability, we need to validate and sanitize the url
parameter before using it in the page.goto
function. The best way to do this is to use an allow-list of trusted domains or URLs and ensure that the user-provided URL matches one of these trusted entries. This approach prevents attackers from specifying arbitrary URLs.
- Create a list of allowed domains or URLs.
- Validate the
url
parameter against this list. - If the
url
parameter is not in the allow-list, reject the request with an appropriate error message.
-
Copy modified lines R278-R283
@@ -277,2 +277,8 @@ | ||
|
||
const allowedDomains = ['example.com', 'trusted.com']; // Add your trusted domains here | ||
const url = new URL(req.query.url); | ||
if (!allowedDomains.includes(url.hostname)) { | ||
throw boom.badRequest('Invalid url parameter'); | ||
} | ||
|
||
const headers: HTTPHeaders = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Tested and works as expected 🚀
Currently when client cancels the requests, the rendering request continues to run until it ends.
This PR fixes that by stopping the rendering process.
It has two main benefits:
Failed to get render key from cache
. This is displayed when the image renderer tries to access Grafana after the request is cancelled because the render key has been deleted but this makes it more complicated to investigate real issues.Fixes #186