fix: sanitize invalid custom protocol headers (#18927)

* fix: sanitize invalid custom protocol headers (#18854)

* lint fix
This commit is contained in:
Micha Hanselmann
2019-06-21 15:00:05 -07:00
committed by Shelley Vohr
parent 3e033b4f27
commit a603a4dde8
2 changed files with 33 additions and 0 deletions

View File

@@ -38,6 +38,20 @@ void BeforeStartInUI(base::WeakPtr<URLRequestAsyncAsarJob> job,
error = net::ERR_NOT_IMPLEMENTED;
}
// sanitize custom headers
if (request_options && request_options->is_dict()) {
const base::Value* headersDict = request_options->FindDictKey("headers");
if (headersDict) {
for (const auto& iter : headersDict->DictItems()) {
if (!iter.second.is_string()) {
args->ThrowError("Value of '" + iter.first +
"' header has to be a string");
return;
}
}
}
}
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&URLRequestAsyncAsarJob::StartAsync, job,

View File

@@ -342,6 +342,25 @@ describe('protocol module', () => {
})
})
it('throws an error when custom headers are invalid', (done) => {
const handler = (request, callback) => {
assert.throws(() => callback({
path: filePath,
headers: { 'X-Great-Header': 42 }
}), /Value of 'X-Great-Header' header has to be a string/)
done()
}
protocol.registerFileProtocol(protocolName, handler, (error) => {
if (error) return done(error)
$.ajax({
url: protocolName + '://fake-host',
cache: false,
success: () => done('request succeeded but it should not'),
error: (xhr, errorType, error) => done(error)
})
})
})
it('sends object as response', (done) => {
const handler = (request, callback) => callback({ path: filePath })
protocol.registerFileProtocol(protocolName, handler, (error) => {