Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
3.9 KB
Raw Blame
import { describe, expect, test } from "bun:test"
import {
  normalizeConfiguredPublicApiBaseUrl,
  publicRequestUrl,
} from "../src/request-url.js"

describe("public API URL configuration", () => {
  test("callback base resolution keeps the configured prefix", async () => {
    process.env.DATABASE_URL = process.env.DATABASE_URL ?? "mysql://root:[email protected]:3306/omnirush_test"
    process.env.DEN_DB_ENCRYPTION_KEY = process.env.DEN_DB_ENCRYPTION_KEY ?? "x".repeat(32)
    process.env.BETTER_AUTH_SECRET = process.env.BETTER_AUTH_SECRET ?? "y".repeat(32)
    process.env.BETTER_AUTH_URL = process.env.BETTER_AUTH_URL ?? "http://127.0.0.1:8790"
    const { resolvePublicApiBaseUrl } = await import("../src/capability-sources/generic-oauth.js")

    expect(resolvePublicApiBaseUrl(
      new Request("http://den-api.internal:8790/v1/example"),
      "https://omnirush.example/api/den/",
    )).toBe("https://omnirush.example/api/den")
  })

  test("preserves and normalizes an HTTPS pathname prefix", () => {
    expect(normalizeConfiguredPublicApiBaseUrl("https://omnirush.example/api/den/", {
      allowInsecureHttp: false,
    })).toBe("https://omnirush.example/api/den")
  })

  test("requires HTTPS outside development and localhost", () => {
    expect(() => normalizeConfiguredPublicApiBaseUrl("http://omnirush.example/api/den", {
      allowInsecureHttp: false,
    })).toThrow("must use HTTPS")
    expect(normalizeConfiguredPublicApiBaseUrl("http://127.0.0.1:8790/api/den", {
      allowInsecureHttp: false,
    })).toBe("http://127.0.0.1:8790/api/den")
    expect(normalizeConfiguredPublicApiBaseUrl("http://omnirush.example/api/den", {
      allowInsecureHttp: true,
    })).toBe("http://omnirush.example/api/den")
  })

  test("rejects malformed and non-base URLs", () => {
    expect(() => normalizeConfiguredPublicApiBaseUrl("not a url", {
      allowInsecureHttp: false,
    })).toThrow("absolute http or https URL")
    expect(() => normalizeConfiguredPublicApiBaseUrl("https://omnirush.example/api?tenant=one", {
      allowInsecureHttp: false,
    })).toThrow("cannot contain credentials, a query string, or a fragment")
  })
})

describe("publicRequestUrl forwarded host policy", () => {
  const request = new Request("http://den-api.internal:8790/v1/example", {
    headers: {
      "x-forwarded-host": "connect.example.com",
      "x-forwarded-proto": "https",
    },
  })

  test("honors a forwarded host on a configured trusted origin", () => {
    expect(publicRequestUrl(request, {
      trustedOrigins: ["https://connect.example.com"],
    }).origin).toBe("https://connect.example.com")
  })

  test("honors a forwarded host covered by a trusted wildcard origin", () => {
    const preview = new Request("http://den-api.internal:8790/v1/example", {
      headers: {
        "x-forwarded-host": "3005-rotated.daytonaproxy01.net",
        "x-forwarded-proto": "https",
      },
    })

    expect(publicRequestUrl(preview, {
      trustedOrigins: ["https://*.daytonaproxy01.net"],
    }).origin).toBe("https://3005-rotated.daytonaproxy01.net")
    expect(publicRequestUrl(preview, {
      trustedOrigins: ["http://*.daytonaproxy01.net"],
    }).origin).toBe("https://den-api.internal:8790")
    expect(publicRequestUrl(preview, {
      trustedOrigins: ["https://*.proxy01.net"],
    }).origin).toBe("https://den-api.internal:8790")
  })

  test("ignores an untrusted or malformed forwarded host", () => {
    expect(publicRequestUrl(request, {
      trustedOrigins: ["https://app.example.com"],
    }).origin).toBe("https://den-api.internal:8790")

    const malformed = new Request("http://den-api.internal:8790/v1/example", {
      headers: {
        "x-forwarded-host": "[email protected]",
        "x-forwarded-proto": "https",
      },
    })
    expect(publicRequestUrl(malformed, {
      trustedOrigins: ["https://connect.example.com"],
    }).origin).toBe("https://den-api.internal:8790")
  })
})