Repository navigation
Match SDK URLs by vendor hostname and path #1405
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
ChristianPavilonis
wants to merge
7
commits into
main
Choose a base branch
from
fix/1199-sdk-url-matching
base: main
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
7 commits
Select commit
Hold shift + click to select a range
21f91f4
Match SDK URLs by vendor hostname and path
ChristianPavilonis ed91166
Share SDK URL parsing and test support
ChristianPavilonis 34a776b
Merge branch 'main' into fix/1199-sdk-url-matching
prk-Jr a9fb1b2
Merge branch 'main' into fix/1199-sdk-url-matching
aram356 4df289f
Merge branch 'main' into fix/1199-sdk-url-matching
aram356 b4c6dcd
Merge branch 'main' into fix/1199-sdk-url-matching
aram356 d07ffe5
Expand SDK URL normalization safely, add ownership and attribute-rout…
ChristianPavilonis 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 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,31 @@ | ||
| //! Shared input policy for SDK URL ownership checks. | ||
|
|
||
| use url::Url; | ||
|
|
||
| /// Parse an HTTP(S) SDK URL with an explicit authority, or a protocol-relative URL. | ||
| /// | ||
| /// Applies browser C0/space trimming, removes tab/CR/LF, and accepts backslash | ||
| /// authority separators. Literal NULs, relative paths, and base-dependent shorthand | ||
| /// stay unowned; HTML replaces NULs rather than treating them as URL whitespace. | ||
| /// Browser/Rust ownership cases live in crates/trusted-server-js/lib/test/fixtures/sdk-url-ownership.json. | ||
| pub(super) fn parse_sdk_network_url(url: &str) -> Option<Url> { | ||
| if url.contains('\0') { | ||
| return None; | ||
| } | ||
| let value = url | ||
| .trim_matches(|character| character <= '\u{20}') | ||
| .replace(['\t', '\n', '\r'], ""); | ||
| let has_authority = |value: &str| matches!(value.as_bytes(), [b'/' | b'\\', b'/' | b'\\', ..]); | ||
| let parsed = if has_authority(&value) { | ||
| Url::parse(&format!("https:{value}")) | ||
| } else { | ||
| let (scheme, remainder) = value.split_once(':')?; | ||
| if !(scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https")) | ||
| || !has_authority(remainder) | ||
| { | ||
| return None; | ||
| } | ||
| Url::parse(&value) | ||
| }; | ||
| parsed.ok() | ||
| } |
119 changes: 119 additions & 0 deletions
119
crates/trusted-server-core/src/integrations/sdk_url_test_support.rs
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,119 @@ | ||
| //! Shared SDK URL corpus and attribute-rewrite assertions. | ||
|
|
||
| use lol_html::{RewriteStrSettings, element, rewrite_str}; | ||
| use serde::Deserialize; | ||
|
|
||
| use super::{AttributeRewriteAction, IntegrationAttributeContext, IntegrationAttributeRewriter}; | ||
|
|
||
| /// One raw URL and its expected integration owner from the shared corpus. | ||
| #[derive(Deserialize)] | ||
| pub(super) struct SdkUrlCase { | ||
| /// Descriptive case name for assertion failures. | ||
| pub(super) name: String, | ||
| /// Input before browser DOM URL resolution. | ||
| pub(super) url: String, | ||
| /// Expected integration ID, or none for an unowned URL. | ||
| pub(super) owner: Option<String>, | ||
| } | ||
|
|
||
| /// Load the URL ownership cases used by both Rust and browser tests. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the committed fixture is invalid. | ||
| pub(super) fn sdk_url_cases() -> Vec<SdkUrlCase> { | ||
| serde_json::from_str(include_str!( | ||
| "../../../trusted-server-js/lib/test/fixtures/sdk-url-ownership.json" | ||
| )) | ||
| .expect("should parse shared SDK URL ownership corpus") | ||
| } | ||
|
|
||
| /// Assert SDK rewriting for both script and link attribute contexts. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if the rewrite result disagrees with the corpus owner and SDK route. | ||
| pub(super) fn assert_sdk_url_rewrite( | ||
| integration: &dyn IntegrationAttributeRewriter, | ||
| case: &SdkUrlCase, | ||
| sdk_path: &str, | ||
| ) { | ||
| let expected = if case.owner.as_deref() == Some(integration.integration_id()) { | ||
| AttributeRewriteAction::Replace(sdk_path.to_string()) | ||
| } else { | ||
| AttributeRewriteAction::Keep | ||
| }; | ||
| for (attribute, element) in [("src", "script"), ("href", "link")] { | ||
| assert!( | ||
| integration.handles_attribute(attribute), | ||
| "should route {attribute} to the {} rewriter", | ||
| integration.integration_id() | ||
| ); | ||
| let context = IntegrationAttributeContext { | ||
| attribute_name: attribute, | ||
| element_name: element, | ||
| request_host: "edge.example.com", | ||
| request_scheme: "https", | ||
| origin_host: "origin.example.com", | ||
| }; | ||
| assert_eq!( | ||
| integration.rewrite(attribute, &case.url, &context), | ||
| expected, | ||
| "should apply expected {} {attribute} rewrite for {}: {}", | ||
| integration.integration_id(), | ||
| case.name, | ||
| case.url | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /// Assert that a matching SDK URL is rewritten after HTML attribute tokenization. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if HTML processing fails or the SDK attribute is not replaced. | ||
| pub(super) fn assert_sdk_html_attribute_rewrite( | ||
| integration: &dyn IntegrationAttributeRewriter, | ||
| case: &SdkUrlCase, | ||
| sdk_path: &str, | ||
| ) { | ||
| for (attribute, tag) in [("src", "script"), ("href", "link")] { | ||
| let html = format!( | ||
| "<{tag} {attribute}=\"{}\"></{tag}>", | ||
| case.url.replace('"', """) | ||
| ); | ||
| let context = IntegrationAttributeContext { | ||
| attribute_name: attribute, | ||
| element_name: tag, | ||
| request_host: "edge.example.com", | ||
| request_scheme: "https", | ||
| origin_host: "origin.example.com", | ||
| }; | ||
| let output = rewrite_str( | ||
| &html, | ||
| RewriteStrSettings { | ||
| element_content_handlers: vec![element!(tag, |element| { | ||
| let value = element | ||
| .get_attribute(attribute) | ||
| .expect("should read the SDK URL attribute"); | ||
| assert_eq!( | ||
| integration.rewrite(attribute, &value, &context), | ||
| AttributeRewriteAction::Replace(sdk_path.to_string()), | ||
| "should rewrite HTML {attribute} for {}", | ||
| case.name | ||
| ); | ||
| element.set_attribute(attribute, sdk_path)?; | ||
| Ok(()) | ||
| })], | ||
| ..RewriteStrSettings::default() | ||
| }, | ||
| ) | ||
| .expect("should process SDK URL markup"); | ||
| assert_eq!( | ||
| output, | ||
| format!("<{tag} {attribute}=\"{sdk_path}\"></{tag}>"), | ||
| "should replace the SDK URL in HTML for {}", | ||
| case.name | ||
| ); | ||
| } | ||
| } | ||
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
14 changes: 9 additions & 5 deletions
14
crates/trusted-server-js/lib/src/integrations/permutive/script_guard.ts
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.