Skip to content

Commit c9238a7

Browse files
authored
Add support for CSP nonce on GSI script & inline styles (#230)
* fix(useLoadGsiScript): assign nonce to GSI script tag * fix(GoogleOAuthProvider): adds nonce to props * docs: add nonce to README * fix(useLoadGsiScript): adds nonce to dependency array * chore: add changeset * docs: fix typo on README
1 parent 4f7582a commit c9238a7

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

.changeset/old-rats-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-oauth/google': patch
3+
---
4+
5+
Add minor CSP support by accepting "nonce" and propagating it to GSI script & inline style

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ const hasAccess = hasGrantedAnyScopeGoogle(
193193
| Required | Prop | Type | Description |
194194
| :------: | ------------------- | ---------- | --------------------------------------------------------------------------- |
195195
|| clientId | `string` | [**Google API client ID**](https://console.cloud.google.com/apis/dashboard) |
196+
| | nonce | `string` | Nonce applied to GSI script tag. Propagates to GSI's inline style tag |
196197
| | onScriptLoadSuccess | `function` | Callback fires on load gsi script success |
197198
| | onScriptLoadError | `function` | Callback fires on load gsi script failure |
198199

packages/@react-oauth/google/src/GoogleOAuthProvider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ interface GoogleOAuthProviderProps extends UseLoadGsiScriptOptions {
1818

1919
export default function GoogleOAuthProvider({
2020
clientId,
21+
nonce,
2122
onScriptLoadSuccess,
2223
onScriptLoadError,
2324
children,
2425
}: GoogleOAuthProviderProps) {
2526
const scriptLoadedSuccessfully = useLoadGsiScript({
27+
nonce,
2628
onScriptLoadSuccess,
2729
onScriptLoadError,
2830
});

packages/@react-oauth/google/src/hooks/useLoadGsiScript.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { useState, useEffect, useRef } from 'react';
22

33
export interface UseLoadGsiScriptOptions {
4+
/**
5+
* Nonce applied to GSI script tag. Propagates to GSI's inline style tag
6+
*/
7+
nonce?: string;
48
/**
59
* Callback fires on load [gsi](https://accounts.google.com/gsi/client) script success
610
*/
@@ -14,7 +18,7 @@ export interface UseLoadGsiScriptOptions {
1418
export default function useLoadGsiScript(
1519
options: UseLoadGsiScriptOptions = {},
1620
): boolean {
17-
const { onScriptLoadSuccess, onScriptLoadError } = options;
21+
const { nonce, onScriptLoadSuccess, onScriptLoadError } = options;
1822

1923
const [scriptLoadedSuccessfully, setScriptLoadedSuccessfully] =
2024
useState(false);
@@ -30,6 +34,7 @@ export default function useLoadGsiScript(
3034
scriptTag.src = 'https://accounts.google.com/gsi/client';
3135
scriptTag.async = true;
3236
scriptTag.defer = true;
37+
scriptTag.nonce = nonce;
3338
scriptTag.onload = () => {
3439
setScriptLoadedSuccessfully(true);
3540
onScriptLoadSuccessRef.current?.();
@@ -44,7 +49,7 @@ export default function useLoadGsiScript(
4449
return () => {
4550
document.body.removeChild(scriptTag);
4651
};
47-
}, []);
52+
}, [nonce]);
4853

4954
return scriptLoadedSuccessfully;
5055
}

0 commit comments

Comments
 (0)