Fixed malformed headers emitting junk trace IDs#1897
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
Walkthrough
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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 `@src/helpers/context-header.ts`:
- Around line 14-20: The version field validation in the isValidTraceContext
logic currently accepts the reserved value `ff`, which violates the W3C Trace
Context specification. Add an additional condition to the isValidTraceContext
validation that explicitly checks version !== 'ff' to reject this reserved
version value, ensuring invalid trace metadata is not emitted downstream.
In `@src/helpers/context-header.unit.test.ts`:
- Around line 40-49: The test matrix in the it.each block for invalid
traceparent validation is missing a regression case for the reserved version
"ff", which means strict version validation isn't fully tested. Add a new
traceparent string to the test array that uses "ff" as the version prefix (the
first segment before the first dash) while keeping the other segments valid,
such as "ff-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01", to ensure the
reserved-version validation is properly covered by the test.
🪄 Autofix (Beta)
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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 89b497dc-b473-459c-87c3-db43e1c32658
📥 Commits
Reviewing files that changed from the base of the PR and between 7389379 and 6e6e3c8e4cd44a94631ce85df18268aa9e187dba.
📒 Files selected for processing (2)
src/helpers/context-header.tssrc/helpers/context-header.unit.test.ts
6e6e3c8 to
b630a49
Compare
b630a49 to
48e4a13
Compare
|
Hey @miloquinn, thanks for your contribution 🙏 I've left some minor comments, this looks good overall! Can you ping me once you've gone through addressing those? |
48e4a13 to
04545c1
Compare
04545c1 to
62de39f
Compare
Addresses review feedback on the traceparent validation: - The W3C Trace Context spec explicitly forbids version 'ff', but the regex alone accepted it, so a spoofed header could still emit invalid trace metadata to Cloud Logging. - Replaced the hard-coded 32/16-zero string comparisons with /^0+$/ tests — the preceding length-checked hex regexes make them equivalent and easier to read. - Added a comment explaining why the validation exists, and a regression test for the reserved version.
The hand-rolled validation duplicated parseTraceParent from @opentelemetry/core, which is already a direct dependency — two W3C parsers in one process invite drift as the spec evolves. It also got one rule wrong: the length check rejected future-version headers carrying extra fields, which the spec says receivers should parse by reading the first four fields. Delegating to the library fixes that for free and shrinks the helper to the mapping that is actually local (null-object shape and the sampled-flag conversion). Behavior is otherwise identical: version 'ff', all-zero IDs, non-hex fields, and version-00 headers with extra fields are still rejected, pinned by the existing test matrix plus a new forward-compat case.
69a5743 to
8ff7d91
Compare
traceparentheader before returning Cloud Logging trace context, so malformed or spoofed headers no longer emit junk trace IDs.parseTraceParentfrom@opentelemetry/core(already a direct dependency) rather than a hand-rolled parser. This enforces the spec rules — hex format, reserved versionff, all-zero trace/span IDs, no extra fields on version00— and correctly parses future-version headers that carry extra fields (the spec says receivers should read the first four fields).getTraceContextis now a thin mapping: null-object shape on missing/invalid input, andtraceFlags & TraceFlags.SAMPLED→sampled.ff, wrong field count).