File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ const TEST_NONCE =
2
+ '47ebaa88ef82ffb86e4ccb0eab1c5ec6bd76767642358e8cf99487673d5904b5'
3
+
4
+ const cspPolicies = [ `style-src 'self' 'unsafe-inline' 'nonce-${ TEST_NONCE } '` ]
5
+
6
+ module . exports = {
7
+ server : {
8
+ baseDir : './samples' ,
9
+ directory : true ,
10
+ routes : {
11
+ '/dist' : './dist' ,
12
+ '/src/assets' : './src/assets' ,
13
+ } ,
14
+ } ,
15
+ files : [
16
+ './samples/vanilla-js/csp' ,
17
+ './samples/react/csp' ,
18
+ './samples/vue/csp' ,
19
+ ] ,
20
+ startPath : '/' ,
21
+ middleware : [
22
+ function ( req , res , next ) {
23
+ const url = req . url
24
+ const allowedPrefixes = [
25
+ '/vanilla-js/csp' ,
26
+ '/react/csp' ,
27
+ '/vue/csp' ,
28
+ '/assets' ,
29
+ '/src' ,
30
+ '/dist' ,
31
+ ]
32
+
33
+ if ( url === '/' ) {
34
+ res . setHeader ( 'Content-Type' , 'text/html' )
35
+ res . end ( `
36
+ <h1>Content Security Policy Test Directories</h1>
37
+ <ul>
38
+ <li><a href="/vanilla-js/csp/">vanilla-js/csp/</a></li>
39
+ <li><a href="/react/csp/">react/csp/</a></li>
40
+ <li><a href="/vue/csp/">vue/csp/</a></li>
41
+ </ul>
42
+ ` )
43
+ return
44
+ }
45
+
46
+ const isAllowed = allowedPrefixes . some (
47
+ ( prefix ) => url . startsWith ( prefix + '/' ) || url === prefix
48
+ )
49
+
50
+ if ( isAllowed ) {
51
+ return next ( )
52
+ }
53
+
54
+ res . statusCode = 403
55
+ res . setHeader ( 'Content-Type' , 'text/plain' )
56
+ res . end ( 'Forbidden' )
57
+ } ,
58
+ function ( req , res , next ) {
59
+ res . setHeader ( 'Content-Security-Policy' , cspPolicies . join ( '; ' ) )
60
+ next ( )
61
+ } ,
62
+ ] ,
63
+ }
You can’t perform that action at this time.
0 commit comments