Repository navigation
Resolve CodeQL security findings - #24
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewer's GuideResolves the CodeQL findings by strengthening browser entropy and credential handling, preventing format-string injection in logs, making script parsing more tolerant, and adding targeted regressions; generated Graphify artifacts are refreshed from the updated source commit. Sequence diagram for secure browser identifier generationsequenceDiagram
participant Browser as Browser runtime
participant Workspace as workspace.js
participant Crypto as Web Crypto
Browser->>Workspace: makeId(prefix)
Workspace->>Crypto: getRandomValues(entropy)
Crypto-->>Workspace: 128 bits of entropy
Workspace-->>Browser: prefix_randomHex
Sequence diagram for keyed credential validationsequenceDiagram
participant Request as Incoming request
participant Guard as request-guard.cjs
participant HMAC as crypto.createHmac
Request->>Guard: constantTimeSecretEqual(left, right)
Guard->>HMAC: credentialTag(left)
HMAC-->>Guard: left credential tag
Guard->>HMAC: credentialTag(right)
HMAC-->>Guard: right credential tag
Guard->>Guard: timingSafeEqual(leftTag, rightTag)
Guard-->>Request: authenticated or rejected
Flow diagram for protected idempotency fingerprintsflowchart LR
Payload[Request payload] --> Canonical[canonicalFingerprintValue]
Canonical --> HMAC[credentialTag for sensitive values]
HMAC --> Fingerprint[idempotencyFingerprint]
Fingerprint --> Store[Idempotency coordination]
SharedKey[CREDENTIAL_TAG_KEY] --> HMAC
SharedKey --> Fingerprint
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 4 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="request-guard.cjs" line_range="244" />
<code_context>
function idempotencyPrincipal(req, payload, funding, authorization) {
if (authorization?.authenticated) {
- return `access:${secretDigest(bearerCredential(req).token).toString("hex")}`;
+ return `access:${credentialTag(bearerCredential(req).token).toString("hex")}`;
}
if (funding === "byok") {
</code_context>
<issue_to_address>
**Retries duplicate billable work**
When old and new server versions overlap while an idempotency record from the old implementation remains live, `idempotencyPrincipal()` and `idempotencyFingerprint()` derive different scopes or fingerprints across versions, so the shared store misses the old record or returns a conflict; callers see duplicate work, charges, or an idempotency error.
Keep scope and fingerprint derivations compatible across versions until old records expire, or migrate the live records before changing them.
Also at `request-guard.cjs:247-248`, `request-guard.cjs:384-387`.
</issue_to_address>
### Comment 2
<location path="request-guard.cjs" line_range="44-47" />
<code_context>
+// Credential tags must be deterministic across shared-guard instances but do
+// not need cross-process stability when coordination is process-local.
+const configuredCredentialTagKey = String(process.env.TOKEN_OPTIMIZER_SHARED_GUARD_SECRET || "");
+const CREDENTIAL_TAG_KEY = Buffer.byteLength(configuredCredentialTagKey) >= 32
+ ? Buffer.from(configuredCredentialTagKey, "utf8")
+ : crypto.randomBytes(32);
+
function requiredString(missingMessage) {
</code_context>
<issue_to_address>
**Shared idempotency keys diverge**
When separate processes use shared adapters configured with `options.secret` or `options.environment` that differs from `process.env.TOKEN_OPTIMIZER_SHARED_GUARD_SECRET`, `idempotencyFingerprint` uses the key captured from `process.env`, while shared-guard callers can configure a different key through the adapter’s `options.secret` or `options.environment`. Separate instances then produce different fingerprints for the same request, so the shared store rejects retries as conflicts instead of joining or replaying them.
Use one shared key source for both fingerprint generation and adapter configuration, or reject configurations where those keys differ.
</issue_to_address>
### Comment 3
<location path="outputs/workspace.js" line_range="34" />
<code_context>
+ const entropy = new Uint8Array(16);
+ globalThis.crypto.getRandomValues(entropy);
+ const randomHex = Array.from(entropy, (byte) => byte.toString(16).padStart(2, "0")).join("");
+ return `${prefix}_${randomHex}`;
}
</code_context>
<issue_to_address>
**Browser trace IDs lose correlation**
When the workspace uses its locally generated trace ID and telemetry normalizes it, `makeId()` returns `trace_<hex>`, but `normalizeTraceId()` in both telemetry modules accepts only `trace_<segment>_<segment>` and returns `null`; telemetry events lose their trace correlation.
Make workspace-generated trace IDs match the format accepted by both telemetry modules, or update those modules to accept the generated format.
</issue_to_address>
### Comment 4
<location path="tests/frontend-static.test.cjs" line_range="23" />
<code_context>
assert.equal(fs.existsSync(retiredGenerator), false, "The retired browser-key generator must not be shipped.");
assert.match(fs.readFileSync(faviconPath, "utf8"), /<svg\b/);
+assert.doesNotMatch(workspaceSource, /Math\.random\s*\(/, "Workspace identifiers must not use predictable randomness.");
+assert.match(workspaceSource, /crypto\.getRandomValues\s*\(/, "Workspace identifiers must use browser CSPRNG entropy.");
+assert.equal(
+ [...scriptElements("<script>globalThis.valid = true;</script >")].length,
</code_context>
<issue_to_address>
**Predictable IDs pass the check**
When `makeId` calls `getRandomValues` but returns a constant or otherwise predictable identifier, the `frontend-static` test only checks that `workspace.js` contains a `getRandomValues` call (line 23); `makeId` can call it but return a constant ID, and the test still passes.
Test `makeId`’s returned IDs using a stubbed Web Crypto source, or assert that the generated entropy contributes to its return value.
</issue_to_address>| function idempotencyPrincipal(req, payload, funding, authorization) { | ||
| if (authorization?.authenticated) { | ||
| return `access:${secretDigest(bearerCredential(req).token).toString("hex")}`; | ||
| return `access:${credentialTag(bearerCredential(req).token).toString("hex")}`; |
There was a problem hiding this comment.
🟡 Medium · Retries duplicate billable work
When old and new server versions overlap while an idempotency record from the old implementation remains live, idempotencyPrincipal() and idempotencyFingerprint() derive different scopes or fingerprints across versions, so the shared store misses the old record or returns a conflict; callers see duplicate work, charges, or an idempotency error.
Keep scope and fingerprint derivations compatible across versions until old records expire, or migrate the live records before changing them.
Also at request-guard.cjs:247-248, request-guard.cjs:384-387.
Prompt for AI agents
In `request-guard.cjs` at line 244:
**Retries duplicate billable work**
When old and new server versions overlap while an idempotency record from the old implementation remains live, `idempotencyPrincipal()` and `idempotencyFingerprint()` derive different scopes or fingerprints across versions, so the shared store misses the old record or returns a conflict; callers see duplicate work, charges, or an idempotency error.
Keep scope and fingerprint derivations compatible across versions until old records expire, or migrate the live records before changing them.
Also at `request-guard.cjs:247-248`, `request-guard.cjs:384-387`.| const configuredCredentialTagKey = String(process.env.TOKEN_OPTIMIZER_SHARED_GUARD_SECRET || ""); | ||
| const CREDENTIAL_TAG_KEY = Buffer.byteLength(configuredCredentialTagKey) >= 32 | ||
| ? Buffer.from(configuredCredentialTagKey, "utf8") | ||
| : crypto.randomBytes(32); |
There was a problem hiding this comment.
🟡 Medium · Shared idempotency keys diverge
When separate processes use shared adapters configured with options.secret or options.environment that differs from process.env.TOKEN_OPTIMIZER_SHARED_GUARD_SECRET, idempotencyFingerprint uses the key captured from process.env, while shared-guard callers can configure a different key through the adapter’s options.secret or options.environment. Separate instances then produce different fingerprints for the same request, so the shared store rejects retries as conflicts instead of joining or replaying them.
Use one shared key source for both fingerprint generation and adapter configuration, or reject configurations where those keys differ.
Prompt for AI agents
In `request-guard.cjs` at lines 44-47:
**Shared idempotency keys diverge**
When separate processes use shared adapters configured with `options.secret` or `options.environment` that differs from `process.env.TOKEN_OPTIMIZER_SHARED_GUARD_SECRET`, `idempotencyFingerprint` uses the key captured from `process.env`, while shared-guard callers can configure a different key through the adapter’s `options.secret` or `options.environment`. Separate instances then produce different fingerprints for the same request, so the shared store rejects retries as conflicts instead of joining or replaying them.
Use one shared key source for both fingerprint generation and adapter configuration, or reject configurations where those keys differ.| const entropy = new Uint8Array(16); | ||
| globalThis.crypto.getRandomValues(entropy); | ||
| const randomHex = Array.from(entropy, (byte) => byte.toString(16).padStart(2, "0")).join(""); | ||
| return `${prefix}_${randomHex}`; |
There was a problem hiding this comment.
🟡 Medium · Browser trace IDs lose correlation
When the workspace uses its locally generated trace ID and telemetry normalizes it, makeId() returns trace_<hex>, but normalizeTraceId() in both telemetry modules accepts only trace_<segment>_<segment> and returns null; telemetry events lose their trace correlation.
Make workspace-generated trace IDs match the format accepted by both telemetry modules, or update those modules to accept the generated format.
Prompt for AI agents
In `outputs/workspace.js` at line 34:
**Browser trace IDs lose correlation**
When the workspace uses its locally generated trace ID and telemetry normalizes it, `makeId()` returns `trace_<hex>`, but `normalizeTraceId()` in both telemetry modules accepts only `trace_<segment>_<segment>` and returns `null`; telemetry events lose their trace correlation.
Make workspace-generated trace IDs match the format accepted by both telemetry modules, or update those modules to accept the generated format.| assert.equal(fs.existsSync(retiredGenerator), false, "The retired browser-key generator must not be shipped."); | ||
| assert.match(fs.readFileSync(faviconPath, "utf8"), /<svg\b/); | ||
| assert.doesNotMatch(workspaceSource, /Math\.random\s*\(/, "Workspace identifiers must not use predictable randomness."); | ||
| assert.match(workspaceSource, /crypto\.getRandomValues\s*\(/, "Workspace identifiers must use browser CSPRNG entropy."); |
There was a problem hiding this comment.
🟡 Medium · Predictable IDs pass the check
When makeId calls getRandomValues but returns a constant or otherwise predictable identifier, the frontend-static test only checks that workspace.js contains a getRandomValues call (line 23); makeId can call it but return a constant ID, and the test still passes.
Test makeId’s returned IDs using a stubbed Web Crypto source, or assert that the generated entropy contributes to its return value.
Prompt for AI agents
In `tests/frontend-static.test.cjs` at line 23:
**Predictable IDs pass the check**
When `makeId` calls `getRandomValues` but returns a constant or otherwise predictable identifier, the `frontend-static` test only checks that `workspace.js` contains a `getRandomValues` call (line 23); `makeId` can call it but return a constant ID, and the test still passes.
Test `makeId`’s returned IDs using a stubbed Web Crypto source, or assert that the generated entropy contributes to its return value.
Summary
Closes CodeQL alerts #1, #2, #3, #4, #5, and #6.
Verification
Summary by Sourcery
Harden identifier generation, credential handling, logging, and script parsing to resolve the reported security findings.
Bug Fixes:
Enhancements:
Documentation:
Tests: