diff --git a/src/lib/isMagnetURI.js b/src/lib/isMagnetURI.js index 45b5c8ebf..e00ee3c32 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 = /(?:^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); - 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 0c513ed77..50c646f18 100644 --- a/test/validators.js +++ b/test/validators.js @@ -10256,6 +10256,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', @@ -10268,6 +10270,8 @@ describe('Validators', () => { 'magnet:?xt:urn:nonexisting:ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678901234', '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 */