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
11
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
11 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 f3fa6b7
Merge branch 'main' into fix/1199-sdk-url-matching
aram356 1f82b07
Address routine SDK URL matching review feedback
ChristianPavilonis 39adfd5
Merge main into fix/1199-sdk-url-matching
ChristianPavilonis bb8a85e
Record served HTML SDK ownership and recognition limits
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
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,36 @@ | ||
| //! Shared input policy for SDK URL ownership checks. | ||
|
|
||
| use std::borrow::Cow; | ||
|
|
||
| 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') { | ||
|
ChristianPavilonis marked this conversation as resolved.
Resolved
|
||
| return None; | ||
| } | ||
| let trimmed = url.trim_matches(|character| character <= '\u{20}'); | ||
| let value = if trimmed.contains(['\t', '\n', '\r']) { | ||
| Cow::Owned(trimmed.replace(['\t', '\n', '\r'], "")) | ||
| } else { | ||
| Cow::Borrowed(trimmed) | ||
| }; | ||
| 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() | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
| //! 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)] | ||
| #[serde(rename_all = "camelCase")] | ||
| 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>, | ||
| /// Owner once `lol_html` has read the attribute, when that differs from `owner`. | ||
| pub(super) server_html_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")] { | ||
|
ChristianPavilonis marked this conversation as resolved.
Resolved
|
||
| 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 the SDK rewrite of a corpus URL after `lol_html` reads it from markup. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if HTML processing fails or the output disagrees with the server-side owner. | ||
| pub(super) fn assert_sdk_html_attribute_rewrite( | ||
|
ChristianPavilonis marked this conversation as resolved.
Resolved
ChristianPavilonis marked this conversation as resolved.
Resolved
|
||
| integration: &dyn IntegrationAttributeRewriter, | ||
| case: &SdkUrlCase, | ||
| sdk_path: &str, | ||
| ) { | ||
| let owner = case.server_html_owner.as_ref().or(case.owner.as_ref()); | ||
| let owned = owner.map(String::as_str) == Some(integration.integration_id()); | ||
| 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"); | ||
| if let AttributeRewriteAction::Replace(next) = | ||
| integration.rewrite(attribute, &value, &context) | ||
| { | ||
| element.set_attribute(attribute, &next)?; | ||
| } | ||
| Ok(()) | ||
| })], | ||
| ..RewriteStrSettings::default() | ||
| }, | ||
| ) | ||
| .expect("should process SDK URL markup"); | ||
| let expected = if owned { | ||
| format!("<{tag} {attribute}=\"{sdk_path}\"></{tag}>") | ||
| } else { | ||
| html | ||
| }; | ||
| assert_eq!( | ||
| output, expected, | ||
| "should apply expected HTML {attribute} rewrite 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
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.
📝 note:
mainnow conflicts with this entry, because #1107 changed an example URL in therewrite.exclude_domainsentry just below it. GitHub reports the PR as conflicting, so it hasn't run the test or format workflows onbb8a85e0f; only CodeQL ran. The resolution is a union: keepmain's file and put this entry first under### Fixed.I ran that merge locally (
git merge origin/main, union on this file only). The merged tree differs frommainin exactly this PR's 16 files.mainalso moved EdgeZero to v0.0.11 in #1107, which this PR's Rust changes don't call. On the merged tree, these pass:cargo fmt,cargo clippy-fastly, and the 20 Lockr and Permutive tests, both natively and under Viceroy (cargo test-fastly)main's trace-assets test), and the bundle buildCI runs the rest once the conflict is resolved.