Skip to content

Improve SentryTraceHeader constructor parameter validation #4604

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alexander-alderman-webb
Copy link

@alexander-alderman-webb alexander-alderman-webb commented Aug 4, 2025

📜 Description

Use similar, regex-based, validation logic to the PHP SDK for the sentry-trace HTTP header: https://github.com/getsentry/sentry-php/blob/master/src/Tracing/TransactionContext.php#L9

#skip-changelog

💡 Motivation and Context

💚 How did you test it?

Added test cases with malformed arguments, which would have caused errors later on previously. These include tests with IDs that are too short or too long, and IDs with invalid characters.

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

github-actions bot commented Aug 4, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 1a820e6

cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

github-actions bot commented Aug 4, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 443.38 ms 468.08 ms 24.70 ms
Size 1.58 MiB 2.09 MiB 521.84 KiB

Previous results on branch: feat/improve-sentry-trace-header-validation

Startup times

Revision Plain With Sentry Diff
38c63b5 414.92 ms 431.77 ms 16.85 ms
75111c8 414.61 ms 448.59 ms 33.98 ms

App size

Revision Plain With Sentry Diff
38c63b5 1.58 MiB 2.09 MiB 521.57 KiB
75111c8 1.58 MiB 2.09 MiB 521.57 KiB

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me, please see the comments.

I've noticed that in the constructor of SentryId there's a case where we would throw a more specific exception about the format of the UUID, but it also mentions that UUIDs could be formatted with dashes which is not true in this particular case as the spec states that the trace id should be exactly 32 hex characters, so I think this approach is fine.

Comment on lines 18 to 21
final Pattern SENTRY_TRACEPARENT_HEADER_REGEX =
Pattern.compile(
"^[ \\t]*(?<traceId>[0-9a-f]{32})-(?<spanId>[0-9a-f]{16})(?<sampled>-[01])?[ \\t]*$",
Pattern.CASE_INSENSITIVE);
Copy link
Member

@lcian lcian Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final Pattern SENTRY_TRACEPARENT_HEADER_REGEX =
Pattern.compile(
"^[ \\t]*(?<traceId>[0-9a-f]{32})-(?<spanId>[0-9a-f]{16})(?<sampled>-[01])?[ \\t]*$",
Pattern.CASE_INSENSITIVE);
private static final Pattern SENTRY_TRACEPARENT_HEADER_REGEX =
Pattern.compile(
"^[ \\t]*(?<traceId>[0-9a-f]{32})-(?<spanId>[0-9a-f]{16})(?<sampled>-[01])?[ \\t]*$",
Pattern.CASE_INSENSITIVE);

We can make it static so we're only going to compile it once, as compiling could be expensive. Otherwise, without static we would potentially compile the regex for each incoming request.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes great point. Addressed this.

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.

Look into replacing code for SentryTraceHeader parsing
2 participants