-
Notifications
You must be signed in to change notification settings - Fork 9
feat: implement PKCE code_verifier storage in sessionStorage for brow… #509
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
Open
nadeem-cs
wants to merge
1
commit into
development
Choose a base branch
from
enhancement/DX-4341
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,68 @@ | ||
| /** | ||
| * PKCE code_verifier persistence in sessionStorage for browser SPAs. | ||
| * Survives OAuth redirects; not used in Node. RFC 7636 / OAuth 2.0 for Browser-Based Apps. | ||
| */ | ||
|
|
||
| const PKCE_STORAGE_KEY_PREFIX = 'contentstack_oauth_pkce' | ||
| const PKCE_STORAGE_EXPIRY_MS = 10 * 60 * 1000 // 10 minutes | ||
|
|
||
| function isBrowser () { | ||
| return typeof window !== 'undefined' && typeof window.sessionStorage !== 'undefined' | ||
| } | ||
|
|
||
| function getStorageKey (appId, clientId, redirectUri) { | ||
| return `${PKCE_STORAGE_KEY_PREFIX}_${appId}_${clientId}_${redirectUri}` | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} appId | ||
| * @param {string} clientId | ||
| * @param {string} redirectUri | ||
| * @returns {string|null} code_verifier if valid and not expired, otherwise null | ||
| */ | ||
| export function getStoredCodeVerifier (appId, clientId, redirectUri) { | ||
| if (!isBrowser()) return null | ||
| try { | ||
| const raw = window.sessionStorage.getItem(getStorageKey(appId, clientId, redirectUri)) | ||
| if (!raw) return null | ||
| const { codeVerifier, expiresAt } = JSON.parse(raw) | ||
| if (!codeVerifier || !expiresAt || Date.now() > expiresAt) return null | ||
| return codeVerifier | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} appId | ||
| * @param {string} clientId | ||
| * @param {string} redirectUri | ||
| * @param {string} codeVerifier | ||
| */ | ||
| export function storeCodeVerifier (appId, clientId, redirectUri, codeVerifier) { | ||
| if (!isBrowser()) return | ||
| try { | ||
| const key = getStorageKey(appId, clientId, redirectUri) | ||
| const value = JSON.stringify({ | ||
| codeVerifier, | ||
| expiresAt: Date.now() + PKCE_STORAGE_EXPIRY_MS | ||
| }) | ||
| window.sessionStorage.setItem(key, value) | ||
| } catch { | ||
| // Ignore storage errors (e.g. private mode); fall back to memory-only | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} appId | ||
| * @param {string} clientId | ||
| * @param {string} redirectUri | ||
| */ | ||
| export function clearStoredCodeVerifier (appId, clientId, redirectUri) { | ||
| if (!isBrowser()) return | ||
| try { | ||
| window.sessionStorage.removeItem(getStorageKey(appId, clientId, redirectUri)) | ||
| } catch { | ||
| // Ignore | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,119 @@ | ||
| import { expect } from 'chai' | ||
| import sinon from 'sinon' | ||
| import { | ||
| getStoredCodeVerifier, | ||
| storeCodeVerifier, | ||
| clearStoredCodeVerifier | ||
| } from '../../lib/core/pkceStorage' | ||
| import { describe, it, beforeEach, afterEach } from 'mocha' | ||
|
|
||
| describe('pkceStorage', () => { | ||
| let sessionStorageStub | ||
|
|
||
| beforeEach(() => { | ||
| sessionStorageStub = { | ||
| getItem: sinon.stub(), | ||
| setItem: sinon.stub(), | ||
| removeItem: sinon.stub() | ||
| } | ||
| global.window = { sessionStorage: sessionStorageStub } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| delete global.window | ||
| }) | ||
|
|
||
| describe('getStoredCodeVerifier', () => { | ||
| it('returns null when not in browser', () => { | ||
| delete global.window | ||
| expect(getStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.equal(null) | ||
| }) | ||
|
|
||
| it('returns null when nothing stored', () => { | ||
| sessionStorageStub.getItem.returns(null) | ||
| expect(getStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.equal(null) | ||
| }) | ||
|
|
||
| it('returns code_verifier when valid and not expired', () => { | ||
| const stored = JSON.stringify({ | ||
| codeVerifier: 'stored_verifier_xyz', | ||
| expiresAt: Date.now() + 600000 | ||
| }) | ||
| sessionStorageStub.getItem.returns(stored) | ||
| expect(getStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.equal('stored_verifier_xyz') | ||
| }) | ||
|
|
||
| it('returns null when stored entry is expired', () => { | ||
| const stored = JSON.stringify({ | ||
| codeVerifier: 'expired_verifier', | ||
| expiresAt: Date.now() - 1000 | ||
| }) | ||
| sessionStorageStub.getItem.returns(stored) | ||
| expect(getStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.equal(null) | ||
| }) | ||
|
|
||
| it('returns null when storage throws', () => { | ||
| sessionStorageStub.getItem.throws(new Error('QuotaExceeded')) | ||
| expect(getStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.equal(null) | ||
| }) | ||
|
|
||
| it('uses key containing appId, clientId, redirectUri', () => { | ||
| sessionStorageStub.getItem.returns(null) | ||
| getStoredCodeVerifier('myApp', 'myClient', 'https://app.example/cb') | ||
| expect(sessionStorageStub.getItem.calledOnce).to.equal(true) | ||
| const key = sessionStorageStub.getItem.firstCall.args[0] | ||
| expect(key).to.include('contentstack_oauth_pkce') | ||
| expect(key).to.include('myApp') | ||
| expect(key).to.include('myClient') | ||
| expect(key).to.include('https://app.example/cb') | ||
| }) | ||
| }) | ||
|
|
||
| describe('storeCodeVerifier', () => { | ||
| it('does nothing when not in browser', () => { | ||
| delete global.window | ||
| storeCodeVerifier('appId', 'clientId', 'http://localhost:8184', 'verifier123') | ||
| expect(sessionStorageStub.setItem.called).to.equal(false) | ||
| }) | ||
|
|
||
| it('stores codeVerifier and expiresAt in sessionStorage', () => { | ||
| const before = Date.now() | ||
| storeCodeVerifier('appId', 'clientId', 'http://localhost:8184', 'verifier123') | ||
| const after = Date.now() | ||
| expect(sessionStorageStub.setItem.calledOnce).to.equal(true) | ||
| const [key, valueStr] = sessionStorageStub.setItem.firstCall.args | ||
| expect(key).to.include('contentstack_oauth_pkce') | ||
| const value = JSON.parse(valueStr) | ||
| expect(value.codeVerifier).to.equal('verifier123') | ||
| expect(value.expiresAt).to.be.at.least(before + 9 * 60 * 1000) | ||
| expect(value.expiresAt).to.be.at.most(after + 10 * 60 * 1000 + 100) | ||
| }) | ||
|
|
||
| it('does not throw when sessionStorage.setItem throws', () => { | ||
| sessionStorageStub.setItem.throws(new Error('QuotaExceeded')) | ||
| expect(() => storeCodeVerifier('appId', 'clientId', 'http://localhost:8184', 'v')).to.not.throw() | ||
| }) | ||
| }) | ||
|
|
||
| describe('clearStoredCodeVerifier', () => { | ||
| it('does nothing when not in browser', () => { | ||
| delete global.window | ||
| clearStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184') | ||
| expect(sessionStorageStub.removeItem.called).to.equal(false) | ||
| }) | ||
|
|
||
| it('calls sessionStorage.removeItem with correct key', () => { | ||
| clearStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184') | ||
| expect(sessionStorageStub.removeItem.calledOnce).to.equal(true) | ||
| const key = sessionStorageStub.removeItem.firstCall.args[0] | ||
| expect(key).to.include('contentstack_oauth_pkce') | ||
| expect(key).to.include('appId') | ||
| expect(key).to.include('clientId') | ||
| }) | ||
|
|
||
| it('does not throw when sessionStorage.removeItem throws', () => { | ||
| sessionStorageStub.removeItem.throws(new Error('SecurityError')) | ||
| expect(() => clearStoredCodeVerifier('appId', 'clientId', 'http://localhost:8184')).to.not.throw() | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nadeem-cs , can we use httpcookies instead of session storage? Session storage might create security issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aman19K Actually using sessionStorage is more recommended approach for the issue we're trying to resolve here - browser compatibility. It does not give any security concern. It will be only risky if the app has XSS bug , only then it can read the session cookie. Otherwise this is more recommended approach.