Skip to content
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 requests on client cancellation #588

Merged
merged 9 commits into from
Jan 10, 2025

Conversation

AgnesToulet
Copy link
Contributor

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:

  • Improve performance when the image renderer is receiving a lot of requests from Grafana, especially when it comes from the alerting feature. In the alerting feature, image renderer requests are stacked when they exceed the concurrent request limit, so they can be made after a few seconds, be cancelled right after that because they exceed the timeout and still use the image renderer CPU and memory to render the image.
  • Remove logs 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

} 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

The
URL
of this request depends on a
user-provided value
.

Copilot Autofix AI 22 days ago

To fix the problem, we need to ensure that the user input is validated and sanitized before being used in the page.goto method. One effective way to do this is to use an allow-list of acceptable URLs or domains. This way, we can ensure that only trusted URLs are used in the request.

  1. Create an allow-list of acceptable URLs or domains.
  2. Validate the user input against this allow-list.
  3. If the input is valid, proceed with the request; otherwise, reject the request with an appropriate error message.
Suggested changeset 1
src/service/http-server.ts
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/service/http-server.ts b/src/service/http-server.ts
--- a/src/service/http-server.ts
+++ b/src/service/http-server.ts
@@ -181,2 +181,3 @@
 
+    const allowedUrls = ['https://example.com', 'https://another-example.com'];
     if (!req.query.url) {
@@ -184,2 +185,5 @@
     }
+    if (!allowedUrls.includes(req.query.url)) {
+      throw boom.badRequest('Invalid url parameter');
+    }
 
EOF
@@ -181,2 +181,3 @@

const allowedUrls = ['https://example.com', 'https://another-example.com'];
if (!req.query.url) {
@@ -184,2 +185,5 @@
}
if (!allowedUrls.includes(req.query.url)) {
throw boom.badRequest('Invalid url parameter');
}

Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options

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

The
URL
of this request depends on a
user-provided value
.
@AgnesToulet AgnesToulet requested review from a team, juanicabanas and evictorero and removed request for a team December 17, 2024 20:08
Copy link
Contributor

@juanicabanas juanicabanas left a 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 🚀

@AgnesToulet AgnesToulet merged commit cd35606 into master Jan 10, 2025
3 of 4 checks passed
@AgnesToulet AgnesToulet deleted the agnestoulet/cancel-requests branch January 10, 2025 13:21
@AgnesToulet AgnesToulet changed the title Support cancel rendering on client cancellation Support cancel rendering requests on client cancellation Jan 14, 2025
@AgnesToulet AgnesToulet mentioned this pull request Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server doesn't cancel browser rendering when client cancel
2 participants