Skip to content

Url::join embeds ?query and #fragment into path instead of parsing them per RFC 3986 #2082

Backlog

Description

@blindlabs-founder

Summary

payjoin::Url::join (payjoin/src/core/url.rs:309-353) documents RFC 3986 reference resolution, but both the leading-/ branch (lines 320-324) and the relative branch (lines 325-349) place the entire segment directly into new_url.path and clear new_url.query = None and new_url.fragment = None without splitting query/fragment components via parse_path_query_fragment.

As a result:

  1. base.join("/c?v=2#frag") sets path() == "/c?v=2#frag", query() == None, and fragment() == None.
  2. Calling query_pairs_mut().append_pair("p", "1") on that result appends ?p=1 after #frag, producing malformed "https://example.com/c?v=2#frag?p=1" and breaking round-trip Url::parse invariants.
  3. base.join("c?v=2#frag") sets path() == "/a/c?v=2#frag", query() == None, fragment() == None.
  4. base.join("?v=2") against https://example.com/a/b drops b and sets path() == "/a/?v=2", query() == None (RFC 3986 Section 5.2.2 keeps the base path /a/b and sets query = Some("v=2")).

Minimal Reproducer (commit ec98fea8440edef83666e0df250bee321f4f410e)

Add as payjoin/tests/repro_url_join.rs:

use payjoin::Url;

#[test]
fn repro_url_join_query_and_fragment() {
    let base = Url::parse("https://example.com/a/b").unwrap();
    let mut abs = base.join("/c?v=2#frag").unwrap();
    assert_eq!(abs.path(), "/c"); // actual: "/c?v=2#frag"
    assert_eq!(abs.query(), Some("v=2")); // actual: None
    assert_eq!(abs.fragment(), Some("frag")); // actual: None
    abs.query_pairs_mut().append_pair("p", "1");
    assert_eq!(abs.as_str(), "https://example.com/c?v=2&p=1#frag"); // actual: ".../c?v=2#frag?p=1"
    let rel = base.join("c?v=2#frag").unwrap();
    assert_eq!((rel.path(), rel.query(), rel.fragment()), ("/a/c", Some("v=2"), Some("frag")));
    let q_only = base.join("?v=2").unwrap();
    assert_eq!((q_only.path(), q_only.query()), ("/a/b", Some("v=2")));
}

Suggested Fix

Call the existing parse_path_query_fragment(segment) helper (url.rs:540) in Url::join after the scheme check:

  • If the parsed path is empty: preserve new_url.path, replace new_url.query only when query.is_some(), and set new_url.fragment = fragment.
  • Otherwise, resolve the parsed path (leading / or relative merge + dot-segment removal) and assign new_url.query = query; new_url.fragment = fragment;.

Activity

  1. CaptainLevi2k04 commented on Nov 26, 2026

    @CaptainLevi2k04

    Hi! I'd like to take this issue on.

    I went through the reproducer and the suggested fix. My understanding is that the scope is to use the existing parse_path_query_fragment helper in Url::join, correctly resolve the parsed path, and preserve or update the base query and fragment according to RFC 3986, especially for query-only and fragment-only references.

    Could one of the maintainers confirm that this is the intended scope before I start implementing it? Please let me know if you'd suggest any changes.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions