Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Record app page views (#2027)

Joshua France29ba14c00b77
diff --git a/package.json b/package.json
index 71ef63db1..8de4ab481 100644
--- a/package.json
+++ b/package.json
@@ -46,7 +46,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
+ "test:pg": "node scripts/run-pg-tests.mjs test/batch-paths.test.ts test/session-append-many.test.ts test/cron-queue.test.ts test/postgres-migration-startup.test.ts test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-instance-registry.test.ts test/readiness.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/postgres-background-ownership.test.ts test/postgres-resource-search.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/postgres-migrations.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-app-page-views.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/loop-ingress-pg.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/slack-mirror-fidelity.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/plugins/portal/src/index.ts b/plugins/portal/src/index.ts
index fdae1cee7..78b2c6264 100644
--- a/plugins/portal/src/index.ts
+++ b/plugins/portal/src/index.ts
@@ -999,7 +999,7 @@ async function handle(req: IncomingMessage, res: ServerResponse): Promise<void>
if (appSuffix && (requestHost.endsWith(appSuffix) || requestHost === APPS_DOMAIN?.toLowerCase())) {
const label = requestHost.slice(0, -appSuffix.length);
if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(label)) return json(res, 404, { error: "not_found" });
- return proxyToAppHost(req, res, CORE);
+ return proxyToAppHost(req, res, CORE, clientIpOf(req));
}
const crossOrigin = prepareCors(req, res, pathname, API_ALLOWED_ORIGINS.origins);
diff --git a/plugins/portal/src/proxy.ts b/plugins/portal/src/proxy.ts
index 579f0647f..2d5f81442 100644
--- a/plugins/portal/src/proxy.ts
+++ b/plugins/portal/src/proxy.ts
@@ -242,7 +242,7 @@ export function proxyToUpstream(
export const FORWARD_BROKER_HEADERS = ["accept", "accept-language", "user-agent", "content-type", "content-length"];
-export function proxyToAppHost(req: IncomingMessage, res: ServerResponse, coreBase: string): void {
+export function proxyToAppHost(req: IncomingMessage, res: ServerResponse, coreBase: string, clientIp?: string): void {
const upstream = new URL(coreBase);
const blocked = new Set([
...DROP_RESPONSE_HEADERS,
@@ -253,13 +253,17 @@ export function proxyToAppHost(req: IncomingMessage, res: ServerResponse, coreBa
"x-agent-capability",
PORTAL_IDENTITY_HEADER,
"x-qm-app-host",
+ "x-qm-client-ip",
"forwarded",
"x-forwarded-host",
"x-forwarded-proto",
"x-forwarded-for",
...(req.headers.connection ?? "").split(",").map((name) => name.trim().toLowerCase()),
]);
- const headers: Record<string, string | string[]> = { "x-qm-app-host": "1" };
+ const headers: Record<string, string | string[]> = {
+ "x-qm-app-host": "1",
+ ...(clientIp ? { "x-qm-client-ip": clientIp } : {}),
+ };
for (const [name, value] of Object.entries(req.headers)) {
if (value !== undefined && !blocked.has(name)) headers[name] = value;
}
diff --git a/plugins/portal/test/app-host-proxy.test.ts b/plugins/portal/test/app-host-proxy.test.ts
index 86211ea8f..d9b3d6d43 100644
--- a/plugins/portal/test/app-host-proxy.test.ts
+++ b/plugins/portal/test/app-host-proxy.test.ts
@@ -13,7 +13,7 @@ const upstream = createServer(async (req, res) => {
});
await new Promise<void>((resolve) => upstream.listen(0, resolve));
const upstreamUrl = `http://localhost:${(upstream.address() as AddressInfo).port}`;
-const portal = createServer((req, res) => proxyToAppHost(req, res, upstreamUrl));
+const portal = createServer((req, res) => proxyToAppHost(req, res, upstreamUrl, "198.51.100.1"));
await new Promise<void>((resolve) => portal.listen(0, resolve));
test.after(() => {
upstream.close();
@@ -38,6 +38,7 @@ test("app proxy preserves uploads and app headers while stripping identity and h
"x-signature": "forged",
"x-as-principal": "admin",
"x-agent-capability": "forged",
+ "x-qm-client-ip": "203.0.113.66",
connection: "x-remove-me",
"x-remove-me": "secret",
cookie: "portal_session=test",
@@ -65,6 +66,7 @@ test("app proxy preserves uploads and app headers while stripping identity and h
assert.equal(body.headers["x-app-key"], "app-key");
assert.equal(body.headers.cookie, "portal_session=test");
assert.equal(body.headers["x-qm-app-host"], "1");
+ assert.equal(body.headers["x-qm-client-ip"], "198.51.100.1", "the gateway, not the visitor, supplies the client IP");
for (const header of ["x-signature", "x-as-principal", "x-agent-capability", "x-remove-me"])
assert.equal(body.headers[header], undefined);
});
diff --git a/src/api/deps.ts b/src/api/deps.ts
index 4261b0e11..790fd56f7 100644
--- a/src/api/deps.ts
+++ b/src/api/deps.ts
@@ -76,6 +76,7 @@ import type { UiStateStore } from "../surfaces/ui-state.ts";
import type { ConnectorTokenSource, SlackUserClient } from "../loops/sources/adapter.ts";
import type { RateLimiter } from "../ratelimit/rate-limiter.ts";
import type { AdvisoryLock } from "../persistence/advisory-lock.ts";
+import type { AppPageViewLog } from "../deploy/page-views.ts";
import type { SlackInstallationStore, SlackSocketAppIdReader } from "../surfaces/slack-installation.ts";
import type { SlackAccountLink, ComposioReturn } from "./routes/composio.ts";
@@ -192,6 +193,7 @@ export interface ServerDeps {
deployAppsSessionSecret?: string;
deployAppsLoginUrl?: string;
deployAppsLoginPath?: "/auth/login" | "/auth/trusted/login";
+ appPageViews?: AppPageViewLog;
scheduler?: Scheduler;
webhookReceiver?: WebhookReceiver;
loopIngress?: LoopIngressService;
diff --git a/src/api/routes/deployments.ts b/src/api/routes/deployments.ts
index 55962944a..1e80adc84 100644
--- a/src/api/routes/deployments.ts
+++ b/src/api/routes/deployments.ts
@@ -12,7 +12,7 @@ import {
import { spawn } from "node:child_process";
import { basename, dirname } from "node:path";
import { deploymentView, type App, type DeployInput, type RedeployInput } from "../app.ts";
-import { errMessage } from "../../util/errors.ts";
+import { errMessage, reportFailureAs } from "../../util/errors.ts";
import { escapeHtml } from "../../../plugins/chassis/src/http.ts";
import { canonicalPayload } from "../../../plugins/chassis/src/source-auth-sign.ts";
import { sendJson, verifyOrReject } from "../http.ts";
@@ -25,6 +25,7 @@ import { resolveShareTarget as resolveShareTargetGrammar } from "../artifact-sha
import { verifyDeployGitAccess, viewerIdentityKey } from "../../deploy/access-token.ts";
import { appAnnotationAsset } from "../../deploy/app-annotate.ts";
import { APP_SHELL_PATH_PREFIX, appShellHtml } from "../../deploy/app-shell.ts";
+import type { AppViewAuthMode } from "../../deploy/page-views.ts";
import { principalDestination } from "../../reach/reach.ts";
import { FRAME_SESSION_COOKIE, portalSession, portalSessionFrom } from "../../deploy/viewer-session.ts";
import { EMBED_ANCESTORS_HINT, parseEmbedAncestors } from "../../deploy/embed-ancestors.ts";
@@ -159,6 +160,7 @@ async function proxyAdminDeployment(ctx: BaseCtx): Promise<void> {
const GATEWAY_AUTH_HEADERS = [
"x-qm-app-host",
+ "x-qm-client-ip",
"x-signature",
"x-timestamp",
"x-as-principal",
@@ -900,6 +902,28 @@ export async function proxyDeploymentSubdomain(ctx: BaseCtx): Promise<boolean> {
authenticatedPermission = await app.effectiveDeploymentPermission(deployment, sub);
}
}
+ const isPageLoad =
+ ctx.method === "GET" &&
+ wantsHtml &&
+ (dest === undefined || dest === "document" || (dest === "iframe" && site !== "same-origin"));
+ const recordPageView = (authMode: AppViewAuthMode): void => {
+ if (!isPageLoad || !deployment || !deps.appPageViews) return;
+ const forwardedIp = fromAppHost ? req.headers["x-qm-client-ip"] : undefined;
+ const ip = typeof forwardedIp === "string" && forwardedIp ? forwardedIp : (req.socket.remoteAddress ?? null);
+ const userAgent = req.headers["user-agent"];
+ void deps.appPageViews
+ .record({
+ deploymentId: deployment.id,
+ version: deployment.appliedVersion ?? deployment.currentVersion ?? null,
+ viewer: sub ?? null,
+ authMode,
+ at: Date.now(),
+ path: safePathname,
+ ip,
+ userAgent: typeof userAgent === "string" ? userAgent : null,
+ })
+ .catch(reportFailureAs("app page view log", undefined, deployment.id));
+ };
if (ctx.method === "GET" && ((!bareApp && isTopDocument) || isShellRequest) && canManage && loginUrl) {
const annotationsEnabled =
!!sub && (await deps.featureFlags?.enabled("app_annotations", scopeId("personal", sub))) === true;
@@ -921,6 +945,7 @@ export async function proxyDeploymentSubdomain(ctx: BaseCtx): Promise<boolean> {
} else sendJson(res, 404, { error: "not_found" });
return true;
}
+ recordPageView("signed_in");
res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
res.end(
appShellHtml({
@@ -998,6 +1023,12 @@ export async function proxyDeploymentSubdomain(ctx: BaseCtx): Promise<boolean> {
cleanUrlRedirect();
return true;
}
+ if (reach.status === "ok") {
+ let authMode: AppViewAuthMode = "public";
+ if (session?.appOnly) authMode = "app_only";
+ else if (sub && (canManage || authenticatedPermission)) authMode = "signed_in";
+ recordPageView(authMode);
+ }
await proxyReach(ctx, reach, pathname, sub && (canManage || authenticatedPermission) ? sub : undefined, {
frameAncestors: embedAncestors,
});
diff --git a/src/deploy/page-views.ts b/src/deploy/page-views.ts
new file mode 100644
index 000000000..43e73d1f6
--- /dev/null
+++ b/src/deploy/page-views.ts
@@ -0,0 +1,80 @@
+import { createPgPool } from "../persistence/pg-pool.ts";
+
+export type AppViewAuthMode = "signed_in" | "public" | "app_only";
+
+export interface AppPageView {
+ deploymentId: string;
+ version: number | null;
+ viewer: string | null;
+ authMode: AppViewAuthMode;
+ at: number;
+ path: string;
+ ip: string | null;
+ userAgent: string | null;
+}
+
+export interface AppPageViewLog {
+ record(view: AppPageView): Promise<void>;
+}
+
+const MAX_PATH = 2048;
+const MAX_USER_AGENT = 512;
+const MAX_IP = 64;
+const MAX_IN_FLIGHT = 32;
+const DROP_REPORT_INTERVAL_MS = 60_000;
+
+const clip = (value: string | null, max: number): string | null => (value === null ? null : value.slice(0, max));
+
+export function createPostgresAppPageViewLog(connectionString: string): AppPageViewLog {
+ const { q } = createPgPool(connectionString, "deploy/app-page-views/0001", [
+ "CREATE TABLE IF NOT EXISTS deployments (id TEXT PRIMARY KEY, json JSONB NOT NULL)",
+ `CREATE TABLE IF NOT EXISTS app_page_views(
+ id BIGSERIAL PRIMARY KEY,
+ deployment_id TEXT NOT NULL REFERENCES deployments(id),
+ version INTEGER,
+ viewer TEXT,
+ auth_mode TEXT NOT NULL,
+ at BIGINT NOT NULL,
+ path TEXT NOT NULL,
+ ip TEXT,
+ user_agent TEXT
+ )`,
+ `CREATE INDEX IF NOT EXISTS app_page_views_by_deployment_at ON app_page_views(deployment_id, at DESC)`,
+ `CREATE INDEX IF NOT EXISTS app_page_views_by_at ON app_page_views(at)`,
+ ]);
+ let inFlight = 0;
+ let dropped = 0;
+ let lastDropReport = 0;
+ return {
+ async record(view) {
+ if (inFlight >= MAX_IN_FLIGHT) {
+ dropped++;
+ const now = Date.now();
+ if (now - lastDropReport < DROP_REPORT_INTERVAL_MS) return;
+ lastDropReport = now;
+ const count = dropped;
+ dropped = 0;
+ throw new Error(`dropped ${count} page views: write backlog full`);
+ }
+ inFlight++;
+ try {
+ await q(
+ `INSERT INTO app_page_views(deployment_id, version, viewer, auth_mode, at, path, ip, user_agent)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
+ [
+ view.deploymentId,
+ view.version,
+ view.viewer,
+ view.authMode,
+ view.at,
+ clip(view.path, MAX_PATH),
+ clip(view.ip, MAX_IP),
+ clip(view.userAgent, MAX_USER_AGENT),
+ ],
+ );
+ } finally {
+ inFlight--;
+ }
+ },
+ };
+}
diff --git a/src/wiring.ts b/src/wiring.ts
index 7cbc2d755..2e47c60e4 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -139,6 +139,7 @@ import type {
import { personalScope, scopeId } from "./types.ts";
import { createAuditLog, type AuditLog } from "./audit/audit-log.ts";
import { createPostgresAuditLog } from "./admin/postgres-audit-log.ts";
+import { createPostgresAppPageViewLog, type AppPageViewLog } from "./deploy/page-views.ts";
import { createRateLimiter, type RateLimiter } from "./ratelimit/rate-limiter.ts";
import { createPostgresRateLimiter } from "./ratelimit/postgres-rate-limiter.ts";
import { createBudgetTracker, estimateCostUsd } from "./ratelimit/budget.ts";
@@ -549,6 +550,7 @@ export interface BuiltApp {
projects: ProjectStore;
environments: EnvironmentStore;
processes?: ProcessRegistry;
+ appPageViews?: AppPageViewLog;
monitors: MonitorStore;
browserSessionStore?: BrowserSessionStore;
monitorPoller?: MonitorPoller;
@@ -1560,6 +1562,8 @@ export function buildApp(
processes = config.databaseUrl ? createPostgresProcessRegistry(config.databaseUrl) : createMemoryProcessRegistry();
}
+ const appPageViews =
+ config.databaseUrl && config.awsDeploy.appsDomain ? createPostgresAppPageViewLog(config.databaseUrl) : undefined;
const brokerSessions = config.databaseUrl ? createPostgresBrokerSessions(config.databaseUrl) : undefined;
const replayDedupe = config.databaseUrl ? createPostgresReplayDedupe(config.databaseUrl) : createMemoryReplayDedupe();
const metrics = config.databaseUrl ? createPostgresMetricsSink(config.databaseUrl) : createMetricsSink();
@@ -2861,6 +2865,7 @@ export function buildApp(
projects,
environments,
...(processes ? { processes } : {}),
+ ...(appPageViews ? { appPageViews } : {}),
monitors,
...(browserSessionStore ? { browserSessionStore } : {}),
...(monitorPoller ? { monitorPoller } : {}),
@@ -2973,6 +2978,7 @@ export function serverDeps(
...(config.deployAppsSessionSecret ? { deployAppsSessionSecret: config.deployAppsSessionSecret } : {}),
...(config.deployAppsLoginUrl ? { deployAppsLoginUrl: config.deployAppsLoginUrl } : {}),
...(config.deployAppsLoginPath ? { deployAppsLoginPath: config.deployAppsLoginPath } : {}),
+ ...(built.appPageViews ? { appPageViews: built.appPageViews } : {}),
scheduler: built.scheduler,
webhookReceiver: built.webhookReceiver,
loopIngress: built.loopIngress,
diff --git a/test/app-page-views.test.ts b/test/app-page-views.test.ts
new file mode 100644
index 000000000..7771a0c09
--- /dev/null
+++ b/test/app-page-views.test.ts
@@ -0,0 +1,206 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { createHmac } from "node:crypto";
+import { request as httpRequest, createServer as createHttpServer } from "node:http";
+import type { AddressInfo } from "node:net";
+import { createApp } from "../src/api/app.ts";
+import { createInsecureTestServer } from "../src/api/server.ts";
+import { createDeployStore } from "../src/deploy/deploy-store.ts";
+import { createDeployService } from "../src/deploy/deploy-service.ts";
+import { createAclStore } from "../src/acl/acl-store.ts";
+import { createDirectoryStore } from "../src/directory/directory-store.ts";
+import { createIdentityService } from "../src/identity/identity-service.ts";
+import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
+import { scopeId } from "../src/types.ts";
+import type { FeatureFlagStore } from "../src/feature-flags.ts";
+import type { AppPageView, AppPageViewLog } from "../src/deploy/page-views.ts";
+
+const SESSION_SECRET = "portal-session-secret";
+const LOGIN_URL = "https://portal.example.com";
+const HOST = "mysite.apps.example.com";
+const auditLog = { record() {}, events: async () => [], tail: async () => [] };
+
+function session(sub: string, appOnly?: true): string {
+ const key = createHmac("sha256", SESSION_SECRET).update("portal.session.v1").digest();
+ const now = Math.floor(Date.now() / 1000);
+ const body = Buffer.from(
+ JSON.stringify({ k: "session", sub, org: "acme", iat: now, exp: now + 3600, ...(appOnly ? { appOnly } : {}) }),
+ ).toString("base64url");
+ return `portal_session=${body}.${createHmac("sha256", key).update(body).digest("base64url")}`;
+}
+
+function get(port: number, path: string, headers: Record<string, string>): Promise<{ status: number; body: string }> {
+ return new Promise((resolve, reject) => {
+ const req = httpRequest(
+ { host: "localhost", port, path, method: "GET", headers: { Host: HOST, ...headers } },
+ (res) => {
+ let body = "";
+ res.on("data", (c) => (body += c));
+ res.on("end", () => resolve({ status: res.statusCode ?? 0, body }));
+ },
+ );
+ req.on("error", reject);
+ req.end();
+ });
+}
+
+async function harness(log: AppPageViewLog) {
+ const upstream = createHttpServer((_req, res) => {
+ res.writeHead(200, { "content-type": "text/html" });
+ res.end("UPSTREAM OK");
+ });
+ upstream.listen(0);
+ const upstreamPort = (upstream.address() as AddressInfo).port;
+ const acl = createAclStore();
+ const deploy = createDeployService({
+ externalSharingAllowed: async () => true,
+ deployStore: createDeployStore(),
+ provider: {
+ profile: { managedScaleToZero: false },
+ apply: async () => ({ host: "127.0.0.1", port: upstreamPort }),
+ destroy: async () => {},
+ },
+ auditLog,
+ acl,
+ deployDir: mkdtempSync(join(tmpdir(), "page-views-")),
+ });
+ const app = createApp({
+ deploy,
+ acl,
+ directory: createDirectoryStore(),
+ sessions: createMemorySessionStore(),
+ identity: createIdentityService(),
+ } as unknown as Parameters<typeof createApp>[0]);
+ const deployed = await app.deploy({
+ ownerScopeId: scopeId("personal", "[email protected]"),
+ createdBy: "[email protected]",
+ entrypoint: "x",
+ files: [],
+ name: "mysite",
+ });
+ const server = createInsecureTestServer(app, {
+ featureFlags: { enabled: async () => true } as unknown as FeatureFlagStore,
+ deployAppsDomain: "apps.example.com",
+ deployGateSecret: "gate-secret",
+ deployAppsSessionSecret: SESSION_SECRET,
+ deployAppsLoginUrl: LOGIN_URL,
+ appPageViews: log,
+ });
+ server.listen(0);
+ return {
+ app,
+ deploymentId: (deployed as { id: string }).id,
+ port: (server.address() as AddressInfo).port,
+ close: async () => {
+ await new Promise<void>((resolve) => server.close(() => resolve()));
+ await new Promise<void>((resolve) => upstream.close(() => resolve()));
+ },
+ };
+}
+
+const settle = () => new Promise((resolve) => setImmediate(resolve));
+
+test("page loads of a published app are recorded with viewer, auth mode and request basics", async () => {
+ const views: AppPageView[] = [];
+ const h = await harness({ record: async (view) => void views.push(view) });
+ try {
+ const owner = await get(h.port, "/reports?token=secret", {
+ Accept: "text/html",
+ Cookie: session("[email protected]"),
+ "User-Agent": "test-agent/1",
+ "x-qm-app-host": "1",
+ "x-qm-client-ip": "203.0.113.7",
+ });
+ assert.equal(owner.status, 200);
+ await settle();
+ assert.equal(views.length, 1);
+ const [view] = views;
+ assert.equal(view!.deploymentId, h.deploymentId);
+ assert.equal(view!.viewer, "[email protected]");
+ assert.equal(view!.authMode, "signed_in");
+ assert.equal(view!.path, "/reports", "the query string is never stored");
+ assert.equal(view!.ip, "203.0.113.7");
+ assert.equal(view!.userAgent, "test-agent/1");
+ assert.equal(typeof view!.version, "number");
+ assert.ok(Math.abs(view!.at - Date.now()) < 10_000);
+
+ const assetHeaders: Record<string, string>[] = [
+ { Accept: "*/*", "Sec-Fetch-Dest": "script" },
+ { Accept: "application/json" },
+ { Accept: "text/html", "Sec-Fetch-Dest": "image" },
+ ];
+ for (const headers of assetHeaders) {
+ const asset = await get(h.port, "/app.js", { ...headers, Cookie: session("[email protected]") });
+ assert.equal(asset.status, 200);
+ }
+ await settle();
+ assert.equal(views.length, 1, "static assets and API calls are not page views");
+
+ await get(h.port, "/", { Accept: "text/html", Cookie: session("[email protected]") });
+ await get(h.port, "/", { Accept: "text/html" });
+ await settle();
+ assert.equal(views.length, 1, "denied and signed-out requests that never reach the app are not recorded");
+
+ await h.app.setDeploymentPublic("mysite", true, { createdBy: "[email protected]" });
+ await get(h.port, "/", { Accept: "text/html", "x-qm-client-ip": "198.51.100.9" });
+ await get(h.port, "/", { Accept: "text/html", Cookie: session("[email protected]") });
+ await get(h.port, "/", { Accept: "text/html", Cookie: session("[email protected]", true) });
+ await settle();
+ assert.deepEqual(
+ views.slice(1).map((v) => [v.viewer, v.authMode]),
+ [
+ [null, "public"],
+ ["[email protected]", "public"],
+ ["[email protected]", "app_only"],
+ ],
+ );
+ assert.notEqual(views[1]!.ip, "198.51.100.9", "a client IP header is only trusted from the app gateway");
+ } finally {
+ await h.close();
+ }
+});
+
+test("the owner's framed shell counts once, not again for the inner app frame", async () => {
+ const views: AppPageView[] = [];
+ const h = await harness({ record: async (view) => void views.push(view) });
+ try {
+ const shell = await get(h.port, "/", {
+ Accept: "text/html",
+ "Sec-Fetch-Dest": "document",
+ Cookie: session("[email protected]"),
+ });
+ assert.equal(shell.status, 200);
+ const inner = await get(h.port, "/", {
+ Accept: "text/html",
+ "Sec-Fetch-Dest": "iframe",
+ "Sec-Fetch-Site": "same-origin",
+ Cookie: session("[email protected]"),
+ });
+ assert.equal(inner.status, 200);
+ await settle();
+ assert.equal(views.length, 1);
+ assert.equal(views[0]!.authMode, "signed_in");
+ } finally {
+ await h.close();
+ }
+});
+
+test("a failing page-view log never blocks the page and is reported", async () => {
+ const errors: string[] = [];
+ const original = console.error;
+ console.error = (...args: unknown[]) => void errors.push(args.map(String).join(" "));
+ const h = await harness({ record: async () => Promise.reject(new Error("db down")) });
+ try {
+ const page = await get(h.port, "/", { Accept: "text/html", Cookie: session("[email protected]") });
+ assert.equal(page.status, 200);
+ assert.equal(page.body, "UPSTREAM OK");
+ await settle();
+ assert.ok(errors.some((line) => line.includes("app page view log") && line.includes("db down")));
+ } finally {
+ console.error = original;
+ await h.close();
+ }
+});
diff --git a/test/postgres-app-page-views.test.ts b/test/postgres-app-page-views.test.ts
new file mode 100644
index 000000000..d9167ef5f
--- /dev/null
+++ b/test/postgres-app-page-views.test.ts
@@ -0,0 +1,81 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { createPostgresAppPageViewLog } from "../src/deploy/page-views.ts";
+
+const URL = process.env.DATABASE_URL;
+const view = () => ({
+ deploymentId: "dep-1",
+ version: null,
+ viewer: null,
+ authMode: "public" as const,
+ at: 1_700_000_000_002,
+ path: "/",
+ ip: null,
+ userAgent: null,
+});
+const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the Postgres page-view tests";
+
+test("pg page-view log stores one row per view with clipped request fields", { skip }, async () => {
+ const pg = (await import("pg")).default;
+ const admin = new pg.Pool({ connectionString: URL });
+ try {
+ await admin.query("DROP TABLE IF EXISTS app_page_views CASCADE");
+ await admin.query("DELETE FROM qm_schema_migrations WHERE id LIKE 'deploy/app-page-views/%'").catch(() => {});
+ await admin.query("CREATE TABLE IF NOT EXISTS deployments (id TEXT PRIMARY KEY, json JSONB NOT NULL)");
+ await admin.query(`INSERT INTO deployments (id, json) VALUES ('dep-1', '{}') ON CONFLICT (id) DO NOTHING`);
+ const log = createPostgresAppPageViewLog(URL!);
+ await log.record({
+ deploymentId: "dep-1",
+ version: 3,
+ viewer: "[email protected]",
+ authMode: "signed_in",
+ at: 1_700_000_000_000,
+ path: "/reports",
+ ip: "203.0.113.7",
+ userAgent: "ua".repeat(400),
+ });
+ await log.record({
+ deploymentId: "dep-1",
+ version: null,
+ viewer: null,
+ authMode: "public",
+ at: 1_700_000_000_001,
+ path: `/${"p".repeat(5000)}`,
+ ip: null,
+ userAgent: null,
+ });
+ const { rows } = await admin.query(
+ "SELECT deployment_id, version, viewer, auth_mode, at, path, ip, user_agent FROM app_page_views ORDER BY at",
+ );
+ assert.equal(rows.length, 2);
+ assert.deepEqual(
+ { ...rows[0], at: Number(rows[0].at), user_agent: rows[0].user_agent.length },
+ {
+ deployment_id: "dep-1",
+ version: 3,
+ viewer: "[email protected]",
+ auth_mode: "signed_in",
+ at: 1_700_000_000_000,
+ path: "/reports",
+ ip: "203.0.113.7",
+ user_agent: 512,
+ },
+ );
+ assert.equal(rows[1].viewer, null);
+ assert.equal(rows[1].auth_mode, "public");
+ assert.equal(rows[1].path.length, 2048);
+ assert.equal(rows[1].ip, null);
+ await assert.rejects(
+ log.record({ ...view(), deploymentId: "no-such-app" }),
+ (err: { code?: string }) => err.code === "23503",
+ );
+ await assert.rejects(
+ admin.query("DELETE FROM deployments WHERE id = 'dep-1'"),
+ (err: { code?: string }) => err.code === "23503",
+ );
+ } finally {
+ await admin.query("DROP TABLE IF EXISTS app_page_views CASCADE").catch(() => {});
+ await admin.query("DELETE FROM deployments WHERE id = 'dep-1'").catch(() => {});
+ await admin.end();
+ }
+});