Skip to content

Commit

Permalink
Merge pull request #659 from daniel-ac-martin/fix-zap-warnings
Browse files Browse the repository at this point in the history
Fix DAST warnings
  • Loading branch information
daniel-ac-martin authored Jan 23, 2023
2 parents 018c186 + 142f37d commit b6e0568
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .zap/rules.tsv
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# False positives
10027 IGNORE (Information Disclosure - Suspicious Comments)
10031 IGNORE (User Controllable HTML Element Attribute)
# We deliberately don't cache HTML as it may contain sensitive information
10049 IGNORE (Non-Storable Content)
# Netlify seems to randomly generate these
10096 IGNORE (Timestamp Disclosure)
# HSTS should be set by the proxy terminating TLS
Expand All @@ -11,11 +13,15 @@
10055 IGNORE (CSP: style-src unsafe-inline)
# We can't seem to run the Ajax spider
10109 INFO (Modern Web Application)
# These are not real forms, and we replicate GDS
10202 IGNORE (Absence of Anti-CSRF Tokens)
# We don't control the headers on Netlify's CDN
10021 OUTOFSCOPE .*/public/.*
10063 OUTOFSCOPE .*/public/.*
# These are not timestamps
10096 OUTOFSCOPE .*/public/.*\.css
# These are HTML errors
10202 OUTOFSCOPE .*/public/.*\.bundle\.js
# We don't control the vendors code
10110 OUTOFSCOPE .*/public/js/vendors\..*\.bundle\.js
90022 OUTOFSCOPE .*/public/js/vendors\..*\.bundle\.js
2 changes: 2 additions & 0 deletions lib/restify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import restifyBunyanLogger from 'restify-bunyan-logger';
import stoppable from 'stoppable';
import { liveness } from './middleware/health-check';
import { htmlByDefault } from './middleware/html-by-default';
import { permissionsPolicy } from './middleware/permissions-policy';
import { preventClickjacking } from './middleware/prevent-clickjacking';
import { preventMimeSniffing } from './middleware/prevent-mime-sniffing';
import { noCacheByDefault } from './middleware/no-cache-by-default';
Expand Down Expand Up @@ -92,6 +93,7 @@ export const createServer = (options: ServerOptions ) => {

httpd.on('after', restifyBunyanLogger());

httpd.pre(permissionsPolicy);
httpd.pre(preventClickjacking);
httpd.pre(preventMimeSniffing);
httpd.pre(noCacheByDefault);
Expand Down
78 changes: 78 additions & 0 deletions lib/restify/src/middleware/permissions-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Middleware } from './common';

const keywords = [
'self',
'src'
];

const ppObj = {
'accelerometer': 'self',
'ambient-light-sensor': 'self',
'autoplay': 'self',
'battery': 'self',
'camera': 'self',
'display-capture': 'self',
'document-domain': 'self',
'encrypted-media ': 'self',
'execution-while-not-rendered': 'self',
'execution-while-out-of-viewport': 'self',
'fullscreen': 'self',
'gamepad': 'self',
'geolocation': 'self',
'gyroscope': 'self',
'hid': 'self',
'idle-detection': 'self',
'local-fonts': 'self',
'magnetometer': 'self',
'microphone': 'self',
'midi': 'self',
'payment': 'self',
'picture-in-picture': 'self',
'publickey-credentials-get': 'self',
'screen-wake-lock': 'self',
'serial': 'self',
'speaker-selection': 'self',
'usb': 'self',
'web-share': 'self',
'xr-spatial-tracking': 'self'
};

const isDefined = (v: any): boolean => (
v !== undefined
);

const policy = Object.keys(ppObj)
.map(directive => {
const _valueArr = ppObj[directive];
const valueArr = (
Array.isArray(_valueArr)
? _valueArr
: [ _valueArr ]
);
const values = (
valueArr
.filter(isDefined)
.map(v => (
keywords.includes(v)
? v
: `"${v}"`
) )
.join(' ')
);

return (
values === ''
? undefined
: `${directive}=(${values})`
);
} )
.filter(isDefined)
.join(', ');

export const permissionsPolicy: Middleware = (_req, res, next) => {
res.header('Permissions-Policy', policy);

next();
};

export default permissionsPolicy;

1 comment on commit b6e0568

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉 Published on https://not-gov.uk as production
🚀 Deployed on https://63cec388f0771a6ff507a2cc--notgovuk.netlify.app

Please sign in to comment.