fix(grafana): sanitize trailing slash on baseUrl in makeGrafanaRawRequest - #604
fix(grafana): sanitize trailing slash on baseUrl in makeGrafanaRawRequest#604minutechreview wants to merge 6 commits into
Conversation
…uest makeGrafanaRawRequest built its request URL by naive string concatenation, so a Grafana instance URL configured with a trailing slash (e.g. https://grafana.example.com/) produced a double-slash request path. Strip the trailing slash before concatenating, matching the sanitization already applied to the JSON path via the shared request builder. Fixes corsairdev#599
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthrough
ChangesGrafana URL sanitization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Greptile SummaryThe PR normalizes trailing slashes in Grafana raw-request base URLs and adds focused regression coverage.
Confidence Score: 4/5The PR appears safe to merge; the only concern is non-blocking test-mock type-safety documentation. The URL normalization works for all current callers, which consistently supply leading-slash endpoints, while the new tests contain a maintainability issue in their broad mock assertions. Files Needing Attention: packages/grafana/client.test.ts Important Files Changed
Reviews (1): Last reviewed commit: "fix(grafana): sanitize trailing slash on..." | Re-trigger Greptile |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/grafana/client.ts`:
- Around line 94-95: Validate the destination in makeGrafanaRawRequest before
attaching or sending ctx.key: parse baseUrl and require HTTPS plus an approved
Grafana origin, rejecting arbitrary base_url_override values. Preserve the
existing slash-trimming and endpoint concatenation for current slash-prefixed
callers.
- Around line 94-95: Validate the resolved Grafana base URL before fetch in the
client request flow, rejecting any non-https: URL, including values supplied
through base_url_override. Ensure the validation occurs before constructing or
sending the credential-bearing request and preserves HTTPS URL handling.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 4cd81db8-6ed2-45de-b09a-a510e23f1f26
📒 Files selected for processing (2)
packages/grafana/client.test.tspackages/grafana/client.ts
Sending the Grafana bearer token to a non-HTTPS baseUrl would transmit it in cleartext. Reject any baseUrl that isn't https:// before building the request, matching CodeRabbit's review on corsairdev#604. Addressed in review on corsairdev#599
|
Thanks for the review.
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/grafana/client.ts`:
- Around line 95-99: Update the HTTPS validation in the Grafana client URL
handling to parse baseUrl with URL, accept any casing of the HTTPS scheme by
checking the normalized protocol equals https:, and map URL parsing failures to
GrafanaAPIError. Preserve rejection of non-HTTPS URLs and add coverage for a
mixed-case HTTPS scheme.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 261e03d1-68ab-46d9-a73c-5805e534bb27
📒 Files selected for processing (2)
packages/grafana/client.test.tspackages/grafana/client.ts
|
@minutechreview is attempting to deploy a commit to the corsair Team on Vercel. A member of the Team first needs to authorize it. |
…eGrafanaRawRequest
|
LGTM trailing-slash strip + HTTPS reject before fetch looks right, tests cover it. Optional : |
… check
startsWith('https://') rejected valid but differently-cased schemes
like HTTPS://. Parse with URL and compare protocol instead, per
review feedback on corsairdev#604.
|
Good catch — fixed in bc8a72d: now parses |
|
LGTM |
Description
makeGrafanaRawRequestbuilt its request URL with naive string concatenation (${baseUrl}${endpoint}), so a Grafana instance URL configured with a trailing slash (e.g.https://grafana.example.com/) produced a double-slash request path. This fixes the same class of bug already fixed for Jira'scloudUrlin #503.While fixing this, CodeRabbit's review on this PR also flagged that a non-HTTPS
baseUrlwould send the Grafana bearer token over cleartext HTTP. Since this PR was already touching the URL-building logic in that function, I fixed that too:makeGrafanaRawRequestnow throws aGrafanaAPIErrorbefore callingfetchif the sanitizedbaseUrlisn'thttps://. Updated the function's JSDoc to document both behaviors.I deliberately left CodeRabbit's other suggestion (an allowlist of "approved Grafana origins") out of scope —
baseUrlis documented as pointing at a user's self-hosted Grafana instance, so there's no fixed origin to allowlist against without a larger product decision. Left a comment on the review thread explaining that.Fixes #599
Checklist
pnpm lintand all checks pass (3 pre-existing warnings elsewhere in the repo, unrelated to this change and present onmain)pnpm typecheckand there are no TypeScript errorspnpm buildand@corsair-dev/grafanabuilds successfullypnpm --filter @corsair-dev/grafana testand the unit tests I added pass (packages/grafana/api.test.tsis a pre-existing live-integration suite gated on realGRAFANA_URL/BEARER_TOKENcredentials that aren't available in this environment — it fails identically onmainwithout them, confirmed by checking out the pre-fix file and re-running)makeGrafanaRawRequest)Screenshots / Demos
Pure request-building fix, no UI. Passing CI run for this branch (build + full test suite, all packages): https://github.com/corsairdev/corsair/actions/runs/30936881333/job/92085139297
Local test run demonstrating the fixed behavior end-to-end (mocked
fetch, realmakeGrafanaRawRequestcall, each assertion checks the actual URLfetchwas called with, or that it wasn't called at all for the rejected case):Additional Notes
No breaking changes, no new dependencies. Scope confined to
packages/grafana/client.tsand its test file.Summary by CodeRabbit