-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for highlighting CSP, HSTS, and HPKP (#1275)
* Add CSP support. * Add HPKP support. * Add HSTS support. * Updates based on feedback. * Updates based on feedback. * Updates based on feedback. * Updates based on feedback. * Updates based on feedback. * Updates based on feedback. * Updates based on Feedback. * Fix parentheses. * List csp, hpkp, and hsts in components.js * Add missing base64 chars to 'nonce-...' and 'sha-...' source expressions * Add support for 'sha384-...' and 'sha512-...' source expressions * Add tests and examples for csp * Add tests and examples for hsts * Add tests and examples for hpkp * hpkp: Include the sha256 hash in the directive itself Prevent the numbers in the base64 string from being classified as "safe" or "unsafe". * Rebuild the minified csp and hpkp components * Rebuild the "Show Language" plugin as well * Fix copy/paste error in test description
- Loading branch information
1 parent
3da238f
commit b08cae5
Showing
23 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
/** | ||
* Original by Scott Helme. | ||
* | ||
* Reference: https://scotthelme.co.uk/csp-cheat-sheet/ | ||
* | ||
* Supports the following: | ||
* - CSP Level 1 | ||
* - CSP Level 2 | ||
* - CSP Level 3 | ||
*/ | ||
|
||
Prism.languages.csp = { | ||
'directive': { | ||
pattern: /\b(?:(?:base-uri|form-action|frame-ancestors|plugin-types|referrer|reflected-xss|report-to|report-uri|require-sri-for|sandbox) |(?:block-all-mixed-content|disown-opener|upgrade-insecure-requests)(?: |;)|(?:child|connect|default|font|frame|img|manifest|media|object|script|style|worker)-src )/i, | ||
alias: 'keyword' | ||
}, | ||
'safe': { | ||
pattern: /'(?:self|none|strict-dynamic|(?:nonce-|sha(?:256|384|512)-)[a-zA-Z0-9+=/]+)'/, | ||
alias: 'selector' | ||
}, | ||
'unsafe': { | ||
pattern: /(?:'unsafe-inline'|'unsafe-eval'|'unsafe-hashed-attributes'|\*)/, | ||
alias: 'function' | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,20 @@ | ||
/** | ||
* Original by Scott Helme. | ||
* | ||
* Reference: https://scotthelme.co.uk/hpkp-cheat-sheet/ | ||
*/ | ||
|
||
Prism.languages.hpkp = { | ||
'directive': { | ||
pattern: /\b(?:(?:includeSubDomains|preload|strict)(?: |;)|pin-sha256="[a-zA-Z0-9+=/]+"|(?:max-age|report-uri)=|report-to )/, | ||
alias: 'keyword' | ||
}, | ||
'safe': { | ||
pattern: /[0-9]{7,}/, | ||
alias: 'selector' | ||
}, | ||
'unsafe': { | ||
pattern: /[0-9]{0,6}/, | ||
alias: 'function' | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,20 @@ | ||
/** | ||
* Original by Scott Helme. | ||
* | ||
* Reference: https://scotthelme.co.uk/hsts-cheat-sheet/ | ||
*/ | ||
|
||
Prism.languages.hsts = { | ||
'directive': { | ||
pattern: /\b(?:max-age=|includeSubDomains|preload)/, | ||
alias: 'keyword' | ||
}, | ||
'safe': { | ||
pattern: /[0-9]{8,}/, | ||
alias: 'selector' | ||
}, | ||
'unsafe': { | ||
pattern: /[0-9]{0,7}/, | ||
alias: 'function' | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,16 @@ | ||
<h1>HTTP Content-Security-Policy header value</h1> | ||
<p>To use this language, use the class "language-csp".</p> | ||
|
||
<h2>A complete policy</h2> | ||
<pre><code>default-src 'none'; | ||
script-src my.cdn.com; | ||
img-src 'self' data:; | ||
child-src 'self' data: ms-appx-web:; | ||
block-all-mixed-content; | ||
report-uri https://my-reports.com/submit; | ||
</code></pre> | ||
|
||
<h2>An policy with unsafe source expressions</h2> | ||
<pre><code>script-src 'self' 'unsafe-eval' 'unsafe-inline'; | ||
style-src 'unsafe-inline' 'unsafe-hashed-attributes' 'self'; | ||
</code></pre> |
This file contains 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,14 @@ | ||
<h1>HTTP Public-Key-Pins header value</h1> | ||
<p>To use this language, use the class "language-hpkp".</p> | ||
|
||
<h2>Pin for one year with report-uri</h2> | ||
<pre><code>pin-sha256="EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4="; | ||
max-age=31536000; | ||
includeSubDomains; | ||
report-uri="https://my-reports.com/submit" | ||
</code></pre> | ||
|
||
<h2>Pin for a short time (considered unsafe)</h2> | ||
<pre><code>pin-sha256="EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4="; | ||
max-age=123 | ||
</code></pre> |
This file contains 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,11 @@ | ||
<h1>HTTP Strict-Transport-Security header value</h1> | ||
<p>To use this language, use the class "language-hsts".</p> | ||
|
||
<h2>Policy with far-future max-age</h2> | ||
<pre><code>max-age=31536000</code></pre> | ||
|
||
<h2>Policy with near-future max-age, considered unsafe</h2> | ||
<pre><code>max-age=123</code></pre> | ||
|
||
<h2>Policy with extra directives</h2> | ||
<pre><code>max-age=31536000; includeSubdomains; preload</code></pre> |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,11 @@ | ||
upgrade-insecure-requests; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "upgrade-insecure-requests;"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for a "void" CSP directive followed by semicolon. |
12 changes: 12 additions & 0 deletions
12
tests/languages/csp/directive_with_source_expression_feature.test
This file contains 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,12 @@ | ||
script-src example.com; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "script-src "], | ||
"example.com;" | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for CSP directive followed by a source expression. |
This file contains 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,19 @@ | ||
default-src 'none'; style-src 'self' 'strict-dynamic' 'nonce-yeah' 'sha256-EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4='; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "default-src "], | ||
["safe", "'none'"], | ||
"; ", | ||
["directive", "style-src "], | ||
["safe", "'self'"], | ||
["safe", "'strict-dynamic'"], | ||
["safe", "'nonce-yeah'"], | ||
["safe", "'sha256-EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4='"], | ||
";" | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for source expressions classified as safe. |
This file contains 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,15 @@ | ||
script-src 'unsafe-inline' 'unsafe-eval' 'unsafe-hashed-attributes'; | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "script-src "], | ||
["unsafe", "'unsafe-inline'"], | ||
["unsafe", "'unsafe-eval'"], | ||
["unsafe", "'unsafe-hashed-attributes'"], | ||
";" | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for source expressions classified as unsafe. |
This file contains 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,12 @@ | ||
max-age=31536000 | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "max-age="], | ||
["safe", "31536000"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HPKP with a "safe" max-age. |
This file contains 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,11 @@ | ||
pin-sha256="EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4=" | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "pin-sha256=\"EpOpN/ahUF6jhWShDUdy+NvvtaGcu5F7qM6+x2mfkh4=\""] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HPKP with a sha256 pin. |
This file contains 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,12 @@ | ||
max-age=123 | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "max-age="], | ||
["unsafe", "123"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HPKP with an "unsafe" max-age. |
This file contains 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,11 @@ | ||
includeSubDomains | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "includeSubDomains"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HSTS with the includeSubDomains directive. |
This file contains 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,11 @@ | ||
preload | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "preload"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HSTS with the preload directive. |
This file contains 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,12 @@ | ||
max-age=31536000 | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "max-age="], | ||
["safe", "31536000"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HSTS with a "safe" max-age. |
This file contains 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,12 @@ | ||
max-age=123 | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["directive", "max-age="], | ||
["unsafe", "123"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for HSTS with an "unsafe" max-age. |