Repository navigation
fix(playwright): send trace context headers only to origins under test #6993
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
Merged
Merged
Changes from all commits
Commits
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
This file was deleted.
Oops, something went wrong.
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,110 @@ | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.Playwright; | ||
|
|
||
| namespace TUnit.Playwright; | ||
|
|
||
| /// <summary> | ||
| /// Adds W3C trace context headers to requests sent to the origins under test only. | ||
| /// Context-wide <c>ExtraHTTPHeaders</c> would also reach third-party origins, where the | ||
| /// non-safelisted <c>traceparent</c> header triggers CORS preflights that usually fail. | ||
| /// </summary> | ||
| internal sealed class PlaywrightTraceContextRoute | ||
| { | ||
| private readonly Regex _origins; | ||
| private readonly KeyValuePair<string, string>[] _headers; | ||
|
|
||
| private PlaywrightTraceContextRoute(Regex origins, KeyValuePair<string, string>[] headers) | ||
| { | ||
| _origins = origins; | ||
| _headers = headers; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Captures the trace headers of the current <see cref="System.Diagnostics.Activity"/>. | ||
| /// Returns <c>null</c> when propagation is disabled, there is no activity, or no origin | ||
| /// is known. Without explicit <paramref name="origins"/>, the origin of | ||
| /// <see cref="BrowserNewContextOptions.BaseURL"/> is used. | ||
| /// </summary> | ||
| public static PlaywrightTraceContextRoute? Create(bool propagate, IReadOnlyList<string>? origins, BrowserNewContextOptions options) | ||
| { | ||
| #if NET | ||
| if (!propagate || System.Diagnostics.Activity.Current is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var pattern = BuildOriginPattern(origins, options.BaseURL); | ||
| if (pattern is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
| Telemetry.PlaywrightActivityPropagator.InjectInto(headers); | ||
| return headers.Count == 0 | ||
| ? null | ||
| : new PlaywrightTraceContextRoute(pattern, headers.ToArray()); | ||
| #else | ||
| return null; | ||
| #endif | ||
| } | ||
|
|
||
| public Task RegisterAsync(IBrowserContext context) => context.RouteAsync(_origins, HandleAsync); | ||
|
thomhurst marked this conversation as resolved.
|
||
|
|
||
| private Task HandleAsync(IRoute route) | ||
| { | ||
| var headers = new Dictionary<string, string>(route.Request.Headers, StringComparer.OrdinalIgnoreCase); | ||
| foreach (var header in _headers) | ||
| { | ||
| // Headers set by the page or by ExtraHTTPHeaders win. | ||
| if (!headers.ContainsKey(header.Key)) | ||
| { | ||
| headers[header.Key] = header.Value; | ||
| } | ||
| } | ||
|
|
||
| // Fallback rather than Continue, so any handler registered before this one still runs. | ||
| return route.FallbackAsync(new RouteFallbackOptions { Headers = headers }); | ||
|
thomhurst marked this conversation as resolved.
|
||
| } | ||
|
|
||
| internal static Regex? BuildOriginPattern(IReadOnlyList<string>? origins, string? baseUrl) | ||
| { | ||
| if (origins is null) | ||
| { | ||
| return string.IsNullOrEmpty(baseUrl) ? null : BuildOriginPattern([baseUrl!], null); | ||
| } | ||
|
|
||
| if (origins.Count == 0) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var pattern = new StringBuilder("^(?:"); | ||
| for (var i = 0; i < origins.Count; i++) | ||
| { | ||
| if (i > 0) | ||
| { | ||
| pattern.Append('|'); | ||
| } | ||
|
|
||
| pattern.Append(Regex.Escape(NormalizeOrigin(origins[i]))); | ||
| } | ||
|
|
||
| // Match the origin exactly: "https://app.test" must not match "https://app.test.evil". | ||
| pattern.Append(")(?:[/?#]|$)"); | ||
| return new Regex(pattern.ToString(), RegexOptions.IgnoreCase); | ||
| } | ||
|
|
||
| private static string NormalizeOrigin(string origin) | ||
| { | ||
| if (!Uri.TryCreate(origin, UriKind.Absolute, out var uri) | ||
| || (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)) | ||
| { | ||
| throw new ArgumentException($"Trace context origin '{origin}' must be an absolute http or https URL.", nameof(origin)); | ||
| } | ||
|
|
||
| // Scheme, host and non-default port, as the browser writes request URLs. | ||
| return uri.GetLeftPart(UriPartial.Authority); | ||
| } | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.