Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
dont allow oauth token exchange on user flow and dont allow http other than localhost
Thiago Araujo da Silva72e7b19a5464
diff --git a/backend/src/server/routes/v1/oauth-router.ts b/backend/src/server/routes/v1/oauth-router.tsindex 52c37dd07e..75023ade2c 100644--- a/backend/src/server/routes/v1/oauth-router.ts+++ b/backend/src/server/routes/v1/oauth-router.ts@@ -6,7 +6,7 @@ import { BadRequestError } from "@app/lib/errors";import { authRateLimit, readLimit, writeLimit } from "@app/server/config/rateLimiter";import { verifyAuth } from "@app/server/plugins/auth/verify-auth";import { AuthMode } from "@app/services/auth/auth-type";-import { parseBasicAuthHeader } from "@app/services/oauth-client/oauth-client-fns";+import { isAllowedRedirectUri, parseBasicAuthHeader } from "@app/services/oauth-client/oauth-client-fns";const SanitizedOauthClientSchema = OauthClientsSchema.omit({ clientSecretHash: true });@@ -14,8 +14,8 @@ const redirectUriSchema = z.string().url().refine(- (uri) => uri.startsWith("https://") || uri.startsWith("http://"),- "Redirect URI must start with https:// or http://"+ isAllowedRedirectUri,+ "Redirect URI must use https:// (http:// is only allowed for loopback addresses such as localhost)");export const registerOAuthRouter = async (server: FastifyZodProvider) => {diff --git a/backend/src/services/auth-token/auth-token-service.ts b/backend/src/services/auth-token/auth-token-service.tsindex d36581f6d0..1d2929872e 100644--- a/backend/src/services/auth-token/auth-token-service.ts+++ b/backend/src/services/auth-token/auth-token-service.ts@@ -196,7 +196,7 @@ export const tokenServiceFactory = ({ tokenDAL, userDAL, orgDAL, keyStore }: TAuconst revokeMySessionById = async (userId: string, sessionId: string) =>tokenDAL.deleteTokenSession({ userId, id: sessionId });- const validateRefreshToken = async (refreshToken?: string) => {+ const validateRefreshToken = async (refreshToken?: string, opts?: { allowOauthClientToken?: boolean }) => {const appCfg = getConfig();if (!refreshToken) {throw new NotFoundError({@@ -214,6 +214,13 @@ export const tokenServiceFactory = ({ tokenDAL, userDAL, orgDAL, keyStore }: TAu});}+ if (decodedToken.oauthClientId && !opts?.allowOauthClientToken) {+ throw new UnauthorizedError({+ message: "This refresh token can only be used at the OAuth token endpoint",+ name: "InvalidToken"+ });+ }+const tokenVersion = await getUserTokenSessionById(decodedToken.tokenVersionId, decodedToken.userId);if (!tokenVersion) {diff --git a/backend/src/services/auth/auth-type.ts b/backend/src/services/auth/auth-type.tsindex c9b6c65746..92e4b84eba 100644--- a/backend/src/services/auth/auth-type.ts+++ b/backend/src/services/auth/auth-type.ts@@ -113,6 +113,7 @@ export type AuthModeRefreshJwtTokenPayload = {subOrganizationId?: string;isMfaVerified?: boolean;mfaMethod?: MfaMethod;+ oauthClientId?: string;};export type AuthModeSignUpTokenPayload = {diff --git a/backend/src/services/oauth-client/oauth-client-fns.test.ts b/backend/src/services/oauth-client/oauth-client-fns.test.tsindex e050693bfc..217b776ed8 100644--- a/backend/src/services/oauth-client/oauth-client-fns.test.ts+++ b/backend/src/services/oauth-client/oauth-client-fns.test.ts@@ -1,6 +1,11 @@import crypto from "node:crypto";-import { computePkceChallenge, isRegisteredRedirectUri, parseBasicAuthHeader } from "./oauth-client-fns";+import {+ computePkceChallenge,+ isAllowedRedirectUri,+ isRegisteredRedirectUri,+ parseBasicAuthHeader+} from "./oauth-client-fns";describe("parseBasicAuthHeader", () => {test("parses a valid Basic auth header", () => {@@ -46,6 +51,41 @@ describe("computePkceChallenge", () => {});});+describe("isAllowedRedirectUri", () => {+ test("allows https for any host", () => {+ expect(isAllowedRedirectUri("https://coder.example.com/callback")).toBe(true);+ });++ test("allows http for localhost", () => {+ expect(isAllowedRedirectUri("http://localhost:3000/callback")).toBe(true);+ });++ test("allows http for 127.0.0.1", () => {+ expect(isAllowedRedirectUri("http://127.0.0.1:8080/callback")).toBe(true);+ });++ test("allows http for IPv6 loopback", () => {+ expect(isAllowedRedirectUri("http://[::1]:8080/callback")).toBe(true);+ });++ test("rejects http for a non-loopback host", () => {+ expect(isAllowedRedirectUri("http://coder.example.com/callback")).toBe(false);+ });++ test("rejects http for a host that merely contains localhost", () => {+ expect(isAllowedRedirectUri("http://localhost.evil.com/callback")).toBe(false);+ });++ test("rejects non-http(s) schemes", () => {+ expect(isAllowedRedirectUri("ftp://localhost/callback")).toBe(false);+ expect(isAllowedRedirectUri("javascript:alert(1)")).toBe(false);+ });++ test("rejects malformed URIs", () => {+ expect(isAllowedRedirectUri("not a url")).toBe(false);+ });+});+describe("isRegisteredRedirectUri", () => {test("matches exact URI", () => {expect(diff --git a/backend/src/services/oauth-client/oauth-client-fns.ts b/backend/src/services/oauth-client/oauth-client-fns.tsindex 4299086575..acb0400df8 100644--- a/backend/src/services/oauth-client/oauth-client-fns.ts+++ b/backend/src/services/oauth-client/oauth-client-fns.ts@@ -33,6 +33,29 @@ export const computePkceChallenge = (codeVerifier: string) => {return Buffer.from(sha256).toString("base64url");};+const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);++// A redirect URI may use https:// for any host. Plaintext http:// is only permitted for loopback+// addresses (local development), per RFC 8252 / the OAuth 2.0 security BCP. Allowing http:// to an+// arbitrary host would expose authorization codes and tokens to network interception.+export const isAllowedRedirectUri = (uri: string) => {+ let parsed: URL;+ try {+ parsed = new URL(uri);+ } catch {+ return false;+ }++ if (parsed.protocol === "https:") return true;++ if (parsed.protocol === "http:") {+ const hostname = parsed.hostname.replace(/^\[|\]$/g, ""); // strip brackets from IPv6 literals+ return LOOPBACK_HOSTNAMES.has(hostname);+ }++ return false;+};+export const isRegisteredRedirectUri = (registeredUris: string[], redirectUri: string) =>registeredUris.some((uri) => {if (uri === redirectUri) return true;diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.tsindex 6400e58282..789c4196a0 100644--- a/backend/src/services/oauth-client/oauth-client-service.ts+++ b/backend/src/services/oauth-client/oauth-client-service.ts@@ -361,7 +361,9 @@ export const oauthClientServiceFactory = ({};}- const { decodedToken, tokenVersion, isGraceHit } = await tokenService.validateRefreshToken(dto.refreshToken);+ const { decodedToken, tokenVersion, isGraceHit } = await tokenService.validateRefreshToken(dto.refreshToken, {+ allowOauthClientToken: true+ });const oauthDecodedToken = decodedToken as TOauthRefreshJwtTokenPayload;if (oauthDecodedToken.oauthClientId !== client.clientId) {diff --git a/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx b/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsxindex ef19d67863..8aacad7b66 100644--- a/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx+++ b/frontend/src/pages/organization/SettingsPage/components/OrgOauthClientsTab/OauthClientModal.tsx@@ -25,10 +25,16 @@ import {import { TOauthClient, useCreateOauthClient, useUpdateOauthClient } from "@app/hooks/api";import { UsePopUpState } from "@app/hooks/usePopUp";+const LOOPBACK_HOSTNAMES = new Set(["localhost", "127.0.0.1", "::1"]);+const isValidRedirectUri = (uri: string) => {try {const parsed = new URL(uri);- return parsed.protocol === "https:" || parsed.protocol === "http:";+ if (parsed.protocol === "https:") return true;+ if (parsed.protocol === "http:") {+ return LOOPBACK_HOSTNAMES.has(parsed.hostname.replace(/^\[|\]$/g, ""));+ }+ return false;} catch {return false;}