From e91bda2bab96a3b170c7523259e7bccaf82114e5 Mon Sep 17 00:00:00 2001 From: Eugene Olonov Date: Thu, 3 Dec 2026 11:16:47 -0800 Subject: [PATCH 1/2] Derive SSO sample PKCE verifier and OAuth state with scrypt CodeQL js/insufficient-password-hash flagged the SSO samples under 07.advanced-web-chat-apps for deriving the PKCE code verifier and the GitHub OAuth "state" from a seed and a salt with a fast hash (SHA-512 / SHA-384). Derive them with crypto.scryptSync (N: 16384) instead. scrypt is deterministic for the same seed and salt, so any server instance still recomputes the same verifier or state, as the samples' distributed design requires. - createPKCECodeVerifier.js (b, c, d, e): 64 bytes, encoded with Node's base64url (86 characters). The previous encoder left a trailing "=", which is outside the RFC 7636 verifier alphabet. - generateOAuthState.js (b, c, d, e): 5 bytes as hex, still 10 characters. c, d and e carry identical copies of this helper, changed the same way. createPKCECodeChallenge.js (SHA-256 of the verifier, required by S256) is unchanged. --- .../aad/oauth/createPKCECodeVerifier.js | 19 +++++++++---------- .../rest-api/src/utils/generateOAuthState.js | 14 ++++++++------ .../routes/oauth/createPKCECodeVerifier.js | 19 +++++++++---------- .../web/src/utils/generateOAuthState.js | 14 ++++++++------ .../routes/oauth/createPKCECodeVerifier.js | 19 +++++++++---------- .../web/src/utils/generateOAuthState.js | 14 ++++++++------ .../aad/oauth/createPKCECodeVerifier.js | 19 +++++++++---------- .../rest-api/src/utils/generateOAuthState.js | 14 ++++++++------ 8 files changed, 68 insertions(+), 64 deletions(-) diff --git a/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js b/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js index 1aa0ad4dfb..3623b3d94f 100644 --- a/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js +++ b/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js @@ -1,15 +1,14 @@ -const { createHash } = require('crypto'); -const encodeBase64URL = require('../../../encodeBase64URL'); +const { scryptSync } = require('crypto'); const { AAD_OAUTH_PKCE_SALT } = process.env; -// Creating a PKCE code verifier based on salt and seed. -// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. -module.exports = seed => { - const hash = createHash('sha512'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); - hash.update(seed); - hash.update(AAD_OAUTH_PKCE_SALT); +// 64 bytes are 86 base64url characters, within the 43–128 characters required by RFC 7636. +const VERIFIER_BYTES = 64; - return encodeBase64URL(hash.digest()); -}; +// Creating a PKCE code verifier based on salt and seed. +// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. +// scrypt is deterministic: every server with the same salt derives the same verifier. +module.exports = seed => scryptSync(seed, AAD_OAUTH_PKCE_SALT, VERIFIER_BYTES, SCRYPT_OPTIONS).toString('base64url'); diff --git a/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/utils/generateOAuthState.js b/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/utils/generateOAuthState.js index 2b5b681e66..51c1d3b479 100644 --- a/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/utils/generateOAuthState.js +++ b/samples/07.advanced-web-chat-apps/b.sso-for-enterprise/rest-api/src/utils/generateOAuthState.js @@ -1,12 +1,14 @@ const crypto = require('crypto'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); + +// 5 bytes are 10 hex characters. +const STATE_BYTES = 5; + // For additional security, GitHub recommends using a "state" parameter that is only known to the servers and never passed to the client. // We are using a well-known salt to create the "state" parameter for distributed knowledge. +// scrypt is deterministic: every server with the same salt derives the same state. module.exports = function generateOAuthState(seed, salt) { - const hash = crypto.createHash('sha384'); - - hash.update(seed); - hash.update(salt); - - return hash.digest('hex').substr(0, 10); + return crypto.scryptSync(seed, salt, STATE_BYTES, SCRYPT_OPTIONS).toString('hex'); }; diff --git a/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/routes/oauth/createPKCECodeVerifier.js b/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/routes/oauth/createPKCECodeVerifier.js index 536ec20fbb..d6d3e72ba6 100644 --- a/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/routes/oauth/createPKCECodeVerifier.js +++ b/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/routes/oauth/createPKCECodeVerifier.js @@ -1,15 +1,14 @@ -const { createHash } = require('crypto'); -const encodeBase64URL = require('../../encodeBase64URL'); +const { scryptSync } = require('crypto'); const { OAUTH_PKCE_SALT } = process.env; -// Creating a PKCE code verifier based on salt and seed. -// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. -module.exports = seed => { - const hash = createHash('sha512'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); - hash.update(seed); - hash.update(OAUTH_PKCE_SALT); +// 64 bytes are 86 base64url characters, within the 43–128 characters required by RFC 7636. +const VERIFIER_BYTES = 64; - return encodeBase64URL(hash.digest()); -}; +// Creating a PKCE code verifier based on salt and seed. +// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. +// scrypt is deterministic: every server with the same salt derives the same verifier. +module.exports = seed => scryptSync(seed, OAUTH_PKCE_SALT, VERIFIER_BYTES, SCRYPT_OPTIONS).toString('base64url'); diff --git a/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/utils/generateOAuthState.js b/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/utils/generateOAuthState.js index 2b5b681e66..51c1d3b479 100644 --- a/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/utils/generateOAuthState.js +++ b/samples/07.advanced-web-chat-apps/c.sso-for-intranet/web/src/utils/generateOAuthState.js @@ -1,12 +1,14 @@ const crypto = require('crypto'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); + +// 5 bytes are 10 hex characters. +const STATE_BYTES = 5; + // For additional security, GitHub recommends using a "state" parameter that is only known to the servers and never passed to the client. // We are using a well-known salt to create the "state" parameter for distributed knowledge. +// scrypt is deterministic: every server with the same salt derives the same state. module.exports = function generateOAuthState(seed, salt) { - const hash = crypto.createHash('sha384'); - - hash.update(seed); - hash.update(salt); - - return hash.digest('hex').substr(0, 10); + return crypto.scryptSync(seed, salt, STATE_BYTES, SCRYPT_OPTIONS).toString('hex'); }; diff --git a/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/routes/oauth/createPKCECodeVerifier.js b/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/routes/oauth/createPKCECodeVerifier.js index 536ec20fbb..d6d3e72ba6 100644 --- a/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/routes/oauth/createPKCECodeVerifier.js +++ b/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/routes/oauth/createPKCECodeVerifier.js @@ -1,15 +1,14 @@ -const { createHash } = require('crypto'); -const encodeBase64URL = require('../../encodeBase64URL'); +const { scryptSync } = require('crypto'); const { OAUTH_PKCE_SALT } = process.env; -// Creating a PKCE code verifier based on salt and seed. -// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. -module.exports = seed => { - const hash = createHash('sha512'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); - hash.update(seed); - hash.update(OAUTH_PKCE_SALT); +// 64 bytes are 86 base64url characters, within the 43–128 characters required by RFC 7636. +const VERIFIER_BYTES = 64; - return encodeBase64URL(hash.digest()); -}; +// Creating a PKCE code verifier based on salt and seed. +// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. +// scrypt is deterministic: every server with the same salt derives the same verifier. +module.exports = seed => scryptSync(seed, OAUTH_PKCE_SALT, VERIFIER_BYTES, SCRYPT_OPTIONS).toString('base64url'); diff --git a/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/utils/generateOAuthState.js b/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/utils/generateOAuthState.js index 2b5b681e66..51c1d3b479 100644 --- a/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/utils/generateOAuthState.js +++ b/samples/07.advanced-web-chat-apps/d.sso-for-teams/web/src/utils/generateOAuthState.js @@ -1,12 +1,14 @@ const crypto = require('crypto'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); + +// 5 bytes are 10 hex characters. +const STATE_BYTES = 5; + // For additional security, GitHub recommends using a "state" parameter that is only known to the servers and never passed to the client. // We are using a well-known salt to create the "state" parameter for distributed knowledge. +// scrypt is deterministic: every server with the same salt derives the same state. module.exports = function generateOAuthState(seed, salt) { - const hash = crypto.createHash('sha384'); - - hash.update(seed); - hash.update(salt); - - return hash.digest('hex').substr(0, 10); + return crypto.scryptSync(seed, salt, STATE_BYTES, SCRYPT_OPTIONS).toString('hex'); }; diff --git a/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js b/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js index 1aa0ad4dfb..3623b3d94f 100644 --- a/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js +++ b/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/routes/aad/oauth/createPKCECodeVerifier.js @@ -1,15 +1,14 @@ -const { createHash } = require('crypto'); -const encodeBase64URL = require('../../../encodeBase64URL'); +const { scryptSync } = require('crypto'); const { AAD_OAUTH_PKCE_SALT } = process.env; -// Creating a PKCE code verifier based on salt and seed. -// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. -module.exports = seed => { - const hash = createHash('sha512'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); - hash.update(seed); - hash.update(AAD_OAUTH_PKCE_SALT); +// 64 bytes are 86 base64url characters, within the 43–128 characters required by RFC 7636. +const VERIFIER_BYTES = 64; - return encodeBase64URL(hash.digest()); -}; +// Creating a PKCE code verifier based on salt and seed. +// We are using seed and salt to simplify the creation of PKCE code in a distributed manner. +// scrypt is deterministic: every server with the same salt derives the same verifier. +module.exports = seed => scryptSync(seed, AAD_OAUTH_PKCE_SALT, VERIFIER_BYTES, SCRYPT_OPTIONS).toString('base64url'); diff --git a/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/utils/generateOAuthState.js b/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/utils/generateOAuthState.js index 2b5b681e66..51c1d3b479 100644 --- a/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/utils/generateOAuthState.js +++ b/samples/07.advanced-web-chat-apps/e.sso-on-behalf-of-authentication/rest-api/src/utils/generateOAuthState.js @@ -1,12 +1,14 @@ const crypto = require('crypto'); +// scrypt cost, set explicitly so every server derives with the same cost. +const SCRYPT_OPTIONS = Object.freeze({ N: 16384 }); + +// 5 bytes are 10 hex characters. +const STATE_BYTES = 5; + // For additional security, GitHub recommends using a "state" parameter that is only known to the servers and never passed to the client. // We are using a well-known salt to create the "state" parameter for distributed knowledge. +// scrypt is deterministic: every server with the same salt derives the same state. module.exports = function generateOAuthState(seed, salt) { - const hash = crypto.createHash('sha384'); - - hash.update(seed); - hash.update(salt); - - return hash.digest('hex').substr(0, 10); + return crypto.scryptSync(seed, salt, STATE_BYTES, SCRYPT_OPTIONS).toString('hex'); }; From 23b2715fb92ef6c918984162c64b6e62ceb7b711 Mon Sep 17 00:00:00 2001 From: Eugene Olonov Date: Thu, 3 Dec 2026 11:19:03 -0800 Subject: [PATCH 2/2] Add the changelog fragment --- .changelog/6097-fix-codeql-sso-samples-hash.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/6097-fix-codeql-sso-samples-hash.md diff --git a/.changelog/6097-fix-codeql-sso-samples-hash.md b/.changelog/6097-fix-codeql-sso-samples-hash.md new file mode 100644 index 0000000000..cd88d1294a --- /dev/null +++ b/.changelog/6097-fix-codeql-sso-samples-hash.md @@ -0,0 +1,3 @@ +### Fixed + +- 🏫 Fixed SSO samples deriving the PKCE verifier and OAuth state with a fast hash, in PR [#6097](https://github.com/microsoft/BotFramework-WebChat/pull/6097), by [@OEvgeny](https://github.com/OEvgeny)