Skip to content

Commit 16c2d23

Browse files
committed
feat(connect): add nosniff
1 parent f1b061c commit 16c2d23

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

src/connect/connect.js

-24
This file was deleted.

src/connect/nosniff.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
function setHeaders(res, headers) {
3+
Object.keys(headers).forEach(key => {
4+
let value = headers[key];
5+
if (value !== null) {
6+
res.setHeader(key, value);
7+
}
8+
});
9+
}
10+
export default function () {
11+
return function (req, res, next) {
12+
setHeaders(res, {
13+
"X-Content-Type-Options": "nosniff"
14+
});
15+
next();
16+
}
17+
}

test/connect/hello-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33
import assert from "power-assert";
44
import connect from "connect"
5+
import nosniff from "../../src/connect/nosniff";
56
import hello from "../../src/connect/hello";
67
import http from "http";
78
import fetch from "node-fetch";
@@ -10,6 +11,7 @@ describe("hello", function () {
1011
var server;
1112
before(function (done) {
1213
var app = connect();
14+
app.use(nosniff());
1315
app.use(hello(responseText));
1416
server = http.createServer(app).listen(3000, done);
1517
});
@@ -23,4 +25,10 @@ describe("hello", function () {
2325
assert.equal(text, responseText);
2426
});
2527
});
28+
it("should return response has `X-Content-Type-Options` header", function () {
29+
return fetch("http://localhost:3000")
30+
.then(res => {
31+
assert.equal(res.headers.get("x-content-type-options"), "nosniff");
32+
})
33+
});
2634
});

0 commit comments

Comments
 (0)