-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
- Loading branch information
Showing
4 changed files
with
88 additions
and
10 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,27 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
|
||
import { getRequestToken } from './requesttoken' | ||
|
||
/** | ||
* Get the CSP nonce for script loading | ||
* | ||
* @return Current nonce if set | ||
* @example When using webpack this can be used to allow webpack to dynamically load additional modules: | ||
* ```js | ||
* import { getCSPNonce } from '@nextcloud/auth' | ||
* | ||
* __webpack_nonce__ = getCSPNonce() | ||
* ``` | ||
*/ | ||
export function getCSPNonce(): string | undefined { | ||
const meta = document?.querySelector<HTMLMetaElement>('meta[name="csp-nonce"]') | ||
// backwards compatibility with older Nextcloud versions | ||
if (!meta) { | ||
const token = getRequestToken() | ||
return token ? btoa(token) : undefined | ||
} | ||
return meta.nonce | ||
} |
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,54 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: GPL-3.0-or-later | ||
*/ | ||
import { randomBytes } from 'crypto' | ||
import { beforeEach, describe, expect, test, vi } from 'vitest' | ||
|
||
/** | ||
* Mock `<meta>` element with nonce | ||
*/ | ||
function mockNonce() { | ||
const nonce = randomBytes(16).toString('base64') | ||
const el = document.createElement('meta') | ||
el.name = 'csp-nonce' | ||
el.nonce = nonce | ||
document.head.appendChild(el) | ||
return nonce | ||
} | ||
|
||
describe('CSP nonce', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
// reset document | ||
document.head.innerHTML = '' | ||
delete document.head.dataset.requesttoken | ||
}) | ||
|
||
test('read nonce from meta element', async () => { | ||
const { getCSPNonce } = await import('../lib') | ||
const nonce = mockNonce() | ||
expect(getCSPNonce()).toBe(nonce) | ||
}) | ||
|
||
test('prefer nonce over csrf token', async () => { | ||
const { getCSPNonce } = await import('../lib') | ||
|
||
const nonce = mockNonce() | ||
document.head.dataset.requesttoken = 'csrf-token' | ||
expect(getCSPNonce()).toBe(nonce) | ||
}) | ||
|
||
test('fall back to csrf token for legacy Nextcloud versions', async () => { | ||
const { getCSPNonce } = await import('../lib') | ||
|
||
document.head.dataset.requesttoken = 'csrf-token' | ||
expect(getCSPNonce()).toBe(btoa('csrf-token')) | ||
}) | ||
|
||
test('return undefined if neither csp nonce nor csrf token is set', async () => { | ||
const { getCSPNonce } = await import('../lib') | ||
|
||
expect(getCSPNonce()).toBe(undefined) | ||
}) | ||
}) |