Skip to content

resolve sha hash exemptions #525

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 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const send = (action: T.Action): void => {
}

export const createCommands = (commandProps: CreateCommandProps): { [key: string]: any } => {
console.log(commandProps)
const { extensionPath, workspaceState } = commandProps
// React panel webview
let webview: any
Expand Down
19 changes: 16 additions & 3 deletions src/services/webview/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<voi

// used for CSP
const nonces: string[] = []
const hashes: string[] = []

// generate vscode-resource build path uri
const createUri = (_filePath: string): any => {
Expand All @@ -50,7 +51,12 @@ async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<voi
// support additional CSP exemptions when CodeRoad is embedded
if (CONTENT_SECURITY_POLICY_EXEMPTIONS && CONTENT_SECURITY_POLICY_EXEMPTIONS.length) {
for (const exemption of CONTENT_SECURITY_POLICY_EXEMPTIONS.split(' ')) {
nonces.push(exemption)
// sha hashes should not be prefixed with 'nonce-'
if (exemption.match(/^sha/)) {
hashes.push(exemption)
} else {
nonces.push(exemption)
}
}
}

Expand All @@ -71,17 +77,24 @@ async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<voi
}

// set CSP (content security policy) to grant permission to local files
// while blocking unexpected malicious network requests
const cspMeta: HTMLMetaElement = document.createElement('meta')
cspMeta.httpEquiv = 'Content-Security-Policy'

const wrapInQuotes = (str: string) => `'${str}'`
const nonceString = nonces.map((nonce: string) => wrapInQuotes(`nonce-${nonce}`)).join(' ')
const hashString = hashes.map(wrapInQuotes).join(' ')

cspMeta.content =
[
`default-src 'self'`,
`manifest-src ${hashString} 'self'`,
`connect-src https: http:`,
// @ts-ignore
`font-src ${panel.webview.cspSource} http: https: data:`,
// @ts-ignore
`img-src ${panel.webview.cspSource} https:`,
`script-src ${nonces.map((nonce) => `'nonce-${nonce}'`).join(' ')} data:`,
`script-src ${nonceString} ${hashString} data:`,
// @ts-ignore
`style-src ${panel.webview.cspSource} https: 'self' 'unsafe-inline'`,
].join('; ') + ';'
Expand All @@ -92,7 +105,7 @@ async function render(panel: vscode.WebviewPanel, rootPath: string): Promise<voi

// set view
panel.webview.html = html
} catch (error) {
} catch (error: any) {
onError(error)
console.error(error)
}
Expand Down