Skip to content

Commit

Permalink
feat(mc-html-template): make transport security configurable (#2244)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdeekens authored Jun 4, 2021
1 parent dc99469 commit 118efed
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .changeset/wild-trees-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@commercetools-frontend/application-config": patch
"@commercetools-frontend/mc-html-template": patch
---

Allow configuration of `Strict-Transport-Security` header through custom application config.

Similar to the `Feature-Policies` header use the `strictTransportSecurity` property of the custom application config to add to the defaults.

```js
headers: {
strictTransportSecurity: ['includeSubDomains']
}
```
11 changes: 11 additions & 0 deletions packages/application-config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"type": "string"
},
"uniqueItems": true
},
"hstsDirective": {
"type": "array",
"items": {
"enum": ["includeSubDomains", "preload"]
},
"uniqueItems": true
}
},
"properties": {
Expand Down Expand Up @@ -148,6 +155,10 @@
"permissionsPolicies": {
"description": "Configuration for the HTTP Permissions-Policy header (https://github.com/w3c/webappsec-permissions-policy/blob/main/permissions-policy-explainer.md)",
"type": "object"
},
"strictTransportSecurity": {
"description": "Additional configuration for the HTTP Strict-Transport-Security header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security)",
"$ref": "#/definitions/hstsDirective"
}
},
"additionalProperties": false,
Expand Down
4 changes: 4 additions & 0 deletions packages/application-config/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,9 @@ export interface JSONSchemaForCustomApplicationConfigurationFiles {
permissionsPolicies?: {
[k: string]: unknown;
};
/**
* Additional configuration for the HTTP Strict-Transport-Security header (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security)
*/
strictTransportSecurity?: ('includeSubDomains' | 'preload')[];
};
}
3 changes: 2 additions & 1 deletion packages/application-config/test/fixtures/config-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"permissionsPolicies": {
"microphone": "()"
}
},
"strictTransportSecurity": ["includeSubDomains"]
}
}
4 changes: 4 additions & 0 deletions packages/application-config/test/process-config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('processing a full config', () => {
permissionsPolicies: {
microphone: '()',
},
strictTransportSecurity: ['includeSubDomains'],
},
});
});
Expand Down Expand Up @@ -235,6 +236,7 @@ describe('processing a full config', () => {
permissionsPolicies: {
microphone: '()',
},
strictTransportSecurity: ['includeSubDomains'],
},
});
});
Expand Down Expand Up @@ -278,6 +280,7 @@ describe('processing a full config', () => {
permissionsPolicies: {
microphone: '()',
},
strictTransportSecurity: ['includeSubDomains'],
},
});
});
Expand Down Expand Up @@ -326,6 +329,7 @@ describe('processing a full config', () => {
permissionsPolicies: {
microphone: '()',
},
strictTransportSecurity: ['includeSubDomains'],
},
});
});
Expand Down
4 changes: 3 additions & 1 deletion packages/mc-html-template/src/process-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ const processHeaders = (applicationConfig) => {
);

return {
'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload',
'Strict-Transport-Security': ['max-age=31536000']
.concat(applicationConfig.headers.strictTransportSecurity || [])
.join('; '),
'X-XSS-Protection': '1; mode=block',
'X-Content-Type-Options': 'nosniff',
'X-Frame-Options': 'DENY',
Expand Down
36 changes: 36 additions & 0 deletions packages/mc-html-template/src/process-headers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,39 @@ describe('structured header string', () => {
});
});
});

describe('with strict transport security', () => {
describe('when value is a string', () => {
it('should convert to a header string', () => {
const testApplicationConfig = {
...defaultApplicationConfig,
headers: {
strictTransportSecurity: ['includeSubDomains'],
},
};

const processedApplicationConfig = processConfig(testApplicationConfig);

expect(
processedApplicationConfig['Strict-Transport-Security']
).toMatchInlineSnapshot(`"max-age=31536000; includeSubDomains"`);
});
});
});

describe('without strict transport security', () => {
describe('when value is a string', () => {
it('should convert to a header string', () => {
const testApplicationConfig = {
...defaultApplicationConfig,
headers: {},
};

const processedApplicationConfig = processConfig(testApplicationConfig);

expect(
processedApplicationConfig['Strict-Transport-Security']
).toMatchInlineSnapshot(`"max-age=31536000"`);
});
});
});

1 comment on commit 118efed

@vercel
Copy link

@vercel vercel bot commented on 118efed Jun 4, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.