Skip to content
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

Read the csp meta tag nonce attribute and fall back to content #1254

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/core/drive/progress_bar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { unindent, getMetaContent } from "../../util"
import { unindent, getCspNonce } from "../../util"

export const ProgressBarID = "turbo-progress-bar"

Expand Down Expand Up @@ -108,8 +108,9 @@ export class ProgressBar {
const element = document.createElement("style")
element.type = "text/css"
element.textContent = ProgressBar.defaultCSS
if (this.cspNonce) {
element.nonce = this.cspNonce
const cspNonce = getCspNonce()
if (cspNonce) {
element.nonce = cspNonce
}
return element
}
Expand All @@ -119,8 +120,4 @@ export class ProgressBar {
element.className = "turbo-progress-bar"
return element
}

get cspNonce() {
return getMetaContent("csp-nonce")
}
}
11 changes: 10 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function activateScriptElement(element) {
return element
} else {
const createdScriptElement = document.createElement("script")
const cspNonce = getMetaContent("csp-nonce")
const cspNonce = getCspNonce()
if (cspNonce) {
createdScriptElement.nonce = cspNonce
}
Expand Down Expand Up @@ -173,6 +173,15 @@ export function getMetaContent(name) {
return element && element.content
}

export function getCspNonce() {
const element = getMetaElement("csp-nonce")

if (element) {
const { nonce, content } = element
return nonce == "" ? content : nonce
}
}

export function setMetaContent(name, content) {
let element = getMetaElement(name)

Expand Down