-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add inbound header validation and tests to trace package #3
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
Open
daveseddon-runpod
wants to merge
5
commits into
master
Choose a base branch
from
feat/trace-validation-and-tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6e87d0
feat: add inbound header validation and tests to trace package
randomizedcoder d2accae
fix: address self-review — validate Python X-Trace-Start, parity, and…
randomizedcoder c964451
chore: drop internal planning doc from PR (planning-only, not shipped)
randomizedcoder cabcbaa
fix(trace): clamp a future X-Trace-Start silently instead of warning
randomizedcoder 251cfb8
feat(trace): bound inbound trace/request source to maxSourceLen (64)
randomizedcoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import unittest | ||
| from datetime import datetime, timedelta, timezone | ||
| from trace import Trace, _resolve_id, _resolve_source, _valid_id | ||
|
|
||
|
|
||
| class TestValidId(unittest.TestCase): | ||
| def test_cases(self): | ||
| cases = [ | ||
| {"description": "canonical uuid", "input": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31", "expected": True}, | ||
| {"description": "service-prefixed id", "input": "trace_018f3a1c2b4d", "expected": True}, | ||
| {"description": "dotted id", "input": "svc.abc.123", "expected": True}, | ||
| {"description": "max length accepted", "input": "a" * 200, "expected": True}, | ||
| {"description": "empty rejected", "input": "", "expected": False}, | ||
| {"description": "one over max rejected", "input": "a" * 201, "expected": False}, | ||
| {"description": "CRLF rejected", "input": "abc\r\nX-Evil: 1", "expected": False}, | ||
| {"description": "newline rejected", "input": "abc\ndef", "expected": False}, | ||
| {"description": "space rejected", "input": "abc def", "expected": False}, | ||
| {"description": "control byte rejected", "input": "abc\x00def", "expected": False}, | ||
| {"description": "non-ascii rejected", "input": "abcé", "expected": False}, | ||
| {"description": "slash rejected", "input": "a/b", "expected": False}, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| self.assertEqual(_valid_id(c["input"]), c["expected"]) | ||
|
|
||
|
|
||
| class TestResolveId(unittest.TestCase): | ||
| def test_cases(self): | ||
| cases = [ | ||
| {"description": "valid id propagated", "input": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31", "propagate": True}, | ||
| {"description": "absent id generated", "input": "", "propagate": False}, | ||
| {"description": "poisoned id regenerated", "input": "abc\r\nX-Evil: 1", "propagate": False}, | ||
| {"description": "over-long id regenerated", "input": "a" * 201, "propagate": False}, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| got = _resolve_id(c["input"]) | ||
| if c["propagate"]: | ||
| self.assertEqual(got, c["input"]) | ||
| else: | ||
| self.assertNotEqual(got, c["input"]) | ||
| self.assertTrue(_valid_id(got), f"generated id {got!r} is not valid") | ||
|
|
||
|
|
||
| class TestResolveSource(unittest.TestCase): | ||
| def test_cases(self): | ||
| cases = [ | ||
| {"description": "empty source stays empty", "input": "", "expected": ""}, | ||
| {"description": "valid source kept", "input": "runpod-graphql", "expected": "runpod-graphql"}, | ||
| {"description": "source at max length kept", "input": "a" * 64, "expected": "a" * 64}, | ||
| {"description": "poisoned source dropped to empty", "input": "svc\r\nX-Evil: 1", "expected": ""}, | ||
| {"description": "spaced source dropped to empty", "input": "not a slug", "expected": ""}, | ||
| {"description": "over-length source dropped to empty", "input": "a" * 65, "expected": ""}, | ||
| {"description": "charset-valid but over-length source dropped", "input": "a" * 200, "expected": ""}, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| self.assertEqual(_resolve_source(c["input"]), c["expected"]) | ||
|
|
||
|
|
||
| class TestFromHeaders(unittest.TestCase): | ||
| def test_cases(self): | ||
| good_trace = "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31" | ||
| good_req = "018f3a1c-2b4d-7e8f-9a0b-1c2d3e4f5061" | ||
| cases = [ | ||
| { | ||
| "description": "valid ids propagated", | ||
| "headers": {"X-Trace-ID": good_trace, "X-Request-ID": good_req, "X-Trace-Source": "main-ui"}, | ||
| "want_trace": good_trace, | ||
| "want_request": good_req, | ||
| "want_trace_source": "main-ui", | ||
| }, | ||
| { | ||
| "description": "absent ids generated", | ||
| "headers": {}, | ||
| "want_trace": None, | ||
| "want_request": None, | ||
| "want_trace_source": "", | ||
| }, | ||
| { | ||
| "description": "poisoned trace id regenerated, valid request id kept", | ||
| "headers": {"X-Trace-ID": "abc\r\nX-Evil: 1", "X-Request-ID": good_req}, | ||
| "want_trace": None, | ||
| "want_request": good_req, | ||
| "want_trace_source": "", | ||
| }, | ||
| { | ||
| "description": "poisoned source dropped to empty", | ||
| "headers": {"X-Trace-ID": good_trace, "X-Request-ID": good_req, "X-Trace-Source": "svc\r\nX-Evil"}, | ||
| "want_trace": good_trace, | ||
| "want_request": good_req, | ||
| "want_trace_source": "", | ||
| }, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| t = Trace.from_headers(dict(c["headers"])) | ||
| if c["want_trace"] is None: | ||
| self.assertTrue(_valid_id(t.trace_id)) | ||
| self.assertNotIn("\r", t.trace_id) | ||
| else: | ||
| self.assertEqual(t.trace_id, c["want_trace"]) | ||
| if c["want_request"] is None: | ||
| self.assertTrue(_valid_id(t.request_id)) | ||
| else: | ||
| self.assertEqual(t.request_id, c["want_request"]) | ||
| self.assertEqual(t.trace_source, c["want_trace_source"]) | ||
|
|
||
|
|
||
| class TestSaveToHeaders(unittest.TestCase): | ||
| def test_writes_go_five_headers_and_fresh_request_id(self): | ||
| t = Trace.from_headers({"X-Trace-ID": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31"}) | ||
| original_request_id = t.request_id | ||
| headers: dict[str, str] = {} | ||
| t.save_to_headers(headers) | ||
|
|
||
| for key in ("X-Trace-ID", "X-Request-ID", "X-Trace-Start", "X-Trace-Source", "X-Request-Source"): | ||
| self.assertIn(key, headers) | ||
|
|
||
| self.assertEqual(headers["X-Trace-ID"], t.trace_id) | ||
| # Regression: X-Request-ID must be a plain string, not a tuple (trailing-comma bug). | ||
| self.assertIsInstance(headers["X-Request-ID"], str) | ||
| # A fresh sub-request id, distinct from the inbound one. | ||
| self.assertNotEqual(headers["X-Request-ID"], original_request_id) | ||
|
|
||
| def test_emits_no_unsafe_bytes_from_poisoned_trace(self): | ||
| # Every inbound header poisoned, including X-Trace-Start — the timestamp is | ||
| # the header that previously leaked CR/LF straight through save_to_headers. | ||
| cases = [ | ||
| {"description": "poisoned trace id", "header": "X-Trace-ID", "value": "abc\r\nX-Evil: 1"}, | ||
| {"description": "poisoned trace source", "header": "X-Trace-Source", "value": "svc\r\nX-Evil: 2"}, | ||
| {"description": "poisoned request id", "header": "X-Request-ID", "value": "req\r\nX-Evil: 3"}, | ||
| {"description": "poisoned trace start", "header": "X-Trace-Start", "value": "2020-01-01T00:00:00Z\r\nX-Evil: 4"}, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| t = Trace.from_headers({c["header"]: c["value"]}) | ||
| headers: dict[str, str] = {} | ||
| t.save_to_headers(headers) | ||
| for key, value in headers.items(): | ||
| self.assertNotIn("\r", value, f"{key} carries CR") | ||
| self.assertNotIn("\n", value, f"{key} carries LF") | ||
|
|
||
| def test_malformed_trace_start_falls_back_to_now(self): | ||
| cases = [ | ||
| {"description": "non-RFC3339 falls back", "value": "not-a-date"}, | ||
| {"description": "CRLF-poisoned falls back", "value": "2020-01-01T00:00:00Z\r\nX-Evil: 1"}, | ||
| ] | ||
| for c in cases: | ||
| with self.subTest(c["description"]): | ||
| t = Trace.from_headers({"X-Trace-ID": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31", "X-Trace-Start": c["value"]}) | ||
| # The stored value re-parses as an RFC3339 timestamp (the fallback | ||
| # to now), never the poisoned input. | ||
| self.assertNotIn("\r", t.trace_start) | ||
| parsed = datetime.fromisoformat(t.trace_start.replace("Z", "+00:00")) | ||
| delta = abs((datetime.now(timezone.utc) - parsed).total_seconds()) | ||
| self.assertLess(delta, 5, f"trace_start {t.trace_start!r} is not ~now") | ||
|
|
||
| def test_valid_trace_start_is_preserved(self): | ||
| t = Trace.from_headers( | ||
| {"X-Trace-ID": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31", "X-Trace-Start": "2020-01-01T00:00:00Z"} | ||
| ) | ||
| parsed = datetime.fromisoformat(t.trace_start.replace("Z", "+00:00")) | ||
| self.assertEqual((parsed.year, parsed.month, parsed.day), (2020, 1, 1)) | ||
|
|
||
| def test_future_trace_start_is_clamped_to_now(self): | ||
| # A caller-supplied future timestamp is clamped to now (matching Go), so it | ||
| # never drives the trace's start time. | ||
| future = (datetime.now(timezone.utc) + timedelta(hours=1)).strftime("%Y-%m-%dT%H:%M:%SZ") | ||
| t = Trace.from_headers( | ||
| {"X-Trace-ID": "5f9c2e6a-1b3d-4c8e-9a0f-2b7c6d5e4f31", "X-Trace-Start": future} | ||
| ) | ||
| parsed = datetime.fromisoformat(t.trace_start.replace("Z", "+00:00")) | ||
| delta = abs((datetime.now(timezone.utc) - parsed).total_seconds()) | ||
| self.assertLess(delta, 5, f"future trace_start {t.trace_start!r} was not clamped to now") | ||
|
|
||
|
|
||
| class TestNewRegression(unittest.TestCase): | ||
| def test_new_does_not_raise(self): | ||
| # Regression: new() used datetime.datetime.now() and raised AttributeError. | ||
| t = Trace.new() | ||
| self.assertTrue(_valid_id(t.trace_id)) | ||
| self.assertTrue(_valid_id(t.request_id)) | ||
|
|
||
| def test_new_generates_unique_ids(self): | ||
| seen = set() | ||
| for _ in range(1000): | ||
| seen.add(Trace.new().trace_id) | ||
| self.assertEqual(len(seen), 1000) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
from_headersnow defaults an absentX-Request-Source/X-Trace-Sourceto""(via_resolve_source(headers.get(..., ""))), instead of the repo-wide"unknown"sentinel this replaced. That sentinel is still used bynew()in this same file, and bytrace/trace.go(thisServiceNamedefaults to"unknown"), sonew()andfrom_headersnow disagree on the default for no source given.Any consumer that logs, queries, or dashboards on
trace_source == "unknown"for untagged inbound requests will silently start seeing"". The PR frames the Python changes as bug fixes plus mirroring the Go validation, not as removing the"unknown"default, so this reads as an undocumented contract change.Is this deliberate cross-language alignment? If so, please also update
new()to match and call it out in the PR description. If not, keep"unknown"as the fallback here.