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) 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'); };