-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Deploy hosted web app from release workflow #2507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
4
commits into
main
Choose a base branch
from
t3code/1be1f71d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb1a299
Deploy hosted web app during releases
juliusmarminge 6e88ff4
Show hosted app channel in web About settings
juliusmarminge f1e0c30
Add hosted app channel selector
juliusmarminge ad418ab
Harden hosted channel routing and next-path validation
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| HOSTED_WEB_CHANNEL_COOKIE, | ||
| isRouterHost, | ||
| normalizeChannel, | ||
| parseCookieValue, | ||
| selectChannel, | ||
| } from "./middleware"; | ||
|
|
||
| function request(path: string, cookie?: string): Request { | ||
| return new Request(`https://app.t3.codes${path}`, { | ||
| headers: cookie ? { cookie } : undefined, | ||
| }); | ||
| } | ||
|
|
||
| describe("hosted web channel middleware", () => { | ||
| it("normalizes latest and nightly channel names", () => { | ||
| expect(normalizeChannel("latest")).toBe("latest"); | ||
| expect(normalizeChannel("nightly")).toBe("nightly"); | ||
| expect(normalizeChannel("mytube")).toBeNull(); | ||
| expect(normalizeChannel("unknown")).toBeNull(); | ||
| }); | ||
|
|
||
| it("matches the configured router host without a port", () => { | ||
| expect(isRouterHost("app.t3.codes:443", "app.t3.codes")).toBe(true); | ||
| expect(isRouterHost("app.t3.codes", "app.t3.codes:443")).toBe(true); | ||
| expect(isRouterHost("latest.app.t3.codes", "app.t3.codes")).toBe(false); | ||
| }); | ||
|
|
||
| it("reads the selected channel from cookies", () => { | ||
| expect( | ||
| selectChannel(request("/settings", `theme=dark; ${HOSTED_WEB_CHANNEL_COOKIE}=nightly`)), | ||
| ).toEqual({ | ||
| channel: "nightly", | ||
| setCookie: false, | ||
| nextPath: "/settings", | ||
| }); | ||
| }); | ||
|
|
||
| it("defaults invalid or missing channel cookies to latest", () => { | ||
| expect(selectChannel(request("/threads", `${HOSTED_WEB_CHANNEL_COOKIE}=bad`))).toEqual({ | ||
| channel: "latest", | ||
| setCookie: false, | ||
| nextPath: "/threads", | ||
| }); | ||
| }); | ||
|
|
||
| it("handles channel opt-in requests with an internal next path only", () => { | ||
| expect(selectChannel(request("/__t3code/channel?channel=nightly&next=/pair"))).toEqual({ | ||
| channel: "nightly", | ||
| setCookie: true, | ||
| nextPath: "/pair", | ||
| }); | ||
|
|
||
| expect( | ||
| selectChannel(request("/__t3code/channel?channel=latest&next=https://evil.example")), | ||
| ).toEqual({ | ||
| channel: "latest", | ||
| setCookie: true, | ||
| nextPath: "/", | ||
| }); | ||
|
|
||
| expect(selectChannel(request("/__t3code/channel?channel=latest&next=/\\evil.example"))).toEqual( | ||
| { | ||
| channel: "latest", | ||
| setCookie: true, | ||
| nextPath: "/", | ||
| }, | ||
| ); | ||
|
|
||
| expect( | ||
| selectChannel(request("/__t3code/channel?channel=latest&next=/settings%3Adebug")), | ||
| ).toEqual({ | ||
| channel: "latest", | ||
| setCookie: true, | ||
| nextPath: "/", | ||
| }); | ||
|
|
||
| expect( | ||
| selectChannel(request("/__t3code/channel?channel=latest&next=/settings%0Adebug")), | ||
| ).toEqual({ | ||
| channel: "latest", | ||
| setCookie: true, | ||
| nextPath: "/", | ||
| }); | ||
| }); | ||
|
|
||
| it("parses cookie values by exact name", () => { | ||
| expect(parseCookieValue("other=value; t3code_web_channel=nightly", "t3code_web_channel")).toBe( | ||
| "nightly", | ||
| ); | ||
| expect(parseCookieValue("x-t3code_web_channel=nightly", "t3code_web_channel")).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { next, rewrite } from "@vercel/functions"; | ||
|
|
||
| export type HostedWebChannel = "latest" | "nightly"; | ||
|
|
||
| export const HOSTED_WEB_CHANNEL_COOKIE = "t3code_web_channel"; | ||
|
|
||
| const DEFAULT_ROUTER_HOST = "app.t3.codes"; | ||
| const DEFAULT_CHANNEL_ORIGINS = { | ||
| latest: "https://latest.app.t3.codes", | ||
| nightly: "https://nightly.app.t3.codes", | ||
| } as const satisfies Record<HostedWebChannel, string>; | ||
|
|
||
| export interface ChannelRouterConfig { | ||
| readonly routerHost: string; | ||
| readonly channelOrigins: Record<HostedWebChannel, string>; | ||
| } | ||
|
|
||
| export interface ChannelSelection { | ||
| readonly channel: HostedWebChannel; | ||
| readonly setCookie: boolean; | ||
| readonly nextPath: string; | ||
| } | ||
|
|
||
| function envValue(name: string): string | undefined { | ||
| const value = process.env[name]?.trim(); | ||
| return value ? value : undefined; | ||
| } | ||
|
|
||
| export function readChannelRouterConfig(): ChannelRouterConfig { | ||
| return { | ||
| routerHost: envValue("T3CODE_WEB_ROUTER_HOST") ?? DEFAULT_ROUTER_HOST, | ||
| channelOrigins: { | ||
| latest: envValue("T3CODE_WEB_LATEST_ORIGIN") ?? DEFAULT_CHANNEL_ORIGINS.latest, | ||
| nightly: envValue("T3CODE_WEB_NIGHTLY_ORIGIN") ?? DEFAULT_CHANNEL_ORIGINS.nightly, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export function normalizeChannel(value: string | null | undefined): HostedWebChannel | null { | ||
| const normalized = value?.trim().toLowerCase(); | ||
| if (normalized === "latest") return "latest"; | ||
| if (normalized === "nightly") return "nightly"; | ||
| return null; | ||
| } | ||
|
|
||
| export function parseCookieValue(cookieHeader: string | null, name: string): string | null { | ||
| if (!cookieHeader) return null; | ||
|
|
||
| for (const segment of cookieHeader.split(";")) { | ||
| const [rawKey, ...rawValue] = segment.split("="); | ||
| if (rawKey?.trim() !== name) continue; | ||
| return rawValue.join("=").trim() || null; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function normalizeHost(value: string | null): string | null { | ||
| const host = value?.split(":")[0]?.trim().toLowerCase(); | ||
| return host ? host : null; | ||
| } | ||
|
|
||
| export function isRouterHost(hostHeader: string | null, routerHost: string): boolean { | ||
| const host = normalizeHost(hostHeader); | ||
| const router = normalizeHost(routerHost); | ||
| return host !== null && host === router; | ||
| } | ||
|
|
||
| function hasControlCharacter(value: string): boolean { | ||
| for (const char of value) { | ||
| const code = char.charCodeAt(0); | ||
| if (code <= 0x1f || code === 0x7f) return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function safeNextPath(value: string | null): string { | ||
| if ( | ||
| !value?.startsWith("/") || | ||
| value.startsWith("//") || | ||
| value.includes("\\") || | ||
| value.includes(":") || | ||
| hasControlCharacter(value) | ||
| ) { | ||
| return "/"; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| export function selectChannel(request: Request): ChannelSelection { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === "/__t3code/channel") { | ||
| return { | ||
| channel: normalizeChannel(url.searchParams.get("channel")) ?? "latest", | ||
| setCookie: true, | ||
| nextPath: safeNextPath(url.searchParams.get("next")), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| channel: | ||
| normalizeChannel( | ||
| parseCookieValue(request.headers.get("cookie"), HOSTED_WEB_CHANNEL_COOKIE), | ||
| ) ?? "latest", | ||
| setCookie: false, | ||
| nextPath: `${url.pathname}${url.search}`, | ||
| }; | ||
| } | ||
|
|
||
| function channelCookie(channel: HostedWebChannel): string { | ||
| return [ | ||
| `${HOSTED_WEB_CHANNEL_COOKIE}=${channel}`, | ||
| "Path=/", | ||
| "Max-Age=31536000", | ||
| "HttpOnly", | ||
| "Secure", | ||
| "SameSite=Lax", | ||
| ].join("; "); | ||
| } | ||
|
|
||
| function buildRewriteUrl(request: Request, origin: string): URL { | ||
| const requestUrl = new URL(request.url); | ||
| const target = new URL(origin); | ||
| target.pathname = requestUrl.pathname; | ||
| target.search = requestUrl.search; | ||
| target.hash = ""; | ||
| return target; | ||
| } | ||
|
|
||
| export const config = { | ||
| matcher: "/:path*", | ||
| }; | ||
|
|
||
| export default function middleware(request: Request): Response { | ||
| const routerConfig = readChannelRouterConfig(); | ||
| if (!isRouterHost(request.headers.get("host"), routerConfig.routerHost)) { | ||
| return next(); | ||
| } | ||
|
|
||
| const selection = selectChannel(request); | ||
|
|
||
| if (selection.setCookie) { | ||
| return new Response(null, { | ||
| status: 302, | ||
| headers: { | ||
| Location: selection.nextPath, | ||
| "Set-Cookie": channelCookie(selection.channel), | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| return rewrite(buildRewriteUrl(request, routerConfig.channelOrigins[selection.channel])); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.