t32k / stylestats

StyleStats is a library to collect CSS statistics.
MIT License
1.83k stars 70 forks source link

URL parsing throws false negatives #91

Closed kaelig closed 9 years ago

kaelig commented 9 years ago

I used the Regex found in Stylestats in a project of mine (to detect if a JavaScript file was passed as a URL), but it did not accept my url https://cdn.polyfill.io/v1/polyfill.min.js?ua=firefox/30.

The regex can be found in stylestats.js: https://github.com/t32k/stylestats/blob/master/lib/stylestats.js#L29

var URL = /^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|cat|coop|int|pro|tel|xxx|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2})?)|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |\/.,*:;=]|%[a-f\d]{2})*)?$/;

I can recommend the use of the valid-url package which worked well for me.

kamranayub commented 9 years ago

Why is there even parsing? Issue a HEAD, request OK, sounds good; fallback to GET if HEAD is blocked. I tried pasting in a link and it's failing. Not a good first impression :(

edit: There might be an issue with the CSS file I put in, there might be a parsing error--but I can't tell since there's no error message or feedback about what's going wrong. Another one worked fine.