Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/core/uri/resolution.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,19 @@ auto URI::relative_to(const URI &base) -> URI & {
return *this;
}

// Hosts must match (but both can be null for URNs)
// The full authority must match (but components can be null for URNs)
if (this->userinfo_ != base.userinfo_) {
return *this;
}

if (this->host_ != base.host_) {
return *this;
}

if (this->port_ != base.port_) {
Copy link

Choose a reason for hiding this comment

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

relative_to() now checks host_/port_, but it can still incorrectly make a URI relative when userinfo_ differs (the method resets userinfo_, so resolving the result against base won’t round-trip to the original URI). Consider also requiring userinfo_ to match before stripping authority components.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

return *this;
}

// Special case: both URIs are exactly the same
if (this->path_ == base.path_ && this->query_ == base.query_ &&
this->fragment_ == base.fragment_) {
Expand Down
21 changes: 21 additions & 0 deletions test/uri/uri_relative_to_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ TEST(URI_relative_to, absolute_absolute_base_false_4) {
EXPECT_EQ(uri.recompose(), "https://bar.com");
}

TEST(URI_relative_to, absolute_absolute_base_false_different_ports) {
const sourcemeta::core::URI base{"http://localhost:8000"};
sourcemeta::core::URI uri{"http://localhost:9000/schemas/test.json"};
uri.relative_to(base);
EXPECT_EQ(uri.recompose(), "http://localhost:9000/schemas/test.json");
}

TEST(URI_relative_to, absolute_absolute_base_false_different_userinfo) {
const sourcemeta::core::URI base{"https://alice@example.com/foo"};
sourcemeta::core::URI uri{"https://bob@example.com/foo/bar"};
uri.relative_to(base);
EXPECT_EQ(uri.recompose(), "https://bob@example.com/foo/bar");
}

TEST(URI_relative_to, absolute_absolute_base_false_userinfo_vs_none) {
const sourcemeta::core::URI base{"https://example.com/foo"};
sourcemeta::core::URI uri{"https://alice@example.com/foo/bar"};
uri.relative_to(base);
EXPECT_EQ(uri.recompose(), "https://alice@example.com/foo/bar");
}

TEST(URI_relative_to, absolute_relative_1) {
const sourcemeta::core::URI base{"https://www.example.com"};
sourcemeta::core::URI uri{"foo"};
Expand Down