-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hash instead of unsafe-inline for naive-ui (#33)
* Add hash instead of unsafe-inline for naive-ui * Migrate GA to node 20 * Check errors in browser's console
- Loading branch information
1 parent
f587459
commit 80d8326
Showing
7 changed files
with
116 additions
and
20 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
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
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,18 @@ | ||
/** | ||
* Add hashes of inline styles to the CSP policy in the HTML. | ||
* | ||
* @description Naive-ui uses inline styles to style components. | ||
* | ||
* [tag-nonce] | ||
* @param {string} html | ||
* @param {string[]} listOfHashes | ||
* @returns {*} | ||
*/ | ||
export function addInlineStylesHashesToHtml(html, listOfHashes) { | ||
const hashes = listOfHashes.map((hash) => `'${hash}'`).join(" "); | ||
|
||
return html.replace( | ||
"style-src 'self'", | ||
`style-src 'self' 'unsafe-hashes' ${hashes}`, | ||
); | ||
} |
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,46 @@ | ||
import crypto from "crypto"; | ||
|
||
/** | ||
* Calculates SHA-256 hash of the given style content and returns it in base64 format. | ||
* | ||
* ``` | ||
* # Input | ||
* max-width:250px;text-align:left;margin:0 auto;width:100%; | ||
* # output | ||
* sha256-O5IIiIzIB9wS0DmNOhwTAp7C6vPXN8QJ3R0ZS+HTUNM= | ||
* ``` | ||
* | ||
* @param {string} styleContent | ||
* @returns {string} | ||
*/ | ||
function calculateStyleHash(styleContent) { | ||
const hash = crypto | ||
.createHash("sha256") | ||
.update(styleContent, "utf8") | ||
.digest("base64"); | ||
return `sha256-${hash}`; | ||
} | ||
|
||
/** | ||
* Extracts inline styles from the given HTML content. | ||
* | ||
* @param {string} appHtml - The HTML content to extract inline styles from. | ||
* @returns {string[]} An array of inline style strings. | ||
*/ | ||
function getInlineStyles(appHtml) { | ||
return ( | ||
appHtml | ||
.match(/ style=".*?"/g) | ||
?.map((line) => line.replace(/^ style="/, "").replace(/"$/, "")) || [] | ||
); | ||
} | ||
|
||
/** | ||
* Generates an array of unique SHA-256 hashes for all inline styles found in the given HTML content. | ||
* | ||
* @param {string} appHtml - The HTML content to extract and hash inline styles from. | ||
* @returns {string[]} An array of unique SHA-256 hashes in base64 format. | ||
*/ | ||
export function getInlineStylesHashes(appHtml) { | ||
return [...new Set(getInlineStyles(appHtml).map(calculateStyleHash))]; | ||
} |
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