Skip to content

Commit

Permalink
Ignore RFC 3986 fragments in BIP 21 URIs
Browse files Browse the repository at this point in the history
Although behavior for when encountering RFC 3986 fragments in BIP 21
URIs is not specified, according to RFC 3986 it is unambiguously not
query data and therefore should be excluded from BIP 21 query
parameters.
  • Loading branch information
nothingmuch committed Dec 2, 2024
1 parent 0585806 commit 646de25
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ impl<'a, T: DeserializeParams<'a>> Uri<'a, bitcoin::address::NetworkUnchecked, T
let mut label = None;
let mut message = None;
if let Some(params) = params {
// [RFC 3986 § 3.4](https://www.rfc-editor.org/rfc/rfc3986#section-3.4):
//
// > The query component is indicated by the first question
// > mark ("?") character and terminated by a number sign ("#") character
// > or by the end of the URI.
let params = match params.find('#') {
Some(pos) => &params[..pos],
None => params,
};

for param in params.split('&') {
let pos = param
.find('=')
Expand Down
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,28 @@ mod tests {

assert_eq!(uri.to_string(), input);
}

#[test]
fn rfc3986_empty_fragment_not_defined_in_bip21() {
let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?label=foo#";
let uri = input.parse::<Uri<'_, _>>().unwrap().require_network(bitcoin::Network::Bitcoin).unwrap();
let label: Cow<'_, str> = uri.label.clone().unwrap().try_into().unwrap();
assert_eq!(uri.address.to_string(), "1andreas3batLhQa2FawWjeyjCqyBzypd");
assert_eq!(label, "foo");
assert!(uri.amount.is_none());
assert!(uri.message.is_none());
assert_eq!(uri.to_string(), "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?label=foo");
}

#[test]
fn rfc3986_non_empty_fragment_not_defined_in_bip21() {
let input = "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?label=foo#&message=not%20part%20of%20a%20message";
let uri = input.parse::<Uri<'_, _>>().unwrap().require_network(bitcoin::Network::Bitcoin).unwrap();
let label: Cow<'_, str> = uri.label.clone().unwrap().try_into().unwrap();
assert_eq!(uri.address.to_string(), "1andreas3batLhQa2FawWjeyjCqyBzypd");
assert_eq!(label, "foo");
assert!(uri.amount.is_none());
assert!(uri.message.is_none());
assert_eq!(uri.to_string(), "bitcoin:1andreas3batLhQa2FawWjeyjCqyBzypd?label=foo");
}
}
6 changes: 3 additions & 3 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ impl<'a, W: fmt::Write> fmt::Write for EqSignChecker<'a, W> {
/// > otherparam = qchar *qchar [ "=" *qchar ]
/// > ```
/// ...
/// > Here, "qchar" corresponds to valid characters of an RFC 3986 URI > query
/// component, excluding the "=" and "&" characters, which this BIP > takes as
/// separators.
/// > Here, "qchar" corresponds to valid characters of an RFC 3986 URI query
/// > component, excluding the "=" and "&" characters, which this BIP takes as
/// > separators.
///
/// [RFC 3986 Appendix A](https://www.rfc-editor.org/rfc/rfc3986#appendix-A):
///
Expand Down

0 comments on commit 646de25

Please sign in to comment.