-
Notifications
You must be signed in to change notification settings - Fork 1
Create automation for easier to read CSP diffs #1210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,6 @@ resources/ | |
|
|
||
| # Precious install | ||
| local/ | ||
|
|
||
| # Cloudflare Pages headers | ||
| static/_headers | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| v22 | ||
| v22.6.0 |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /** | ||
| * HTTP Headers Configuration for Cloudflare Pages | ||
| * This file is the source of truth for static/_headers generation | ||
| * Run: npm run build:headers | ||
| */ | ||
|
|
||
| interface HeadersConfig { | ||
| paths: Array<{ | ||
| pattern: string; | ||
| headers: Record<string, string[] | Record<string, string[]>>; | ||
| }>; | ||
| } | ||
|
|
||
| const config: HeadersConfig = { | ||
| paths: [ | ||
| { | ||
| pattern: '/*', | ||
| headers: { | ||
| 'Content-Security-Policy': { | ||
| 'connect-src': [ | ||
| "'self'", | ||
| 'https://status.maxmind.com', | ||
| 'https://www.maxmind.com', | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| // https://knowledge.hubspot.com/domains-and-urls/ssl-and-domain-security-in-hubspot#content-security-policy | ||
|
|
||
| // HubSpot API | ||
| 'https://api.hubspot.com', | ||
|
|
||
| // HubSpot static assets (conversations embed) | ||
| 'https://forms.hsforms.com', | ||
|
|
||
| 'https://*.googleapis.com', | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| // https://developers.google.com/tag-platform/security/guides/csp#google_analytics_4_google_analytics | ||
| 'https://*.google-analytics.com', | ||
| 'https://*.analytics.google.com', | ||
| 'https://*.googletagmanager.com', | ||
|
|
||
| // https://developers.google.com/tag-platform/security/guides/csp#google_ads | ||
| 'https://*.g.doubleclick.net', | ||
|
|
||
| // Google domains (various TLDs for international support) | ||
| 'https://*.google.com', | ||
| ], | ||
| 'default-src': [ | ||
| "'self'", | ||
| ], | ||
| 'font-src': [ | ||
| "'self'", | ||
|
|
||
| // Loaded indirectly by Google Vertex search | ||
| 'https://fonts.gstatic.com', | ||
| ], | ||
| 'form-action': [ | ||
| "'self'", | ||
| ], | ||
| 'frame-ancestors': [ | ||
| "'self'", | ||
| ], | ||
| 'frame-src': [ | ||
| "'self'", | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| // https://knowledge.hubspot.com/domains-and-urls/ssl-and-domain-security-in-hubspot#content-security-policy | ||
|
|
||
| // HubSpot calls-to-action (pop-ups) and chatflows | ||
| 'https://app.hubspot.com', | ||
|
|
||
| // https://developers.google.com/tag-platform/security/guides/csp#google_ads | ||
| 'https://www.googletagmanager.com', | ||
|
|
||
| // Google Vertex search | ||
| 'https://www.google.com', | ||
| ], | ||
| 'img-src': [ | ||
| "'self'", | ||
| 'data:', | ||
| 'https:', | ||
| ], | ||
| 'object-src': [ | ||
| "'none'", | ||
| ], | ||
| 'script-src': [ | ||
| "'self'", | ||
| "'report-sample'", | ||
| "'unsafe-inline'", | ||
|
|
||
| // eslint-disable-next-line max-len | ||
| // https://knowledge.hubspot.com/domains-and-urls/ssl-and-domain-security-in-hubspot#content-security-policy | ||
|
|
||
| // HubSpot tracking code | ||
| 'https://js.hs-scripts.com', | ||
|
|
||
| // HubSpot Analytics | ||
| 'https://js.hs-analytics.net', | ||
|
|
||
| // HubSpot cookie banner | ||
| 'https://js.hs-banner.com', | ||
|
|
||
| // HubSpot Conversations and Chatflows | ||
| 'https://js.usemessages.com', | ||
|
|
||
| // HubSpot forms | ||
| 'https://js.hsforms.net', | ||
|
|
||
| // MaxMind marketing site | ||
| 'https://www.maxmind.com', | ||
|
|
||
| // Google Vertex search | ||
| 'https://cloud.google.com', | ||
| 'https://www.gstatic.com', | ||
|
|
||
| // https://developers.google.com/tag-platform/security/guides/csp#google_ads_conversions | ||
| 'https://www.googleadservices.com', | ||
| 'https://www.google.com', | ||
|
|
||
| // Google Tag Manager | ||
| 'https://*.googletagmanager.com', | ||
| ], | ||
| 'style-src': [ | ||
| "'self'", | ||
| "'unsafe-inline'", | ||
|
|
||
| // Google Fonts API and Vertex search default styles | ||
| 'https://fonts.googleapis.com', | ||
|
|
||
| // Google static assets | ||
| 'https://www.gstatic.com', | ||
| ], | ||
| }, | ||
| 'Feature-Policy': [ | ||
| "accelerometer 'none'", | ||
| "autoplay 'none'", | ||
| "camera 'none'", | ||
| "encrypted-media 'none'", | ||
| "fullscreen 'none'", | ||
| "geolocation 'none'", | ||
| "gyroscope 'none'", | ||
| "magnetometer 'none'", | ||
| "microphone 'none'", | ||
| "midi 'none'", | ||
| "payment 'none'", | ||
| "picture-in-picture 'none'", | ||
| "usb 'none'", | ||
| "sync-xhr 'none'", | ||
| ], | ||
| 'Permissions-Policy': [ | ||
| 'accelerometer=()', | ||
| 'ambient-light-sensor=()', | ||
| 'autoplay=()', | ||
| 'battery=()', | ||
| 'camera=()', | ||
| 'display-capture=()', | ||
| 'document-domain=()', | ||
| 'encrypted-media=()', | ||
| 'execution-while-not-rendered=()', | ||
| 'execution-while-out-of-viewport=()', | ||
| 'fullscreen=()', | ||
| 'gamepad=()', | ||
| 'geolocation=()', | ||
| 'gyroscope=()', | ||
| 'hid=()', | ||
| 'idle-detection=()', | ||
| 'magnetometer=()', | ||
| 'microphone=()', | ||
| 'midi=()', | ||
| 'payment=()', | ||
| 'picture-in-picture=()', | ||
| 'publickey-credentials-get=()', | ||
| 'screen-wake-lock=()', | ||
| 'serial=()', | ||
| 'speaker-selection=()', | ||
| 'usb=()', | ||
| 'web-share=()', | ||
| 'xr-spatial-tracking=()', | ||
| ], | ||
| 'Referrer-Policy': [ | ||
| 'strict-origin-when-cross-origin', | ||
| ], | ||
| 'Strict-Transport-Security': [ | ||
| 'max-age=63072000', | ||
| 'includeSubDomains', | ||
| 'preload', | ||
| ], | ||
| 'X-Content-Type-Options': [ | ||
| 'nosniff', | ||
| ], | ||
| 'X-Frame-Options': [ | ||
| 'DENY', | ||
| ], | ||
| 'X-XSS-Protection': [ | ||
| '1', | ||
| 'mode=block', | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * Generate static/_headers file from bin/_headers.config.ts | ||
| * | ||
| * This script converts a structured TypeScript configuration into the | ||
| * Cloudflare Pages _headers format. | ||
| */ | ||
|
|
||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
|
|
||
| import config from './_headers.config.ts'; | ||
|
|
||
| interface PathConfig { | ||
| pattern: string; | ||
| headers: Record<string, string[] | Record<string, string[]>>; | ||
| } | ||
|
|
||
| /** | ||
| * Generate _headers file content from config | ||
| */ | ||
| function generateHeaders(config: { paths: PathConfig[] }): string { | ||
| let output = ''; | ||
|
|
||
| // Add warning comment at the top | ||
| output += '# ⚠️ DO NOT EDIT THIS FILE DIRECTLY!\n'; | ||
| output += '# This file is automatically generated from bin/_headers.config.ts\n'; | ||
| output += '# To make changes, edit bin/_headers.config.ts and run: npm run build:headers\n'; | ||
| output += '#\n'; | ||
| output += '# See README.md for more information\n'; | ||
| output += '\n'; | ||
|
|
||
| for (const pathConfig of config.paths) { | ||
| // Write path pattern | ||
| output += pathConfig.pattern + '\n'; | ||
|
|
||
| // Process all headers | ||
| for (const [ | ||
| header, | ||
| value, | ||
| ] of Object.entries(pathConfig.headers)) { | ||
| // Handle Content-Security-Policy (nested object) | ||
| if (header === 'Content-Security-Policy') { | ||
| const csp = value as Record<string, string[]>; | ||
| const directives: string[] = []; | ||
|
|
||
| for (const [ | ||
| directive, | ||
| sources, | ||
| ] of Object.entries(csp)) { | ||
| directives.push(`${directive} ${sources.join(' ')}`); | ||
| } | ||
|
|
||
| output += ` Content-Security-Policy: ${directives.join('; ')}\n`; | ||
| continue; | ||
| } | ||
|
|
||
| // Handle all other headers (arrays) | ||
| const values = value as string[]; | ||
| const separator = header === 'Permissions-Policy' ? ', ' : '; '; | ||
| output += ` ${header}: ${values.join(separator)}\n`; | ||
| } | ||
| } | ||
|
|
||
| return output; | ||
| } | ||
|
|
||
| // Main execution | ||
| try { | ||
| const outputPath = path.join(process.cwd(), 'static', '_headers'); | ||
|
|
||
| // Generate headers file | ||
| const headersContent = generateHeaders(config); | ||
|
|
||
| // Write output file | ||
| fs.writeFileSync(outputPath, headersContent); | ||
|
|
||
| console.log('✅ Generated static/_headers from bin/_headers.config.ts'); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error generating headers file:', | ||
| error instanceof Error ? error.message : String(error) | ||
| ); | ||
| process.exit(1); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,6 +144,9 @@ export default tseslint.config( | |
| quotes: [ | ||
| 'warn', | ||
| 'single', | ||
| { | ||
| avoidEscape: true, | ||
| }, | ||
| ], | ||
| semi: [ | ||
| 1, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not generating the expected output. It's not outputting the proper delimiters (ie
;or,) for non-CSP values.