The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
feat(website): keep a /desktop link's utm_* and rid for the sign-in
0xken157fbfaf04cbbc
diff --git a/website/src/lib/signInAttribution.spec.ts b/website/src/lib/signInAttribution.spec.tsnew file mode 100644index 000000000..c15bf4648--- /dev/null+++ b/website/src/lib/signInAttribution.spec.ts@@ -0,0 +1,63 @@+import { describe, expect, it } from "vitest"+import { ATTRIBUTION_MAX_AGE, attributionCookies } from "./signInAttribution"++const LIVE = { hostname: "harness.autonomous.ai", https: true }++const written = (cookies: ReturnType<typeof attributionCookies>) =>+ Object.fromEntries(cookies.map((c) => [c.name, c.maxAge ? c.value : null]))++describe("attributionCookies", () => {+ it("keeps a tagged link's utm_* and rid where auth.autonomous.ai reads them", () => {+ const cookies = attributionCookies(new URLSearchParams("utm_source=x&utm_medium=social&utm_campaign=launch&rid=r1"), LIVE)++ expect(written(cookies)).toEqual({+ utm_source: "x",+ utm_medium: "social",+ utm_campaign: "launch",+ utm_term: null,+ utm_content: null,+ rid: "r1",+ })+ // The parent domain: auth.autonomous.ai cannot see a cookie that belongs to this host alone.+ expect(cookies.every((c) => c.domain === ".autonomous.ai" && c.path === "/" && c.secure && !c.httpOnly)).toBe(true)+ expect(cookies.find((c) => c.name === "rid")?.maxAge).toBe(ATTRIBUTION_MAX_AGE)+ })++ it("lets the latest link replace the whole set, clearing a tag it did not carry", () => {+ // An older link's rid left beside a newer link's utm_source would credit the wrong referrer.+ const cookies = attributionCookies(new URLSearchParams("utm_source=newsletter"), LIVE)++ expect(cookies.filter((c) => c.maxAge === 0).map((c) => c.name)).toEqual([+ "utm_medium",+ "utm_campaign",+ "utm_term",+ "utm_content",+ "rid",+ ])+ })++ it("leaves the set alone for an untagged visit", () => {+ expect(attributionCookies(new URLSearchParams(""), LIVE)).toEqual([])+ expect(attributionCookies(new URLSearchParams("platform=mac&utm_source=%20%20"), LIVE)).toEqual([])+ })++ it("trims and caps each value like the backend does", () => {+ const cookies = attributionCookies(new URLSearchParams(`utm_source=%20x%20&rid=${"r".repeat(500)}`), LIVE)++ expect(cookies.find((c) => c.name === "utm_source")?.value).toBe("x")+ expect(cookies.find((c) => c.name === "rid")?.value).toHaveLength(128)+ })++ it("sets a host-only cookie off autonomous.ai, where the parent domain would be refused", () => {+ const cookies = attributionCookies(new URLSearchParams("rid=r1"), { hostname: "localhost", https: false })++ expect(cookies.every((c) => c.domain === undefined && !c.secure)).toBe(true)+ })++ it("names the parent domain for autonomous.ai itself and any of its hosts", () => {+ for (const hostname of ["autonomous.ai", "HARNESS.autonomous.ai", "local-harness.autonomous.ai"]) {+ expect(attributionCookies(new URLSearchParams("rid=r1"), { hostname, https: true })[0].domain).toBe(".autonomous.ai")+ }+ expect(attributionCookies(new URLSearchParams("rid=r1"), { hostname: "evilautonomous.ai", https: true })[0].domain).toBeUndefined()+ })+})diff --git a/website/src/lib/signInAttribution.ts b/website/src/lib/signInAttribution.tsnew file mode 100644index 000000000..0e7bb9cc9--- /dev/null+++ b/website/src/lib/signInAttribution.ts@@ -0,0 +1,66 @@+/**+ * Where a download came from, kept where the sign-in can find it.+ *+ * A person who lands on harness.autonomous.ai/desktop from a tagged link downloads a static+ * installer, so nothing in the app knows the link. The sign-in does: auth.autonomous.ai reads the+ * `utm_*` and `rid` cookies on `.autonomous.ai` and appends them to the redirect_uri, the desktop app's+ * `harness login` sends them with /api/auth/exchange, and the backend records them on the account+ * (backend lib/signInAttribution.ts). www.autonomous.ai/harness-app already sets those cookies; this+ * host sets the same ones, with the same names and lifetime, so a link to either page is attributed.+ *+ * Like www.autonomous.ai, a tagged visit replaces the whole set (the latest link wins, and a tag it+ * did not carry is cleared rather than left over from an older link) and an untagged visit leaves the+ * set alone.+ */+export const ATTRIBUTION_KEYS = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content", "rid"] as const++/** www.autonomous.ai keeps them for 30 days. */+export const ATTRIBUTION_MAX_AGE = 30 * 24 * 60 * 60++/** Same cap as the backend: anything longer is cut there anyway. */+const MAX_VALUE_LENGTH = 128++export interface AttributionCookie {+ name: (typeof ATTRIBUTION_KEYS)[number]+ /** Empty with `maxAge: 0` clears a tag the latest link did not carry. */+ value: string+ domain?: string+ path: "/"+ maxAge: number+ sameSite: "lax"+ secure: boolean+ // Not HttpOnly, like www.autonomous.ai's: the sign-in page may read them in the browser.+ httpOnly: false+}++/**+ * The parent domain a cookie must name for auth.autonomous.ai to see it, or none off that domain+ * (local development), where a host-only cookie is the most that can be set.+ */+function cookieDomain(hostname: string): string | undefined {+ return hostname === "autonomous.ai" || hostname.endsWith(".autonomous.ai") ? ".autonomous.ai" : undefined+}++/** The host the person reached, as the browser saw it: the public name and scheme, not the pod's. */+export interface SiteOrigin {+ hostname: string+ https: boolean+}++/** The cookies to write for a request with [query] on [site], or none when it carries no tag. */+export function attributionCookies(query: URLSearchParams, site: SiteOrigin): AttributionCookie[] {+ const values = ATTRIBUTION_KEYS.map((key) => [key, query.get(key)?.trim().slice(0, MAX_VALUE_LENGTH) ?? ""] as const)+ if (!values.some(([, value]) => value)) return []+ const domain = cookieDomain(site.hostname.toLowerCase())+ const secure = site.https+ return values.map(([name, value]) => ({+ name,+ value,+ ...(domain ? { domain } : {}),+ path: "/",+ maxAge: value ? ATTRIBUTION_MAX_AGE : 0,+ sameSite: "lax",+ secure,+ httpOnly: false,+ }))+}diff --git a/website/src/proxy.spec.ts b/website/src/proxy.spec.tsnew file mode 100644index 000000000..b49ce41e0--- /dev/null+++ b/website/src/proxy.spec.ts@@ -0,0 +1,55 @@+import { NextRequest } from "next/server"+import { unstable_doesMiddlewareMatch } from "next/experimental/testing/server"+import { describe, expect, it } from "vitest"+import { config, proxy } from "./proxy"++function setCookies(url: string, headers: Record<string, string> = {}): string[] {+ return proxy(new NextRequest(url, { headers })).headers.getSetCookie()+}++describe("proxy", () => {+ it("keeps a tagged /desktop link's tags on .autonomous.ai, as the browser reached the site", () => {+ // Behind the ingress the server sees its own address; the forwarded host and scheme are the public ones.+ const cookies = setCookies("http://10.0.0.7:3000/desktop?utm_source=x&utm_campaign=launch&rid=r1", {+ "x-forwarded-host": "harness.autonomous.ai",+ "x-forwarded-proto": "https",+ })++ expect(cookies).toEqual(expect.arrayContaining([+ expect.stringMatching(/^utm_source=x;.*Domain=\.autonomous\.ai/i),+ expect.stringMatching(/^utm_campaign=launch;/),+ expect.stringMatching(/^rid=r1;/),+ expect.stringMatching(/^utm_term=;.*Max-Age=0/i),+ ]))+ expect(cookies.find((c) => c.startsWith("rid="))).toMatch(/Secure/i)+ })++ it("does the same for a link straight to a download", () => {+ const cookies = setCookies("https://harness.autonomous.ai/desktop/download-macos?rid=r2", { host: "harness.autonomous.ai" })++ expect(cookies).toEqual(expect.arrayContaining([expect.stringMatching(/^rid=r2;.*Domain=\.autonomous\.ai/i)]))+ })++ it("writes nothing for an untagged request", () => {+ expect(setCookies("https://harness.autonomous.ai/desktop", { host: "harness.autonomous.ai" })).toEqual([])+ })++ it("marks a response that sets them as private, so no shared cache replays one person's referral", () => {+ const tagged = proxy(new NextRequest("https://harness.autonomous.ai/desktop?rid=r1", { headers: { host: "harness.autonomous.ai" } }))+ const plain = proxy(new NextRequest("https://harness.autonomous.ai/desktop", { headers: { host: "harness.autonomous.ai" } }))++ expect(tagged.headers.get("cache-control")).toBe("private, no-store")+ expect(plain.headers.get("cache-control")).toBeNull()+ })++ it("runs for landing pages and downloads, not for build output, release files or sign-in callbacks", () => {+ const runs = (url: string) => unstable_doesMiddlewareMatch({ config, url })+ expect(runs("/desktop?rid=r1")).toBe(true)+ expect(runs("/desktop/download-macos?rid=r1")).toBe(true)+ expect(runs("/?rid=r1")).toBe(true)+ expect(runs("/_next/static/chunks/a.js")).toBe(false)+ expect(runs("/harness-web/releases/1.3.25-abc/main.dart.js")).toBe(false)+ expect(runs("/auth/callback?code=c&state=s&rid=r1")).toBe(false)+ expect(runs("/callback?code=c&state=s&rid=r1")).toBe(false)+ })+})diff --git a/website/src/proxy.ts b/website/src/proxy.tsnew file mode 100644index 000000000..34fe32516--- /dev/null+++ b/website/src/proxy.ts@@ -0,0 +1,36 @@+import { NextResponse, type NextRequest } from "next/server"+import { attributionCookies, type SiteOrigin } from "./lib/signInAttribution"++/**+ * The public host and scheme. The standalone server sits behind the ingress, which terminates TLS+ * and forwards the browser's Host; without these headers (local `next dev`) the URL is the truth.+ */+function siteOrigin(request: NextRequest): SiteOrigin {+ const forwardedHost = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim()+ const host = forwardedHost || request.headers.get("host") || request.nextUrl.host+ const proto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim() || request.nextUrl.protocol.replace(/:$/, "")+ return { hostname: host.replace(/:\d+$/, ""), https: proto === "https" }+}++/**+ * Keeps a tagged link's `utm_*` and `rid` for the sign-in (lib/signInAttribution.ts). Every page and+ * download route goes through here — /desktop, a link straight to /desktop/download-macos, the web app+ * at / — and an untagged request passes untouched.+ */+export function proxy(request: NextRequest): NextResponse {+ const response = NextResponse.next()+ const cookies = attributionCookies(request.nextUrl.searchParams, siteOrigin(request))+ if (!cookies.length) return response+ for (const { name, value, ...options } of cookies) response.cookies.set(name, value, options)+ // /desktop is prerendered with `s-maxage=31536000`. Cloudflare passes it through today, but a cache+ // that kept this response would hand one person's referral cookies to everyone after them.+ response.headers.set("Cache-Control", "private, no-store")+ return response+}++export const config = {+ // Static build output and the web app's immutable release files never carry a campaign link. Nor do+ // the sign-in callbacks: auth.autonomous.ai echoes the cookies' own tags onto them, and writing them+ // back on every web sign-in would keep a 30-day attribution alive for as long as someone signs in.+ matcher: ["/((?!_next/|harness-web/releases/|auth/callback|callback|favicon\\.ico).*)"],+}