url: return valid file: urls fom url.format()
`file:` URLs that do not start with `file://` are invalid. Browsers convert `file:/etc/passwd` to `file:///etc/passwd`. This is also what the docs indicate we are doing, but we're not. PR-URL: https://github.com/nodejs/node/pull/7234 Fixes: https://github.com/nodejs/node/issues/3361 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Brian White <mscdex@mscdex.net>
This commit is contained in:
parent
874839c6dc
commit
336b027411
21
lib/url.js
21
lib/url.js
@ -553,7 +553,7 @@ Url.prototype.format = function() {
|
|||||||
var protocol = this.protocol || '';
|
var protocol = this.protocol || '';
|
||||||
var pathname = this.pathname || '';
|
var pathname = this.pathname || '';
|
||||||
var hash = this.hash || '';
|
var hash = this.hash || '';
|
||||||
var host = false;
|
var host = '';
|
||||||
var query = '';
|
var query = '';
|
||||||
|
|
||||||
if (this.host) {
|
if (this.host) {
|
||||||
@ -602,13 +602,18 @@ Url.prototype.format = function() {
|
|||||||
|
|
||||||
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
|
// only the slashedProtocols get the //. Not mailto:, xmpp:, etc.
|
||||||
// unless they had them to begin with.
|
// unless they had them to begin with.
|
||||||
if (this.slashes ||
|
if (this.slashes || slashedProtocol[protocol]) {
|
||||||
(!protocol || slashedProtocol[protocol]) && host !== false) {
|
if (this.slashes || host) {
|
||||||
host = '//' + (host || '');
|
if (pathname && pathname.charCodeAt(0) !== 47/*/*/)
|
||||||
if (pathname && pathname.charCodeAt(0) !== 47/*/*/)
|
pathname = '/' + pathname;
|
||||||
pathname = '/' + pathname;
|
host = '//' + host;
|
||||||
} else if (!host) {
|
} else if (protocol.length >= 4 &&
|
||||||
host = '';
|
protocol.charCodeAt(0) === 102/*f*/ &&
|
||||||
|
protocol.charCodeAt(1) === 105/*i*/ &&
|
||||||
|
protocol.charCodeAt(2) === 108/*l*/ &&
|
||||||
|
protocol.charCodeAt(3) === 101/*e*/) {
|
||||||
|
host = '//';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
search = search.replace('#', '%23');
|
search = search.replace('#', '%23');
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
require('../common');
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const inspect = require('util').inspect;
|
||||||
|
|
||||||
var url = require('url');
|
const url = require('url');
|
||||||
|
|
||||||
// URLs to parse, and expected data
|
// URLs to parse, and expected data
|
||||||
// { url : parsed }
|
// { url : parsed }
|
||||||
@ -881,8 +882,16 @@ for (const u in parseTests) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.deepStrictEqual(actual, expected);
|
assert.deepStrictEqual(
|
||||||
assert.deepStrictEqual(spaced, expected);
|
actual,
|
||||||
|
expected,
|
||||||
|
`expected ${inspect(expected)}, got ${inspect(actual)}`
|
||||||
|
);
|
||||||
|
assert.deepStrictEqual(
|
||||||
|
spaced,
|
||||||
|
expected,
|
||||||
|
`expected ${inspect(expected)}, got ${inspect(spaced)}`
|
||||||
|
);
|
||||||
|
|
||||||
expected = parseTests[u].href;
|
expected = parseTests[u].href;
|
||||||
actual = url.format(parseTests[u]);
|
actual = url.format(parseTests[u]);
|
||||||
@ -1165,6 +1174,14 @@ var formatTests = {
|
|||||||
hash: '#frag',
|
hash: '#frag',
|
||||||
search: '?abc=the#1?&foo=bar',
|
search: '?abc=the#1?&foo=bar',
|
||||||
pathname: '/fooA100%mBr',
|
pathname: '/fooA100%mBr',
|
||||||
|
},
|
||||||
|
|
||||||
|
// https://github.com/nodejs/node/issues/3361
|
||||||
|
'file:///home/user': {
|
||||||
|
href: 'file:///home/user',
|
||||||
|
protocol: 'file',
|
||||||
|
pathname: '/home/user',
|
||||||
|
path: '/home/user'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for (const u in formatTests) {
|
for (const u in formatTests) {
|
||||||
@ -1172,13 +1189,13 @@ for (const u in formatTests) {
|
|||||||
delete formatTests[u].href;
|
delete formatTests[u].href;
|
||||||
const actual = url.format(u);
|
const actual = url.format(u);
|
||||||
const actualObj = url.format(formatTests[u]);
|
const actualObj = url.format(formatTests[u]);
|
||||||
assert.equal(actual, expect,
|
assert.strictEqual(actual, expect,
|
||||||
'wonky format(' + u + ') == ' + expect +
|
'wonky format(' + u + ') == ' + expect +
|
||||||
'\nactual:' + actual);
|
'\nactual:' + actual);
|
||||||
assert.equal(actualObj, expect,
|
assert.strictEqual(actualObj, expect,
|
||||||
'wonky format(' + JSON.stringify(formatTests[u]) +
|
'wonky format(' + JSON.stringify(formatTests[u]) +
|
||||||
') == ' + expect +
|
') == ' + expect +
|
||||||
'\nactual: ' + actualObj);
|
'\nactual: ' + actualObj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1556,7 +1573,7 @@ var relativeTests2 = [
|
|||||||
];
|
];
|
||||||
relativeTests2.forEach(function(relativeTest) {
|
relativeTests2.forEach(function(relativeTest) {
|
||||||
const a = url.resolve(relativeTest[1], relativeTest[0]);
|
const a = url.resolve(relativeTest[1], relativeTest[0]);
|
||||||
const e = relativeTest[2];
|
const e = url.format(relativeTest[2]);
|
||||||
assert.equal(a, e,
|
assert.equal(a, e,
|
||||||
'resolve(' + [relativeTest[1], relativeTest[0]] + ') == ' + e +
|
'resolve(' + [relativeTest[1], relativeTest[0]] + ') == ' + e +
|
||||||
'\n actual=' + a);
|
'\n actual=' + a);
|
||||||
@ -1598,9 +1615,13 @@ relativeTests2.forEach(function(relativeTest) {
|
|||||||
var actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]);
|
var actual = url.resolveObject(url.parse(relativeTest[1]), relativeTest[0]);
|
||||||
var expected = url.parse(relativeTest[2]);
|
var expected = url.parse(relativeTest[2]);
|
||||||
|
|
||||||
assert.deepStrictEqual(actual, expected);
|
assert.deepStrictEqual(
|
||||||
|
actual,
|
||||||
|
expected,
|
||||||
|
`expected ${inspect(expected)} but got ${inspect(actual)}`
|
||||||
|
);
|
||||||
|
|
||||||
expected = relativeTest[2];
|
expected = url.format(relativeTest[2]);
|
||||||
actual = url.format(actual);
|
actual = url.format(actual);
|
||||||
|
|
||||||
assert.equal(actual, expected,
|
assert.equal(actual, expected,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user