Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions apps/server/src/auth/EnvironmentAuth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@t3tools/contracts";
import { expect, it } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import * as Deferred from "effect/Deferred";
import { onTestFinished, vi } from "vite-plus/test";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
Expand Down Expand Up @@ -278,13 +279,14 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))(
),
);

it.effect("authenticates requests without credentials through WebSocket setup", () => {
it.effect("authenticates requests without credentials and never ends that session", () => {
vi.stubEnv("T3CODE_UNSAFE_NO_AUTH", "1");
onTestFinished(() => {
vi.unstubAllEnvs();
});
return Effect.gen(function* () {
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
const sessions = yield* SessionStore.SessionStore;
const request = { cookies: {}, headers: {} };
const session = yield* serverAuth.authenticateHttpRequest(request as never);
const ticket = yield* serverAuth.issueWebSocketTicket(session);
Expand All @@ -299,7 +301,20 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodePlatformCrypto.layer))(
expect(session.subject).toBe("unsafe-no-auth");
expect(session.scopes).toEqual(AuthStandardClientScopes);
expect(websocketSession).toEqual(session);
}).pipe(Effect.provide(layerEnvironmentAuth()));

const unsafeInvalidated = yield* Deferred.make<void>();
yield* sessions
.awaitInvalidation(session.sessionId)
.pipe(
Effect.exit,
Effect.andThen(Deferred.succeed(unsafeInvalidated, undefined)),
Effect.forkScoped({ startImmediately: true }),
);
const control = yield* sessions.issue({ subject: "control" });
yield* sessions.revoke(control.sessionId);
yield* sessions.awaitInvalidation(control.sessionId);
expect(yield* Deferred.isDone(unsafeInvalidated)).toBe(false);
}).pipe(Effect.scoped, Effect.provide(layerEnvironmentAuth()));
});

it.effect("prefers a bearer token over a stale legacy cookie", () =>
Expand Down
8 changes: 2 additions & 6 deletions apps/server/src/auth/EnvironmentAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
type AuthEnvironmentScope,
type AuthPairingLink,
type AuthPairingCredentialResult,
AuthSessionId,
type AuthSessionId,
type AuthSessionState,
authScopeResponse,
type ServerAuthDescriptor,
Expand Down Expand Up @@ -63,16 +63,12 @@ export const INTERNAL_ADMINISTRATIVE_BOOTSTRAP_SUBJECT = "administrative-bootstr
export const CLOUD_CONNECT_SUBJECT = "cloud-connect";

const unsafeSession: AuthenticatedSession & { readonly audience?: undefined } = {
sessionId: AuthSessionId.make("unsafe-no-auth"),
sessionId: SessionStore.UNSAFE_NO_AUTH_SESSION_ID,
subject: "unsafe-no-auth",
method: "browser-session-cookie",
scopes: AuthStandardClientScopes,
};

/** The no-auth session has no stored row, so nothing can revoke it. */
export const isUnsafeNoAuthSession = (sessionId: AuthSessionId) =>
sessionId === unsafeSession.sessionId;

export interface IssuedPairingLink {
readonly id: string;
readonly credential: string;
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/auth/SessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ export class SessionStore extends Context.Service<
}
>()("t3/auth/SessionStore") {}

/** The session `T3CODE_UNSAFE_NO_AUTH` grants. */
export const UNSAFE_NO_AUTH_SESSION_ID = AuthSessionId.make("unsafe-no-auth");

const SIGNING_SECRET_NAME = "server-signing-key";
const DEFAULT_SESSION_TTL = Duration.days(30);
const DEFAULT_WEBSOCKET_TOKEN_TTL = Duration.minutes(5);
Expand Down Expand Up @@ -1208,6 +1211,8 @@ const make = Effect.gen(function* () {
sessionId: AuthSessionId,
proofKeyThumbprint?: string,
) {
// It has no stored row, which the read below takes as revoked.
if (sessionId === UNSAFE_NO_AUTH_SESSION_ID) return yield* Effect.never;
return yield* Effect.scoped(
Effect.gen(function* () {
// Subscribe before reading: revocation can race with the WebSocket upgrade.
Expand Down
22 changes: 10 additions & 12 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3757,18 +3757,16 @@ export const layer = Layer.unwrap(
return yield* Effect.acquireUseRelease(
sessions.markConnected(session.sessionId),
() =>
EnvironmentAuth.isUnsafeNoAuthSession(session.sessionId)
? framedRpcWebSocketHttpEffect
: Effect.raceFirst(
framedRpcWebSocketHttpEffect,
sessions.awaitInvalidation(session.sessionId, session.proofKeyThumbprint).pipe(
Effect.as(HttpServerResponse.empty()),
Effect.catchTags({
SessionCredentialVerificationError: (error) =>
failEnvironmentInternal("internal_error", error),
}),
),
),
Effect.raceFirst(
framedRpcWebSocketHttpEffect,
sessions.awaitInvalidation(session.sessionId, session.proofKeyThumbprint).pipe(
Effect.as(HttpServerResponse.empty()),
Effect.catchTags({
SessionCredentialVerificationError: (error) =>
failEnvironmentInternal("internal_error", error),
}),
),
),
() => sessions.markDisconnected(session.sessionId),
);
}).pipe(
Expand Down
2 changes: 1 addition & 1 deletion fork/whiteboard/run-t3.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Restart after a rebuild with `systemctl --user restart whiteboard-chat`; read it

The fork's T3 Code differs from upstream in the points below for the whiteboard's sake. Keep this list current when merging upstream. Server points are switched by environment variables that `run-t3.sh` sets. Web points are switched at build time by `VITE_T3_WHITEBOARD=1`, read by `apps/web/vite/fork/whiteboardBuild.ts` (Vite) and `apps/web/src/fork/whiteboard/build.ts` (client code; `isEmbeddedInWhiteboard()` adds the check that the page is inside the board's iframe). Whiteboard-only client code lives in `apps/web/src/fork/whiteboard/`; upstream files hold only the call or import.

- No login, behind a switch. With `T3CODE_UNSAFE_NO_AUTH=1` (set by `run-t3.sh`), requests without a credential, or with a rejected one, get a session with the standard client scopes (`unsafe-no-auth`; no admin scopes such as `review:write`) instead of a 401, websocket tickets issued to that session are accepted, and its websocket does not wait for revocation (the session has no stored row, so upstream's revocation check would end the socket at once). Without the variable upstream auth applies unchanged, so upstream's auth tests stay as they are. Files: `apps/server/src/auth/EnvironmentAuth.ts`, `apps/server/src/ws.ts` (one added test in `EnvironmentAuth.test.ts`).
- No login, behind a switch. With `T3CODE_UNSAFE_NO_AUTH=1` (set by `run-t3.sh`), requests without a credential, or with a rejected one, get a session with the standard client scopes (`unsafe-no-auth`; no admin scopes such as `review:write`) instead of a 401, websocket tickets issued to that session are accepted, and `SessionStore.awaitInvalidation` never completes for it, so no socket or stream ends for its revocation (the session has no stored row, which upstream's check reads as revoked). Without the variable upstream auth applies unchanged, so upstream's auth tests stay as they are. Files: `apps/server/src/auth/EnvironmentAuth.ts`, `apps/server/src/auth/SessionStore.ts` (one added test in `EnvironmentAuth.test.ts`).
- Framing from the same origin, behind a switch. With `T3CODE_ALLOW_SAME_ORIGIN_FRAME=1` (set by `run-t3.sh`) the app page is sent with `Content-Security-Policy: frame-ancestors 'self'` and `X-Frame-Options: SAMEORIGIN`, so the board page can frame `/chat/` and other sites stay refused. Without the variable upstream's headers apply unchanged (`frame-ancestors 'none'`, `DENY`), so upstream's test stays as it is. Files: `apps/server/src/fork/sameOriginFrame.ts` (with test), `apps/server/src/http.ts` (the call).
- Whiteboard skills for agents. `T3CODE_CLAUDE_PLUGIN_ROOT` is loaded as a local Claude plugin; `T3CODE_CODEX_SKILL_ROOT` is registered with each Codex app-server through `skills/extraRoots/set`. Files: `apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts` (`makeClaudeQueryOptions`, with test), `CodexAdapterV2.ts` (after `initialize`), `apps/server/src/provider/codexLaunchArgs.ts` (`resolveCodexSkillRoot`, with test), `apps/server/src/provider/CodexProvider.ts` (probe client). `run-t3.sh` sets both variables, puts `tools/bin` on `PATH`, and sets `BASH_ENV` to `tools/agent-env.sh`.
- Served under `/chat/` (whiteboard build). The Vite plugin sets `base: "/chat/"` and recolours `manifest.webmanifest` from `#161616` to the board page's `#0a0a0a`; the router gets `basepath: "/chat"`. The manifest's icon paths are relative in every build. Files: `apps/web/vite/fork/whiteboardBuild.ts`, `apps/web/vite.config.ts` (plugin entry), `apps/web/src/router.ts`, `apps/web/public/manifest.webmanifest`.
Expand Down
Loading