Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent modification of invalid Content-Disposition header to avoid potential parsing errors. #42026

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions shell/browser/api/electron_api_web_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ v8::Local<v8::Value> HttpResponseHeadersToV8(
if (base::EqualsCaseInsensitiveASCII("Content-Disposition", key) &&
!value.empty()) {
net::HttpContentDisposition header(value, std::string());
std::string decodedFilename =
header.is_attachment() ? " attachment" : " inline";
// The filename must be encased in double quotes for serialization
// to happen correctly.
std::string filename = "\"" + header.filename() + "\"";
value = decodedFilename + "; filename=" + filename;
if (!header.filename().empty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better to check header.parse_result_flags()?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gdavidkov would you still like to move this forward? Looks like this is the last thing blocking merge :)

std::string decodedFilename =
header.is_attachment() ? " attachment" : " inline";
// The filename must be encased in double quotes for serialization
// to happen correctly.
std::string filename = "\"" + header.filename() + "\"";
value = decodedFilename + "; filename=" + filename;
}
}
response_headers.EnsureList(key)->Append(value);
}
Expand Down
18 changes: 18 additions & 0 deletions spec/api-web-request-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ describe('webRequest module', () => {
]);
const content = req.url;
res.end(content);
} else if (req.url === '/contentDisposition-invalid') {
res.writeHead(200, [
'content-disposition',
Buffer.from('attachment; filename*=UTF-8"test.json"').toString('binary')
]);
const content = req.url;
res.end(content);
} else {
res.setHeader('Custom', ['Header']);
let content = req.url;
Expand Down Expand Up @@ -483,6 +490,17 @@ describe('webRequest module', () => {
expect(data).to.equal('/contentDisposition');
});

it('does not change content-disposition header when it is invalid', async () => {
ses.webRequest.onHeadersReceived((details, callback) => {
expect(details.responseHeaders!['content-disposition']).to.deep.equal([' attachment; filename*=UTF-8"test.json"']);
callback({});
});
const { data, headers } = await ajax(defaultURL + 'contentDisposition-invalid');
const disposition = Buffer.from('attachment; filename*=UTF-8"test.json"').toString('binary');
expect(headers).to.to.have.property('content-disposition', disposition);
expect(data).to.equal('/contentDisposition-invalid');
});

it('follows server redirect', async () => {
ses.webRequest.onHeadersReceived((details, callback) => {
const responseHeaders = details.responseHeaders;
Expand Down