@@ -44,6 +44,7 @@ import * as ServerSecretStore from "./ServerSecretStore.ts";
4444import * as SessionStore from "./SessionStore.ts" ;
4545import { REUSABLE_DEV_SESSION_EXPIRES_AT , resolveReusableDevAuth } from "./ReusableDevAuth.ts" ;
4646import { verifyRequestDpopProof } from "./dpop.ts" ;
47+ import { DESKTOP_RENDERER_ORIGINS } from "../httpCors.ts" ;
4748import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
4849
4950const DEFAULT_SESSION_SUBJECT = "cli-issued-session" ;
@@ -687,6 +688,47 @@ export function selectRequestCredential(
687688 return undefined ;
688689}
689690
691+ type RequestCredentialSource = NonNullable < ReturnType < typeof selectRequestCredential > > [ "source" ] ;
692+
693+ /** Whether a credential came from a browser cookie rather than an Authorization header. */
694+ export const isBrowserCookieCredential = ( source : RequestCredentialSource | "dev-cookie" ) =>
695+ source === "cookie" || source === "legacy-cookie" || source === "dev-cookie" ;
696+
697+ function firstHeaderValue ( value : string | undefined ) : string | undefined {
698+ const first = value ?. split ( "," ) [ 0 ] ?. trim ( ) . toLowerCase ( ) ;
699+ return first && first . length > 0 ? first : undefined ;
700+ }
701+
702+ /**
703+ * Whether a cookie-carrying request comes from a page allowed to use it.
704+ * Browsers send `Origin` on WebSocket upgrades, cross-origin requests, and
705+ * non-GET requests, so an absent header means a same-origin read or a
706+ * non-browser client. Otherwise the request must be same-origin by the
707+ * browser's own `Sec-Fetch-Site` (which survives the dev proxy rewriting
708+ * `Host`), come from this server's own host (directly or behind a proxy that
709+ * reports `X-Forwarded-Host`), or come from one of the trusted app origins.
710+ */
711+ function isBrowserOriginAllowed (
712+ request : HttpServerRequest . HttpServerRequest ,
713+ trustedOrigins : ReadonlySet < string > ,
714+ ) : boolean {
715+ const origin = request . headers . origin ;
716+ if ( origin === undefined ) return true ;
717+ if ( request . headers [ "sec-fetch-site" ] === "same-origin" ) return true ;
718+ if ( trustedOrigins . has ( origin ) ) return true ;
719+ let originHost : string ;
720+ try {
721+ originHost = new URL ( origin ) . host . toLowerCase ( ) ;
722+ } catch {
723+ return false ;
724+ }
725+ if ( originHost === "" ) return false ;
726+ return (
727+ originHost === firstHeaderValue ( request . headers . host ) ||
728+ originHost === firstHeaderValue ( request . headers [ "x-forwarded-host" ] )
729+ ) ;
730+ }
731+
690732/** @public Service construction is part of the canonical Effect module API. */
691733export const make = Effect . gen ( function * ( ) {
692734 const policy = yield * EnvironmentAuthPolicy . EnvironmentAuthPolicy ;
@@ -726,25 +768,49 @@ export const make = Effect.gen(function* () {
726768 } ) ) ,
727769 ) ;
728770
729- const authenticateRequest = (
730- request : HttpServerRequest . HttpServerRequest ,
731- ) : Effect . Effect < AuthenticatedSession , ServerAuthCredentialError | ServerAuthInternalError > => {
771+ const resolveRequestCredential = ( request : HttpServerRequest . HttpServerRequest ) => {
732772 const selectedCredential = selectRequestCredential (
733773 request ,
734774 sessions . cookieName ,
735775 sessions . legacyCookieName ,
736776 ) ;
737- const dpopToken = parseDpopToken ( request ) ;
738777 const hasAuthorization = request . headers . authorization !== undefined ;
739778 const devCookieToken = devAuth ? request . cookies [ devAuth . cookieName ] : undefined ;
740- const credential =
779+ return (
741780 selectedCredential ??
742781 ( ! hasAuthorization && devCookieToken !== undefined
743782 ? { token : devCookieToken , source : "dev-cookie" as const }
744- : undefined ) ;
783+ : undefined )
784+ ) ;
785+ } ;
786+
787+ // Pages the server or its dev setup serves itself. Other origins on the same
788+ // site (another localhost port, a LAN IP's other ports) still get the cookie,
789+ // so cookie sessions must not authenticate their requests or sockets.
790+ const trustedBrowserOrigins = new Set < string > ( [
791+ ...( config . devUrl ? [ config . devUrl . origin ] : [ ] ) ,
792+ ...DESKTOP_RENDERER_ORIGINS ,
793+ ...config . devAllowedOrigins ,
794+ ] ) ;
795+
796+ const authenticateRequest = (
797+ request : HttpServerRequest . HttpServerRequest ,
798+ ) : Effect . Effect < AuthenticatedSession , ServerAuthCredentialError | ServerAuthInternalError > => {
799+ const dpopToken = parseDpopToken ( request ) ;
800+ const credential = resolveRequestCredential ( request ) ;
745801 if ( ! credential ?. token ) {
746802 return Effect . fail ( new ServerAuthMissingCredentialError ( { } ) ) ;
747803 }
804+ if (
805+ isBrowserCookieCredential ( credential . source ) &&
806+ ! isBrowserOriginAllowed ( request , trustedBrowserOrigins )
807+ ) {
808+ return Effect . fail (
809+ new ServerAuthInvalidCredentialError ( {
810+ diagnostic : "Browser sessions are only accepted from the app's own origin." ,
811+ } ) ,
812+ ) ;
813+ }
748814 return authenticateToken ( credential . token ) . pipe (
749815 Effect . flatMap ( ( session ) => {
750816 if ( session . proofKeyThumbprint ) {
@@ -1248,14 +1314,13 @@ export const make = Effect.gen(function* () {
12481314 const authenticateBrowserSession : EnvironmentAuth [ "Service" ] [ "authenticateBrowserSession" ] = (
12491315 request ,
12501316 ) => {
1251- const token =
1252- request . cookies [ sessions . cookieName ] ??
1253- ( sessions . legacyCookieName ? request . cookies [ sessions . legacyCookieName ] : undefined ) ??
1254- ( devAuth ? request . cookies [ devAuth . cookieName ] : undefined ) ;
1255- if ( ! token ) return Effect . fail ( new ServerAuthMissingCredentialError ( { } ) ) ;
1256- return authenticateToken ( token ) . pipe (
1317+ const credential = resolveRequestCredential ( request ) ;
1318+ if ( ! credential || ! isBrowserCookieCredential ( credential . source ) ) {
1319+ return Effect . fail ( new ServerAuthMissingCredentialError ( { } ) ) ;
1320+ }
1321+ return authenticateRequest ( request ) . pipe (
12571322 Effect . filterOrFail (
1258- ( session ) => session . method === "browser-session-cookie" && ! session . proofKeyThumbprint ,
1323+ ( session ) => session . method === "browser-session-cookie" ,
12591324 ( ) => new ServerAuthInvalidCredentialError ( { diagnostic : "Not a browser session." } ) ,
12601325 ) ,
12611326 Effect . withSpan ( "EnvironmentAuth.authenticateBrowserSession" ) ,
0 commit comments