From c08abad6d19a8b6a6e213c0178347bcc5bc42941 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Fri, 24 Jun 2022 23:39:52 +0800 Subject: [PATCH 1/3] feat(isMagnetURI): support Bittorrent v2 --- src/lib/isMagnetURI.js | 9 +++++++-- test/validators.js | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/isMagnetURI.js b/src/lib/isMagnetURI.js index 45b5c8ebf..dc75491d8 100644 --- a/src/lib/isMagnetURI.js +++ b/src/lib/isMagnetURI.js @@ -1,8 +1,13 @@ import assertString from './util/assertString'; -const magnetURI = /^magnet:\?xt(?:\.1)?=urn:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?($|&)/i; +const magnetURIComponent = /xt(?:\.1)?=urn:((?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})($|&)/i; export default function isMagnetURI(url) { assertString(url); - return magnetURI.test(url.trim()); + + if (url.indexOf('magnet:?') !== 0) { + return false; + } + + return magnetURIComponent.test(url); } diff --git a/test/validators.js b/test/validators.js index 16fb6b89b..0fecc1c4f 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10230,6 +10230,8 @@ describe('Validators', () => { 'magnet:?xt=urn:md5:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', 'magnet:?xt=urn:tree:tiger:ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', 'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', + 'magnet:?tr=udp://helloworld:1337/announce&xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', + 'magnet:?xt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', ], invalid: [ ':?xt=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', @@ -10242,6 +10244,7 @@ describe('Validators', () => { 'magnet:?xt:urn:nonexisting:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', 'magnet:?xt.2=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', 'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234567890123456789ABCD', + 'magnet:?xt=urn:btmh:1120caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', ], }); /* eslint-enable max-len */ From 369a79c53b7fb6b76f5fc239dfba9aa4364f998a Mon Sep 17 00:00:00 2001 From: Rhilip Date: Sat, 25 Jun 2022 09:19:58 +0800 Subject: [PATCH 2/3] fix(isMagnetURI): make sure `xt=` is uri component --- src/lib/isMagnetURI.js | 2 +- test/validators.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/isMagnetURI.js b/src/lib/isMagnetURI.js index dc75491d8..be1f5803e 100644 --- a/src/lib/isMagnetURI.js +++ b/src/lib/isMagnetURI.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const magnetURIComponent = /xt(?:\.1)?=urn:((?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})($|&)/i; +const magnetURIComponent = /[?&]xt(?:\.1)?=urn:((?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})($|&)/i; export default function isMagnetURI(url) { assertString(url); diff --git a/test/validators.js b/test/validators.js index 0fecc1c4f..8056f04f6 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10245,6 +10245,7 @@ describe('Validators', () => { 'magnet:?xt.2=urn:btih:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', 'magnet:?xt=urn:ed2k:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234567890123456789ABCD', 'magnet:?xt=urn:btmh:1120caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', + 'magnet:?ttxt=urn:btmh:1220caf1e1c30e81cb361b9ee167c4aa64228a7fa4fa9f6105232b28ad099f3a302e', ], }); /* eslint-enable max-len */ From 6c94aae3c164db5c0be56906010a5e867fe59f88 Mon Sep 17 00:00:00 2001 From: Rhilip Date: Wed, 29 Jun 2022 14:34:21 +0800 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Sarhan Aissi --- src/lib/isMagnetURI.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isMagnetURI.js b/src/lib/isMagnetURI.js index be1f5803e..e00ee3c32 100644 --- a/src/lib/isMagnetURI.js +++ b/src/lib/isMagnetURI.js @@ -1,6 +1,6 @@ import assertString from './util/assertString'; -const magnetURIComponent = /[?&]xt(?:\.1)?=urn:((?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})($|&)/i; +const magnetURIComponent = /(?:^magnet:\?|[^?&]&)xt(?:\.1)?=urn:(?:(?:aich|bitprint|btih|ed2k|ed2khash|kzhash|md5|sha1|tree:tiger):[a-z0-9]{32}(?:[a-z0-9]{8})?|btmh:1220[a-z0-9]{64})(?:$|&)/i; export default function isMagnetURI(url) { assertString(url);