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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## UNRELEASED (YYYY-MM-DD)

### Breaking changes

- `MagnetLink` now refuses to parse strings that contain a newline (`\n`), producing
a `MagnetLinkError::InvalidURINewLine` error

## Version 0.3.2 (2025-08-29)

### Added
Expand Down
21 changes: 21 additions & 0 deletions src/magnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{InfoHash, InfoHashError, TorrentID};
pub enum MagnetLinkError {
/// The URI was not valid according to [`Url::parse`](url::Url::parse).
InvalidURI { source: url::ParseError },
/// The URI contains a newline
InvalidURINewLine,
/// The URI scheme was not `magnet`
InvalidScheme { scheme: String },
/// No Bittorrent v1/v2 hash was found in the magnet URI
Expand All @@ -29,6 +31,9 @@ impl std::fmt::Display for MagnetLinkError {
MagnetLinkError::InvalidURI { source } => {
write!(f, "Invalid URI: {source}")
}
MagnetLinkError::InvalidURINewLine => {
write!(f, "Invalid URI: newlines are not allowed in magnet links")
}
MagnetLinkError::InvalidScheme { scheme } => {
write!(f, "Invalid URI scheme: {scheme}")
}
Expand Down Expand Up @@ -87,6 +92,11 @@ impl MagnetLink {
/// Generates a new MagnetLink from a string. Will fail if the string is not a valid URL, and
/// in the conditions defined in [`MagnetLink::from_url`](crate::magnet::MagnetLink::from_url).
pub fn new(s: &str) -> Result<MagnetLink, MagnetLinkError> {
// The error returned by Uri::parse when there is a newline is not very obvious, so we
// sacrifice performance to save neurons from fellow developers.
if s.contains('\n') {
return Err(MagnetLinkError::InvalidURINewLine);
}
let u = Url::parse(s)?;
MagnetLink::from_url(&u)
}
Expand Down Expand Up @@ -329,4 +339,15 @@ mod tests {
}
);
}

#[test]
fn fails_newline_in_magnet() {
let mut magnet_url = std::fs::read_to_string("tests/bittorrent-v2-test.magnet").unwrap();
magnet_url.push('\n');

let res = MagnetLink::new(&magnet_url);
assert!(res.is_err());

assert_eq!(res.unwrap_err(), MagnetLinkError::InvalidURINewLine,);
}
}
2 changes: 1 addition & 1 deletion tests/bittorrent-v1-emma-goldman.magnet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
magnet:?xt=urn:btih:C811B41641A09D192B8ED81B14064FFF55D85CE3&dn=Emma+Goldman+-+Essential+Works+of+Anarchism+%2816+books%29&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce
magnet:?xt=urn:btih:C811B41641A09D192B8ED81B14064FFF55D85CE3&dn=Emma+Goldman+-+Essential+Works+of+Anarchism+%2816+books%29&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=http%3A%2F%2Ftracker.openbittorrent.com%3A80%2Fannounce&tr=udp%3A%2F%2Fopentracker.i2p.rocks%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce
2 changes: 1 addition & 1 deletion tests/bittorrent-v2-hybrid-test.magnet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
magnet:?xt=urn:btih:631a31dd0a46257d5078c0dee4e66e26f73e42ac&xt=urn:btmh:1220d8dd32ac93357c368556af3ac1d95c9d76bd0dff6fa9833ecdac3d53134efabb&dn=bittorrent-v1-v2-hybrid-test
magnet:?xt=urn:btih:631a31dd0a46257d5078c0dee4e66e26f73e42ac&xt=urn:btmh:1220d8dd32ac93357c368556af3ac1d95c9d76bd0dff6fa9833ecdac3d53134efabb&dn=bittorrent-v1-v2-hybrid-test
2 changes: 1 addition & 1 deletion tests/bittorrent-v2-test.magnet
Original file line number Diff line number Diff line change
@@ -1 +1 @@
magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e&dn=bittorrent-v2-test
magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e&dn=bittorrent-v2-test