Skip to content

Commit 319851b

Browse files
committed
[fix] Remove CR, HT, and LF
Copy the behavior of browser `URL` interface and remove CR, HT, and LF from the input URL.
1 parent 4e53a8c commit 319851b

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

index.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var required = require('requires-port')
44
, qs = require('querystringify')
5+
, CRHTLF = /[\n\r\t]/g
56
, slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//
67
, protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i
78
, windowsDriveLetter = /^[a-zA-Z]:/
@@ -135,6 +136,7 @@ function isSpecial(scheme) {
135136
*/
136137
function extractProtocol(address, location) {
137138
address = trimLeft(address);
139+
address = address.replace(CRHTLF, '');
138140
location = location || {};
139141

140142
var match = protocolre.exec(address);
@@ -235,6 +237,7 @@ function resolve(relative, base) {
235237
*/
236238
function Url(address, location, parser) {
237239
address = trimLeft(address);
240+
address = address.replace(CRHTLF, '');
238241

239242
if (!(this instanceof Url)) {
240243
return new Url(address, location, parser);

test/test.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe('url-parse', function () {
102102
});
103103

104104
it('does not truncate the input string', function () {
105-
var input = 'foo\nbar\rbaz\u2028qux\u2029';
105+
var input = 'foo\x0bbar\x0cbaz\u2028qux\u2029';
106106

107107
assume(parse.extractProtocol(input)).eql({
108108
slashes: false,
@@ -113,7 +113,16 @@ describe('url-parse', function () {
113113
});
114114

115115
it('trimsLeft', function () {
116-
assume(parse.extractProtocol(' javascript://foo')).eql({
116+
assume(parse.extractProtocol('\x0b\x0c javascript://foo')).eql({
117+
slashes: true,
118+
protocol: 'javascript:',
119+
rest: 'foo',
120+
slashesCount: 2
121+
});
122+
});
123+
124+
it('removes CR, HT, and LF', function () {
125+
assume(parse.extractProtocol('jav\n\rasc\nript\r:/\t/fo\no')).eql({
117126
slashes: true,
118127
protocol: 'javascript:',
119128
rest: 'foo',
@@ -408,6 +417,31 @@ describe('url-parse', function () {
408417
assume(parsed.href).equals('//example.com');
409418
});
410419

420+
it('removes CR, HT, and LF', function () {
421+
var parsed = parse(
422+
'ht\ntp://a\rb:\tcd@exam\rple.com:80\t80/pat\thname?fo\no=b\rar#ba\tz'
423+
);
424+
425+
assume(parsed.protocol).equals('http:');
426+
assume(parsed.username).equals('ab');
427+
assume(parsed.password).equals('cd');
428+
assume(parsed.host).equals('example.com:8080');
429+
assume(parsed.hostname).equals('example.com');
430+
assume(parsed.port).equals('8080');
431+
assume(parsed.pathname).equals('/pathname');
432+
assume(parsed.query).equals('?foo=bar');
433+
assume(parsed.hash).equals('#baz');
434+
assume(parsed.href).equals(
435+
'http://ab:cd@example.com:8080/pathname?foo=bar#baz'
436+
);
437+
438+
parsed = parse('s\nip:al\rice@atl\tanta.com');
439+
440+
assume(parsed.protocol).equals('sip:');
441+
assume(parsed.pathname).equals('alice@atlanta.com');
442+
assume(parsed.href).equals('sip:alice@atlanta.com');
443+
});
444+
411445
describe('origin', function () {
412446
it('generates an origin property', function () {
413447
var url = 'http://google.com:80/pathname'

0 commit comments

Comments
 (0)