Skip to content

Commit 5b5b7d9

Browse files
committed
feat: Set nosniff for JSONP responses (#38)
1 parent fe01f3e commit 5b5b7d9

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/Response.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,16 @@ export default class Response {
476476
.replace(/\u2028/g, '\\u2028')
477477
.replace(/\u2029/g, '\\u2029');
478478

479+
// NOTE: The `/**/` is a security mitigation for "Rosetta Flash JSONP abuse", see
480+
// silvermine/lambda-express#38. The `typeof` is to prevent errors on the client
481+
// if the callback function doesn't exist, see expressjs/express#1773.
479482
this._body = `/**/ typeof ${callbackFunctionName} === 'function' && ${callbackFunctionName}(${stringified});`;
480-
return this.type('text/javascript; charset=utf-8').end();
483+
484+
return this.type('text/javascript; charset=utf-8')
485+
// `nosniff` is set to mitigate "Rosetta Flash JSONP abuse", see
486+
// silvermine/lambda-express#38
487+
.set('X-Content-Type-Options', 'nosniff')
488+
.end();
481489
}
482490

483491
return this.json(o);

tests/Response.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,8 @@ describe('Response', () => {
721721

722722
resp = new Response(app, new Request(app, evt, handlerContext()), cb);
723723
output.multiValueHeaders['Content-Type'] = [ 'text/javascript; charset=utf-8' ];
724+
// See silvermine/lambda-express#38
725+
output.multiValueHeaders['X-Content-Type-Options'] = [ 'nosniff' ];
724726

725727
if (extender) {
726728
extender(resp, output);

0 commit comments

Comments
 (0)