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] CSP Middleware: Use native res.getHeader/setHeader methods #312

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/middleware/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const HEADER_CONTENT_SECURITY_POLICY_REPORT_ONLY = "Content-Security-Policy-Repo
const rPolicy = /^([-_a-zA-Z0-9]+)(:report-only|:ro)?$/i;

function addHeader(res, header, value) {
const current = res.get(header);
const current = res.getHeader(header);
if ( current == null ) {
res.set(header, value);
res.setHeader(header, value);
} else if ( Array.isArray(current) ) {
res.set(header, [...current, value]);
res.setHeader(header, [...current, value]);
} else {
res.set(header, [current, value]);
res.setHeader(header, [current, value]);
}
}

Expand Down
16 changes: 8 additions & 8 deletions test/lib/server/middleware/csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ test("Default Settings", (t) => {
t.plan(3 + 7); // fourth request should end in middleware and not call next!
const middleware = cspMiddleware("sap-ui-xx-csp-policy", {});
const res = {
get: function() {
getHeader: function() {
return undefined;
},
set: function(header, value) {
setHeader: function(header, value) {
t.fail(`should not be called with header ${header} and value ${value}`);
}
};
Expand Down Expand Up @@ -54,10 +54,10 @@ test("Custom Settings", (t) => {
});
let expected;
const res = {
get: function() {
getHeader: function() {
return undefined;
},
set: function(header, value) {
setHeader: function(header, value) {
if ( header.toLowerCase() === "content-security-policy" ) {
t.is(value, expected.shift(), "should have the expected value");
} else {
Expand Down Expand Up @@ -92,10 +92,10 @@ test("No Dynamic Policy Definition", (t) => {
allowDynamicPolicyDefinition: false
});
const res = {
get: function() {
getHeader: function() {
return undefined;
},
set: function(header, value) {
setHeader: function(header, value) {
if ( header.toLowerCase() === "content-security-policy" ) {
t.is(value, expected.shift(), "should have the expected value");
} else {
Expand Down Expand Up @@ -124,10 +124,10 @@ test("Header Manipulation", (t) => {
});
let cspHeader = "default-src: spdy:";
const res = {
get: function() {
getHeader: function() {
return cspHeader;
},
set: function(header, value) {
setHeader: function(header, value) {
if ( header.toLowerCase() === "content-security-policy" ) {
cspHeader = value;
} else {
Expand Down