Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(hog): read every JSON document through one schema-checked helper (#129798)

Rafael Audibertf4bfc5e28910
diff --git a/hog/src/cli.test.ts b/hog/src/cli.test.ts
index 02a64e49ca51..1a954eae963d 100644
--- a/hog/src/cli.test.ts
+++ b/hog/src/cli.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, test } from 'bun:test'
+import { z } from 'zod'
import { LEGACY_COMMANDS } from '@/commands/legacy'
import { VERSION } from '@/version'
@@ -85,7 +86,12 @@ describe('hog', () => {
test('commands lists every command for agents', async () => {
const result = await runHog(['commands'], { env: { CLAUDECODE: '1' } })
expect(result.exitCode).toBe(0)
- const manifest = result.json()
+ const manifest = result.json(
+ z.looseObject({
+ globalOptions: z.array(z.unknown()),
+ commands: z.array(z.looseObject({ name: z.string() })),
+ })
+ )
expect(manifest).toMatchObject({ cli: 'hog', agent: 'claude-code', aliases: { et: 'error-tracking' } })
expect(manifest.globalOptions).toContainEqual(expect.objectContaining({ flags: '-o, --output <format>' }))
expect(manifest.commands.find((command: { name: string }) => command.name === 'query run')).toMatchObject({
diff --git a/hog/src/cli.ts b/hog/src/cli.ts
index b1fdd9bb38d8..549a8305aeae 100644
--- a/hog/src/cli.ts
+++ b/hog/src/cli.ts
@@ -1,5 +1,6 @@
import { Command as Commander, CommanderError, Option } from 'commander'
import { parseArgs, type ParseArgsOptionsConfig } from 'node:util'
+import { z } from 'zod'
import { COMMANDS } from '@/commands'
import { resolveMode, type Mode, type OutputFormat } from '@/core/agent'
@@ -18,6 +19,7 @@ import { findConfig, loadConfig } from '@/core/config'
import { Context } from '@/core/context'
import { EXIT_FAILURE, EXIT_USAGE, HogError } from '@/core/errors'
import { DRY_RUN_ENV, envFlag, GLOBAL_OPTIONS, networkRuntime, optOutFlag, resolveGlobals } from '@/core/globals'
+import { parseJsonOrUndefined } from '@/core/json'
import { Output } from '@/core/output'
import type { Runtime } from '@/core/runtime'
import { Telemetry } from '@/core/telemetry'
@@ -414,7 +416,9 @@ function toHogError(caught: unknown): HogError {
return new HogError('internal_error', caught instanceof Error ? caught.message : String(caught))
}
-// Read without the schema, so an opt-out still counts in a file another check rejects. A file
+const TelemetryOptOut = z.object({ cli: z.object({ telemetry: z.literal(false) }) })
+
+// Read without the full schema, so an opt-out still counts in a file another check rejects. A file
// that isn't JSON stops the command later, with telemetry on.
async function configOptsOut(runtime: Runtime): Promise<boolean> {
const path = await findConfig(runtime)
@@ -422,7 +426,7 @@ async function configOptsOut(runtime: Runtime): Promise<boolean> {
return false
}
try {
- return (JSON.parse(await runtime.readFile(path)) as { cli?: { telemetry?: unknown } })?.cli?.telemetry === false
+ return parseJsonOrUndefined(await runtime.readFile(path), TelemetryOptOut) !== undefined
} catch {
return false
}
diff --git a/hog/src/commands/api/call.ts b/hog/src/commands/api/call.ts
index f3e73deef8da..36ae6efe6723 100644
--- a/hog/src/commands/api/call.ts
+++ b/hog/src/commands/api/call.ts
@@ -1,32 +1,22 @@
+import { z } from 'zod'
+
import { defineCommand } from '@/core/command'
import { EXIT_USAGE, HogError } from '@/core/errors'
+import { parseJson } from '@/core/json'
import { findTool, grantedScopes, missingScopesError, toHogError, withApiConnection } from '@/posthog/api-tools'
import { parseOrganizationId, parseProjectId } from '@/posthog/ids'
+const ToolInput = z.record(z.string(), z.unknown(), { error: 'The input must be a JSON object.' })
+
function parseInput(text: string | undefined): Record<string, unknown> {
if (text === undefined) {
return {}
}
- let input: unknown
- try {
- input = JSON.parse(text)
- } catch (error) {
- throw new HogError(
- 'invalid_json',
- `The input isn't valid JSON: ${(error as Error).message}`,
- 'Pass one JSON object, like \'{"limit": 10}\'.',
- EXIT_USAGE
- )
- }
- if (input === null || typeof input !== 'object' || Array.isArray(input)) {
- throw new HogError(
- 'invalid_json',
- 'The input must be a JSON object.',
- 'Pass one JSON object, like \'{"limit": 10}\'.',
- EXIT_USAGE
- )
- }
- return input as Record<string, unknown>
+ return parseJson(text, ToolInput, {
+ code: 'invalid_json',
+ hint: 'Pass one JSON object, like \'{"limit": 10}\'.',
+ exitCode: EXIT_USAGE,
+ })
}
export default defineCommand({
diff --git a/hog/src/commands/auth/auth.test.ts b/hog/src/commands/auth/auth.test.ts
index 874ca139c831..4ea57eb1c307 100644
--- a/hog/src/commands/auth/auth.test.ts
+++ b/hog/src/commands/auth/auth.test.ts
@@ -3,7 +3,7 @@ import { describe, expect, test } from 'bun:test'
import type { Secrets } from '@/core/runtime'
import { LOGIN_SCOPES } from '@/posthog/device-auth'
import { HOG_CLIENT_ID } from '@/posthog/oauth'
-import { runHog } from '@/testing'
+import { readJson, runHog } from '@/testing'
const EU = 'https://eu.posthog.com'
const LEGACY_CLI_FILE = '/home/test/.posthog/credentials.json'
@@ -76,7 +76,7 @@ describe('hog auth login', () => {
next: 'hog auth login --wait',
})
expect(result.stdout).not.toContain(DEVICE_CODE)
- expect(JSON.parse(result.files[PENDING_FILE] ?? '{}')).toMatchObject({ deviceCode: DEVICE_CODE })
+ expect(readJson(result.files[PENDING_FILE] ?? '{}')).toMatchObject({ deviceCode: DEVICE_CODE })
expect(result.opened).toEqual([])
expect(result.requests).toEqual([
{
@@ -176,7 +176,9 @@ describe('hog auth login', () => {
})
expect(result.json()).toMatchObject({ status: 'logged_in', scopes: ['openid', 'insight:read'] })
- expect(JSON.parse(result.files[SETTINGS_FILE] ?? '{}').logins[EU].scopes).toEqual(['openid', 'insight:read'])
+ expect(readJson(result.files[SETTINGS_FILE] ?? '{}')).toMatchObject({
+ logins: { [EU]: { scopes: ['openid', 'insight:read'] } },
+ })
})
test('--wait stores the token for the approved project in the keychain', async () => {
@@ -196,13 +198,13 @@ describe('hog auth login', () => {
})
expect(result.stdout).not.toContain(ACCESS_TOKEN)
// The keychain keeps only the tokens. What isn't secret is in settings.json.
- expect(JSON.parse(result.keychain[EU] ?? '{}')).toEqual({
+ expect(readJson(result.keychain[EU] ?? '{}')).toEqual({
accessToken: ACCESS_TOKEN,
refreshToken: REFRESH_TOKEN,
})
// Windows Credential Manager refuses an item over 2,560 bytes, and Bun stores it as UTF-8.
expect(Buffer.byteLength(result.keychain[EU] ?? '')).toBeLessThanOrEqual(2560)
- expect(JSON.parse(result.files[SETTINGS_FILE] ?? '{}')).toEqual({
+ expect(readJson(result.files[SETTINGS_FILE] ?? '{}')).toEqual({
logins: {
[EU]: {
store: 'keychain',
@@ -267,10 +269,10 @@ describe('hog auth login', () => {
credentialsPath: HOG_CREDENTIALS_FILE,
keychainError: expect.stringContaining('keychain'),
})
- expect(JSON.parse(result.files[HOG_CREDENTIALS_FILE] ?? '{}')).toEqual({
+ expect(readJson(result.files[HOG_CREDENTIALS_FILE] ?? '{}')).toEqual({
[EU]: { accessToken: ACCESS_TOKEN, refreshToken: REFRESH_TOKEN },
})
- expect(JSON.parse(result.files[SETTINGS_FILE] ?? '{}')).toMatchObject({
+ expect(readJson(result.files[SETTINGS_FILE] ?? '{}')).toMatchObject({
logins: { 'https://eu.posthog.com': { store: 'file', projectId: '42' } },
})
}
@@ -338,7 +340,7 @@ describe('hog auth login', () => {
expect(result.stdout).toContain(`Logged in to ${host}, project 42.`)
expect(result.opened).toHaveLength(opensBrowser ? 1 : 0)
expect(result.files[PENDING_FILE]).toBeUndefined()
- expect(JSON.parse(result.keychain[host] ?? '{}')).toMatchObject(secret)
+ expect(readJson(result.keychain[host] ?? '{}')).toMatchObject(secret)
expect(result.stdout).toContain('Try it: hog query run')
})
@@ -691,7 +693,7 @@ describe('hog auth login', () => {
})
expect(result.keychain).toEqual({})
expect(result.files[HOG_CREDENTIALS_FILE]).toBeUndefined()
- expect(JSON.parse(result.files[SETTINGS_FILE] ?? '{}')).toEqual({ logins: {} })
+ expect(readJson(result.files[SETTINGS_FILE] ?? '{}')).toEqual({ logins: {} })
expect(result.files[PENDING_FILE]).toBeUndefined()
})
diff --git a/hog/src/commands/auth/migrate.ts b/hog/src/commands/auth/migrate.ts
index e83ed8441981..573aa1db8ec6 100644
--- a/hog/src/commands/auth/migrate.ts
+++ b/hog/src/commands/auth/migrate.ts
@@ -1,7 +1,9 @@
+import { z } from 'zod'
+
import { defineCommand } from '@/core/command'
+import { parseJsonOrUndefined } from '@/core/json'
import { escapeControls } from '@/core/output'
import { PersonalAPIKey } from '@/posthog/access-key'
-import { parseJson } from '@/posthog/credential-store/helpers'
import type { StoreChoice } from '@/posthog/credential-store/types'
import { DEFAULT_HOST, posthogFile } from '@/posthog/credentials'
import { parseHost } from '@/posthog/host'
@@ -22,6 +24,9 @@ type MigrateResult =
}
| { status: 'nothing_to_migrate'; legacyFile: string; next: string }
+// The fields go through the brand factories below, so a bad value names what is wrong with it.
+const LegacyCredentials = z.looseObject({ token: z.unknown(), env_id: z.unknown(), host: z.unknown() })
+
// TODO(v1.1): drop this command. It exists so people moving from posthog-cli keep their key
// without logging in again, and hog reads the old file nowhere else.
export default defineCommand({
@@ -45,7 +50,7 @@ export default defineCommand({
}
throw error
}
- const stored = parseJson(text) as { token?: unknown; env_id?: unknown; host?: unknown } | null | undefined
+ const stored = parseJsonOrUndefined(text, LegacyCredentials)
const failure = {
code: 'invalid_legacy_credentials',
context: `hog couldn't move the key in ${legacyFile}.`,
diff --git a/hog/src/commands/config/config.test.ts b/hog/src/commands/config/config.test.ts
index 609208c3f4e2..f77e9529575b 100644
--- a/hog/src/commands/config/config.test.ts
+++ b/hog/src/commands/config/config.test.ts
@@ -3,13 +3,13 @@ import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { configSchema } from '@/core/config'
-import { runHog } from '@/testing'
+import { readJson, runHog } from '@/testing'
const VALID = JSON.stringify({ errorTracking: { sources: [{ path: 'dist', kind: 'js' }] } })
describe('hog config', () => {
test('posthog.schema.json matches the zod definitions', () => {
- const file = JSON.parse(readFileSync(join(import.meta.dir, '../../../posthog.schema.json'), 'utf8'))
+ const file = readJson(readFileSync(join(import.meta.dir, '../../../posthog.schema.json'), 'utf8'))
expect(file).toEqual(configSchema())
})
diff --git a/hog/src/commands/endpoints/endpoints.test.ts b/hog/src/commands/endpoints/endpoints.test.ts
index 248ad58e4a62..a9ac0c2d8de2 100644
--- a/hog/src/commands/endpoints/endpoints.test.ts
+++ b/hog/src/commands/endpoints/endpoints.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, test } from 'bun:test'
+import { z } from 'zod'
-import { runHog, TEST_CREDENTIALS } from '@/testing'
+import { readJson, runHog, TEST_CREDENTIALS } from '@/testing'
import { pushPlanLines } from './push'
@@ -94,7 +95,7 @@ describe('hog endpoints', () => {
result.stdout
.trim()
.split('\n')
- .map((line) => JSON.parse(line).name)
+ .map((line) => readJson(line, z.object({ name: z.string() })).name)
).toEqual(['weekly', 'by-type'])
})
@@ -157,7 +158,7 @@ describe('hog endpoints', () => {
})
expect(result.exitCode).toBe(1)
expect(result.files).toMatchObject(expected)
- expect(result.json().problems).toEqual([expect.objectContaining({ path: 'weekly.yaml' })])
+ expect(result.json()).toMatchObject({ problems: [expect.objectContaining({ path: 'weekly.yaml' })] })
})
test.each([
@@ -199,7 +200,17 @@ describe('hog endpoints', () => {
respond: server([TRENDS, HOGQL]),
})
expect(result.exitCode).toBe(exitCode)
- const plan = result.json().plan as { name: string; action: string; changes: { field: string }[] }[]
+ const { plan } = result.json(
+ z.object({
+ plan: z.array(
+ z.object({
+ name: z.string(),
+ action: z.string(),
+ changes: z.array(z.object({ field: z.string() })),
+ })
+ ),
+ })
+ )
expect(
plan
.filter((item) => item.action !== 'skip')
diff --git a/hog/src/commands/error-tracking/error-tracking.test.ts b/hog/src/commands/error-tracking/error-tracking.test.ts
index ed8218d1f481..531df17190d3 100644
--- a/hog/src/commands/error-tracking/error-tracking.test.ts
+++ b/hog/src/commands/error-tracking/error-tracking.test.ts
@@ -12,6 +12,7 @@ import {
} from 'node:fs'
import { tmpdir } from 'node:os'
import { basename, join } from 'node:path'
+import { z } from 'zod'
import { cleanUp, cssTargets } from '@/error-tracking/cleanup'
import { contentHash, SymbolDataType, writeSymbolData } from '@/error-tracking/container'
@@ -32,6 +33,12 @@ function project(files: Record<string, string>): string {
return folder
}
+const UploadOutput = z.looseObject({
+ symbolSets: z.array(z.looseObject({ contentHash: z.string() })).default([]),
+ skipped: z.array(z.object({ path: z.string(), reason: z.string() })).default([]),
+ release: z.unknown(),
+})
+
describe('hog error-tracking upload', () => {
test.each([
['the map ID from posthog.json', [], 'map-1'],
@@ -120,7 +127,7 @@ describe('hog error-tracking upload', () => {
cwd: folder,
useDisk: true,
})
- expect(result.json().symbolSets).toEqual([
+ expect(result.json(UploadOutput).symbolSets).toEqual([
expect.objectContaining({ chunkId: expect.stringMatching(/^[0-9a-f]{8}-[0-9a-f]{4}-7/) }),
])
})
@@ -152,7 +159,7 @@ describe('hog error-tracking upload', () => {
const result = await runHog(['et', 'inject'], { env: { CLAUDECODE: '1' }, cwd: folder, useDisk: true })
expect(result.exitCode).toBe(0)
- expect(result.json().skipped).toEqual([
+ expect(result.json(UploadOutput).skipped).toEqual([
{ path: expect.stringMatching(/dist\/a\.js$/), reason: expect.stringContaining(reason) },
])
expect(readFileSync(join(folder, untouched), 'utf8')).toBe(before)
@@ -180,7 +187,7 @@ describe('hog error-tracking upload', () => {
expect(result.exitCode).toBe(0)
const reasons = Object.fromEntries(
- result.json().skipped.map((skip: { path: string; reason: string }) => [basename(skip.path), skip.reason])
+ result.json(UploadOutput).skipped.map((skip) => [basename(skip.path), skip.reason])
)
expect(reasons).toEqual({
'page.js': 'the sourcemap is empty',
@@ -269,7 +276,9 @@ describe('hog error-tracking upload', () => {
useDisk: true,
})
expect(result.exitCode).toBe(0)
- expect(result.json().symbolSets).toEqual([expect.objectContaining({ path: composed, kind: 'hermes' })])
+ expect(result.json(UploadOutput).symbolSets).toEqual([
+ expect.objectContaining({ path: composed, kind: 'hermes' }),
+ ])
})
test('upload fails when Hermes bundles have no composed maps', async () => {
@@ -323,7 +332,9 @@ describe('hog error-tracking upload', () => {
useDisk: true,
})
expect(result.exitCode).toBe(exitCode)
- expect(exitCode === 0 ? JSON.stringify(result.json().symbolSets) : result.error().message).toContain(expected)
+ expect(
+ exitCode === 0 ? JSON.stringify(result.json(UploadOutput).symbolSets) : result.error().message
+ ).toContain(expected)
})
test.each([
@@ -348,7 +359,7 @@ describe('hog error-tracking upload', () => {
if (linkOutside) {
expect(result.exitCode).toBe(0)
- expect(result.json().symbolSets).toEqual([
+ expect(result.json(UploadOutput).symbolSets).toEqual([
expect.objectContaining({ path: 'apple/Shop.app.dSYM', status: 'planned' }),
])
} else {
@@ -382,7 +393,7 @@ describe('hog error-tracking upload', () => {
useDisk: true,
})
expect(result.exitCode).toBe(0)
- return result.json().symbolSets[0].contentHash
+ return result.json(UploadOutput).symbolSets[0]!.contentHash
}
try {
const all = await plan([])
@@ -407,7 +418,7 @@ describe('hog error-tracking upload', () => {
cwd: outside,
useDisk: true,
})
- expect(result.json().release).toEqual({
+ expect(result.json(UploadOutput).release).toEqual({
name: 'example-app',
version: 'c0ffee00c0ffee00c0ffee00c0ffee00c0ffee00',
id: null,
diff --git a/hog/src/commands/schema/schema.test.ts b/hog/src/commands/schema/schema.test.ts
index 850f99d5948f..5d3f730de02f 100644
--- a/hog/src/commands/schema/schema.test.ts
+++ b/hog/src/commands/schema/schema.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'bun:test'
-import { runHog, TEST_CREDENTIALS } from '@/testing'
+import { readJson, runHog, TEST_CREDENTIALS } from '@/testing'
const AGENT = { ...TEST_CREDENTIALS, CLAUDECODE: '1' }
@@ -32,7 +32,7 @@ describe('hog schema', () => {
})
expect(result.exitCode).toBe(0)
expect(result.files['/work/src/lib/posthog-typed.ts']).toBe('new')
- expect(JSON.parse(result.files['/work/posthog.json']!)).toEqual({
+ expect(readJson(result.files['/work/posthog.json']!)).toEqual({
errorTracking: { release: { name: 'web' } },
languages: {
typescript: {
@@ -68,7 +68,7 @@ describe('hog schema', () => {
})
expect(result.files[written]).toBe('generated')
- expect(JSON.parse(result.files['/work/posthog.json']!)).toMatchObject({
+ expect(readJson(result.files['/work/posthog.json']!)).toMatchObject({
$schema: expect.stringContaining('posthog.schema.json'),
languages: { typescript: { output_path: stored } },
})
diff --git a/hog/src/core/approval.test.ts b/hog/src/core/approval.test.ts
index 47239db3a828..a4ad6182b0ea 100644
--- a/hog/src/core/approval.test.ts
+++ b/hog/src/core/approval.test.ts
@@ -3,7 +3,13 @@ import { describe, expect, test } from 'bun:test'
import { COMMANDS } from '@/commands'
import { fakeRuntime } from '@/testing'
-import { resumeApproval, type ApprovalFlow, type PendingApproval, type PollOutcome } from './approval'
+import {
+ PendingApprovalSchema,
+ resumeApproval,
+ type ApprovalFlow,
+ type PendingApproval,
+ type PollOutcome,
+} from './approval'
import { Context } from './context'
import { HogError } from './errors'
import type { GlobalOptions } from './globals'
@@ -38,6 +44,7 @@ describe('resumeApproval', () => {
const flow: ApprovalFlow<PendingApproval, string> = {
command: 'hog thing start',
noun: 'thing',
+ pending: PendingApprovalSchema,
start: async () => PENDING,
poll: async () => {
polledAt.push(runtime.now() - START)
diff --git a/hog/src/core/approval.ts b/hog/src/core/approval.ts
index 11a3875a844f..5b63beaad625 100644
--- a/hog/src/core/approval.ts
+++ b/hog/src/core/approval.ts
@@ -1,19 +1,24 @@
+import { z } from 'zod'
+
import type { Context } from './context'
import { HogError } from './errors'
+import { parseJsonOrUndefined } from './json'
import { escapeControls, type Output } from './output'
import { hogFile } from './paths'
import type { Spinner } from './prompts'
import type { Runtime } from './runtime'
// Something a person approves in a browser, saved in hog's folder while hog waits for them.
-export interface PendingApproval {
- url: string
+export const PendingApprovalSchema = z.object({
+ url: z.string(),
// The code the approval page shows, so the person can check it is the request they started.
- userCode?: string
+ userCode: z.string().optional(),
// Epoch milliseconds after which PostHog forgets the request.
- expiresAt: number
- intervalSeconds: number
-}
+ expiresAt: z.number(),
+ intervalSeconds: z.number(),
+})
+
+export type PendingApproval = z.output<typeof PendingApprovalSchema>
export type PollOutcome<Value> =
| { state: 'pending' }
@@ -32,6 +37,8 @@ export interface ApprovalFlow<Pending extends PendingApproval, Value> {
command: string
// What the person approves, in copy and in error codes, like `login`.
noun: string
+ // Reads the pending item back from its file. An item hog can't read counts as none.
+ pending: z.ZodType<Pending>
start: () => Promise<Pending>
// Whether a pending item from an earlier run can stand in for a new one, so an agent that
// retries hands the person the link it already shared.
@@ -43,7 +50,7 @@ export interface ApprovalFlow<Pending extends PendingApproval, Value> {
// What resuming a pending item needs. It can't start a new one.
export type ApprovalWait<Pending extends PendingApproval, Value> = Pick<
ApprovalFlow<Pending, Value>,
- 'command' | 'noun' | 'poll'
+ 'command' | 'noun' | 'pending' | 'poll'
>
export type ApprovalResult<Value> =
@@ -71,10 +78,10 @@ function pendingPath(runtime: Runtime, command: string): string {
async function readPending<Pending extends PendingApproval>(
runtime: Runtime,
- command: string
+ flow: ApprovalWait<Pending, unknown>
): Promise<Pending | undefined> {
try {
- return JSON.parse(await runtime.readFile(pendingPath(runtime, command))) as Pending
+ return parseJsonOrUndefined(await runtime.readFile(pendingPath(runtime, flow.command)), flow.pending)
} catch {
return undefined
}
@@ -89,7 +96,7 @@ async function startPending<Pending extends PendingApproval, Value>(
runtime: Runtime,
flow: ApprovalFlow<Pending, Value>
): Promise<Pending> {
- const existing = await readPending<Pending>(runtime, flow.command)
+ const existing = await readPending(runtime, flow)
if (existing && existing.expiresAt - runtime.now() > REUSE_MARGIN_MS && flow.reuse?.(existing)) {
return existing
}
@@ -229,7 +236,7 @@ export async function resumeApproval<Pending extends PendingApproval, Value>(
ctx: Context,
flow: ApprovalWait<Pending, Value>
): Promise<Value> {
- const pending = await readPending<Pending>(ctx.runtime, flow.command)
+ const pending = await readPending(ctx.runtime, flow)
if (!pending) {
throw new HogError(
`no_pending_${flow.noun}`,
diff --git a/hog/src/core/config.ts b/hog/src/core/config.ts
index 7cea1bbefc1a..94d0440c0776 100644
--- a/hog/src/core/config.ts
+++ b/hog/src/core/config.ts
@@ -4,6 +4,7 @@ import { z } from 'zod'
import { HostSchema } from '@/posthog/host'
import { HogError } from './errors'
+import { JsonObject, parseJson } from './json'
import type { Runtime } from './runtime'
export const CONFIG_FILE = 'posthog.json'
@@ -156,11 +157,9 @@ export async function findConfig(runtime: Runtime): Promise<string | undefined>
export function validateConfig(contents: string): { config?: PostHogConfig; problems: ConfigProblem[] } {
let raw: unknown
try {
- raw = JSON.parse(contents)
+ raw = parseJson(contents, z.unknown(), { code: 'invalid_config' })
} catch (error) {
- return {
- problems: [{ path: '', message: `Not valid JSON: ${error instanceof Error ? error.message : error}` }],
- }
+ return { problems: [{ path: '', message: error instanceof Error ? error.message : String(error) }] }
}
const parsed = PostHogConfig.safeParse(raw)
if (parsed.success) {
@@ -202,7 +201,12 @@ export async function updateConfig(
change: (raw: Record<string, unknown>) => void
): Promise<string> {
const path = loaded?.path ?? join(runtime.cwd(), CONFIG_FILE)
- const raw: Record<string, unknown> = loaded ? JSON.parse(await runtime.readFile(path)) : { $schema: SCHEMA_URL }
+ const raw = loaded
+ ? parseJson(await runtime.readFile(path), JsonObject, {
+ code: 'invalid_config',
+ context: `${path} is not valid.`,
+ })
+ : { $schema: SCHEMA_URL }
change(raw)
await runtime.writeFile(path, `${JSON.stringify(raw, null, 2)}\n`)
return path
diff --git a/hog/src/core/file-lock.ts b/hog/src/core/file-lock.ts
index 08cd35cb3003..8bdaf7d03b30 100644
--- a/hog/src/core/file-lock.ts
+++ b/hog/src/core/file-lock.ts
@@ -1,5 +1,7 @@
import { randomUUID } from 'node:crypto'
+import { z } from 'zod'
+import { parseJsonOrUndefined } from './json'
import { hogFile } from './paths'
import type { Runtime } from './runtime'
@@ -10,14 +12,13 @@ const LOCK_STALE_MS = 180_000
const LOCK_REFRESH_MS = 60_000
const LOCK_POLL_MS = 250
-interface LockContents {
- id?: string
- createdAt?: number
-}
+const LockContentsSchema = z.object({ id: z.string().optional(), createdAt: z.number().optional() })
+
+type LockContents = z.output<typeof LockContentsSchema>
async function readLock(runtime: Runtime, lock: string): Promise<LockContents | undefined> {
try {
- return JSON.parse(await runtime.readFile(lock)) as LockContents
+ return parseJsonOrUndefined(await runtime.readFile(lock), LockContentsSchema)
} catch {
return undefined
}
diff --git a/hog/src/core/globals.test.ts b/hog/src/core/globals.test.ts
index ff1b9c98cc33..935f013b7ea1 100644
--- a/hog/src/core/globals.test.ts
+++ b/hog/src/core/globals.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'bun:test'
-import { runHog, TEST_CREDENTIALS } from '@/testing'
+import { readJson, runHog, TEST_CREDENTIALS } from '@/testing'
// hog logins for each host, the first one the last login.
function storedLogins(...hosts: string[]): { files: Record<string, string>; keychain: Record<string, string> } {
@@ -42,7 +42,7 @@ describe('global options', () => {
files: { '/work/a.yaml': 'name: a\nquery: SELECT 1\n' },
respond: () => ({ status: 200, body: { results: [] } }),
})
- expect(result.json().plan).toEqual([expect.objectContaining({ action: 'create' })])
+ expect(result.json()).toMatchObject({ plan: [expect.objectContaining({ action: 'create' })] })
expect(result.exitCode).toBe(0)
})
@@ -110,7 +110,7 @@ describe('global options', () => {
test('HOG_OUTPUT picks the format', async () => {
const result = await runHog(['commands'], { env: { HOG_OUTPUT: 'json' }, stdoutIsTTY: true })
- expect(() => JSON.parse(result.stdout)).not.toThrow()
+ expect(() => readJson(result.stdout)).not.toThrow()
})
test('--skip-ssl-verification turns off certificate checks', async () => {
diff --git a/hog/src/core/json.ts b/hog/src/core/json.ts
new file mode 100644
index 000000000000..f79a5b9ed5e1
--- /dev/null
+++ b/hog/src/core/json.ts
@@ -0,0 +1,56 @@
+import { z } from 'zod'
+
+import { failWith, parseOrThrow, type ParseFailure } from './schema'
+
+// The only module that calls JSON.parse. Its result is `unknown` until a schema checks it, so no
+// caller casts JSON to the type it hopes for.
+
+export const JsonObject = z.record(z.string(), z.unknown())
+
+export function parseJson<S extends z.ZodType>(text: string, schema: S, failure: ParseFailure): z.output<S> {
+ let raw: unknown
+ try {
+ raw = JSON.parse(text)
+ } catch (error) {
+ throw failWith(failure, `Not valid JSON: ${error instanceof Error ? error.message : String(error)}`)
+ }
+ return parseOrThrow(schema, raw, failure)
+}
+
+// For files hog wrote itself and data it can do without: anything it can't read counts as missing.
+export function parseJsonOrUndefined<S extends z.ZodType>(text: string, schema: S): z.output<S> | undefined {
+ try {
+ return schema.safeParse(JSON.parse(text)).data
+ } catch {
+ return undefined
+ }
+}
+
+// Keeps integers beyond JavaScript's safe range as their source text, so a ClickHouse UInt64
+// survives the round trip to output unchanged, as it does in posthog-cli. Undefined for text that
+// isn't JSON. Callers check the result against a schema of their own.
+export function parseLosslessJson(text: string): unknown {
+ try {
+ return JSON.parse(text, (_key, value, context) =>
+ typeof value === 'number' &&
+ !Number.isSafeInteger(value) &&
+ context.source &&
+ /^-?\d+$/.test(context.source)
+ ? JSON.rawJSON(context.source)
+ : value
+ )
+ } catch {
+ return undefined
+ }
+}
+
+// Decodes one escaped JSON string literal, quotes included. Undefined when it isn't one. A JSON
+// reader that keeps key order calls this for every escaped string, so it skips zod.
+export function decodeJsonString(literal: string): string | undefined {
+ try {
+ const value: unknown = JSON.parse(literal)
+ return typeof value === 'string' ? value : undefined
+ } catch {
+ return undefined
+ }
+}
diff --git a/hog/src/core/output.ts b/hog/src/core/output.ts
index dd187b3cae00..bb2cdae78655 100644
--- a/hog/src/core/output.ts
+++ b/hog/src/core/output.ts
@@ -130,7 +130,7 @@ export class Output {
// posthog-cli prints JSON through serde_json without `preserve_order`, so object keys come
// out sorted. Sorting here keeps hog's JSON the same for nested objects.
function sortKeys(value: unknown): unknown {
- // Integers too large for a JS number stay as their source text (see parseLossless).
+ // Integers too large for a JS number stay as their source text (see `parseLosslessJson` in core/json.ts).
if (JSON.isRawJSON(value)) {
return value
}
diff --git a/hog/src/core/raw-json.ts b/hog/src/core/raw-json.ts
deleted file mode 100644
index 0b24e27a5d0b..000000000000
--- a/hog/src/core/raw-json.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-// Parses JSON and keeps integers beyond JavaScript's safe range as their source text, so a
-// ClickHouse UInt64 survives the round trip to output unchanged, as it does in posthog-cli.
-export function parseLossless(text: string): unknown {
- return JSON.parse(text, (_key, value, context) =>
- typeof value === 'number' && !Number.isSafeInteger(value) && context.source && /^-?\d+$/.test(context.source)
- ? JSON.rawJSON(context.source)
- : value
- )
-}
diff --git a/hog/src/core/schema.ts b/hog/src/core/schema.ts
index 1ca18b2af489..6af07bff672c 100644
--- a/hog/src/core/schema.ts
+++ b/hog/src/core/schema.ts
@@ -32,11 +32,10 @@ export function parseOrThrow<S extends z.ZodType>(schema: S, raw: unknown, failu
if (parsed.success) {
return parsed.data
}
- const message = getErrorMessage(parsed.error)
- throw new HogError(
- failure.code,
- failure.context ? `${failure.context} ${message}` : message,
- failure.hint,
- failure.exitCode
- )
+ throw failWith(failure, getErrorMessage(parsed.error))
+}
+
+export function failWith(failure: ParseFailure, message: string): HogError {
+ const full = failure.context ? `${failure.context} ${message}` : message
+ return new HogError(failure.code, full, failure.hint, failure.exitCode)
}
diff --git a/hog/src/core/telemetry.ts b/hog/src/core/telemetry.ts
index 339e7105d27c..06574e271427 100644
--- a/hog/src/core/telemetry.ts
+++ b/hog/src/core/telemetry.ts
@@ -1,10 +1,12 @@
import { createHash, randomUUID } from 'node:crypto'
+import { z } from 'zod'
import type { AccessKey } from '@/posthog/access-key'
import { posthogFile, type Credentials } from '@/posthog/credentials'
import { VERSION } from '@/version'
import type { Mode } from './agent'
+import { parseJsonOrUndefined } from './json'
import type { Env, Runtime } from './runtime'
// PostHog's own project, where posthog-cli release builds send their events too.
@@ -91,9 +93,17 @@ interface Identity {
project?: string
}
-interface CachedIdentity extends Identity {
- checkedAt: number
-}
+const CachedIdentitySchema = z.object({
+ checkedAt: z.number(),
+ distinctId: z.string().optional(),
+ organization: z.string().optional(),
+ project: z.string().optional(),
+})
+
+type CachedIdentity = z.output<typeof CachedIdentitySchema>
+
+// hog writes this file itself, so a cache it can't read is broken and hog starts a new one.
+const IdentityCacheSchema = z.record(z.string(), CachedIdentitySchema)
// Only PostHog Cloud users exist in the project telemetry goes to.
function isCloudHost(host: string): boolean {
@@ -219,15 +229,7 @@ export class Telemetry {
const cacheKey = createHash('sha256').update(`${key.host}\n${key.projectId}\n${key.loginKey}`).digest('hex')
let cache: Record<string, CachedIdentity> = {}
try {
- const parsed: unknown = JSON.parse(await this.runtime.readFile(path))
- // A cache that isn't an object is broken, so hog starts a new one instead of failing the command.
- if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) {
- cache = Object.fromEntries(
- Object.entries(parsed).filter(
- ([, entry]) => typeof (entry as Partial<CachedIdentity> | null)?.checkedAt === 'number'
- )
- ) as Record<string, CachedIdentity>
- }
+ cache = parseJsonOrUndefined(await this.runtime.readFile(path), IdentityCacheSchema) ?? {}
} catch {
// No cache yet.
}
diff --git a/hog/src/error-tracking/json.ts b/hog/src/error-tracking/json.ts
index 7aad13f85f85..2ba1bccf9080 100644
--- a/hog/src/error-tracking/json.ts
+++ b/hog/src/error-tracking/json.ts
@@ -1,3 +1,5 @@
+import { decodeJsonString } from '@/core/json'
+
// A JSON reader and writer that produce the same bytes as serde_json 1.0.145 with default features,
// which posthog-cli uses to read and rewrite sourcemaps. The bytes feed uploads and content hashes,
// so `JSON.parse` and `JSON.stringify` are not enough: they reorder integer-like keys, round large
@@ -186,7 +188,10 @@ export class JsonParser {
if (!escaped) {
return text.slice(start + 1, end)
}
- const decoded = JSON.parse(text.slice(start, end + 1)) as string
+ const decoded = decodeJsonString(text.slice(start, end + 1))
+ if (decoded === undefined) {
+ this.fail('invalid escape')
+ }
if (LONE_SURROGATE.test(decoded)) {
this.fail('lone leading surrogate in hex escape')
}
diff --git a/hog/src/error-tracking/release.ts b/hog/src/error-tracking/release.ts
index 432428b19f87..9ca5069f19b4 100644
--- a/hog/src/error-tracking/release.ts
+++ b/hog/src/error-tracking/release.ts
@@ -2,7 +2,7 @@ import { HogError } from '@/core/errors'
import { contentHash } from './container'
import type { GitInfo } from './git'
-import { ApiFailure, type SymbolSetClient } from './upload'
+import { ApiFailure, errorCode, type SymbolSetClient } from './upload'
export interface Release {
id: string
@@ -97,7 +97,7 @@ export async function fetchOrCreateRelease(
const created = (await client.api('POST', 'error_tracking/releases', body)) as Release
return { ...created, id: created.id.toLowerCase() }
} catch (error) {
- if (error instanceof ApiFailure && isStructuredCode(error.body, 'release_hash_in_use')) {
+ if (error instanceof ApiFailure && errorCode(error.body) === 'release_hash_in_use') {
const raced = await lookup(client, hashId)
if (raced) {
return { ...raced, id: raced.id.toLowerCase() }
@@ -106,11 +106,3 @@ export async function fetchOrCreateRelease(
throw new HogError('release_create_failed', `hog couldn't create the release: ${(error as Error).message}`)
}
}
-
-function isStructuredCode(body: string, code: string): boolean {
- try {
- return (JSON.parse(body) as { code?: unknown }).code === code
- } catch {
- return false
- }
-}
diff --git a/hog/src/error-tracking/upload.ts b/hog/src/error-tracking/upload.ts
index a7b6f6c0effa..2d48f49fe15e 100644
--- a/hog/src/error-tracking/upload.ts
+++ b/hog/src/error-tracking/upload.ts
@@ -1,4 +1,7 @@
+import { z } from 'zod'
+
import { HogError } from '@/core/errors'
+import { parseJson, parseJsonOrUndefined } from '@/core/json'
import type { Runtime } from '@/core/runtime'
import { USER_AGENT } from '@/posthog/api'
import { hostUrl } from '@/posthog/host'
@@ -45,6 +48,12 @@ function transferTimeout(bytes: number): number {
}
const RATE_LIMIT_PER_MINUTE = 480
+const ErrorBody = z.looseObject({ code: z.unknown() })
+
+export function errorCode(body: string): unknown {
+ return parseJsonOrUndefined(body, ErrorBody)?.code
+}
+
class ApiFailure extends Error {
constructor(
readonly status: number | undefined,
@@ -60,12 +69,7 @@ class ApiFailure extends Error {
// posthog-cli matches error codes in the parsed body, or anywhere in the raw text as a fallback.
hasCode(code: string): boolean {
- try {
- if ((JSON.parse(this.body) as { code?: unknown }).code === code) {
- return true
- }
- } catch {}
- return this.body.includes(code)
+ return errorCode(this.body) === code || this.body.includes(code)
}
}
@@ -184,7 +188,12 @@ export class SymbolSetClient {
if (!response.ok) {
throw new ApiFailure(response.status, text, url.toString())
}
- return text ? JSON.parse(text) : undefined
+ return text
+ ? parseJson(text, z.unknown(), {
+ code: 'unexpected_response',
+ context: `PostHog answered ${url} with a body hog can't read.`,
+ })
+ : undefined
}
async upload(uploads: SymbolSetUpload[], options: UploadOptions): Promise<UploadOutcome> {
diff --git a/hog/src/posthog/api.ts b/hog/src/posthog/api.ts
index f6a487402434..8eed32462994 100644
--- a/hog/src/posthog/api.ts
+++ b/hog/src/posthog/api.ts
@@ -1,5 +1,5 @@
import { HogError } from '@/core/errors'
-import { parseLossless } from '@/core/raw-json'
+import { parseLosslessJson } from '@/core/json'
import type { RuntimeFetch } from '@/core/runtime'
import { VERSION } from '@/version'
@@ -59,12 +59,8 @@ export async function sendRequest(fetchFn: RuntimeFetch, url: URL, request: Http
'Check the host and your network connection.'
)
}
- let json: unknown
- try {
- json = text ? parseLossless(text) : undefined
- } catch {
- json = undefined
- }
+ // Callers check `json` against a schema of their own. A body that isn't JSON leaves it undefined.
+ const json = text ? parseLosslessJson(text) : undefined
return { status: response.status, ok: response.ok, json, text }
}
diff --git a/hog/src/posthog/credential-store/file.ts b/hog/src/posthog/credential-store/file.ts
index e67bf0725370..ff19262cafab 100644
--- a/hog/src/posthog/credential-store/file.ts
+++ b/hog/src/posthog/credential-store/file.ts
@@ -1,8 +1,9 @@
+import { JsonObject, parseJsonOrUndefined } from '@/core/json'
import { hogFile } from '@/core/paths'
import type { Runtime } from '@/core/runtime'
import { hostKey, HostSchema, type Host } from '@/posthog/host'
-import { parseJson, parseSecret } from './helpers'
+import { StoredSecretSchema } from './helpers'
import type { CredentialStore, StoredSecret } from './types'
// One JSON file, readable only by its owner, for machines without a keychain hog can use.
@@ -25,10 +26,7 @@ export class FileStore implements CredentialStore {
}
throw error
}
- const parsed = parseJson(text)
- return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)
- ? (parsed as Record<string, unknown>)
- : {}
+ return parseJsonOrUndefined(text, JsonObject) ?? {}
}
async hosts(): Promise<Host[]> {
@@ -36,7 +34,8 @@ export class FileStore implements CredentialStore {
}
async read(host: Host): Promise<StoredSecret | undefined> {
- return parseSecret((await this.readAll())[hostKey(host)])
+ // A stored value hog doesn't recognize, such as one a later version wrote, reads as no login.
+ return StoredSecretSchema.safeParse((await this.readAll())[hostKey(host)]).data
}
async write(host: Host, secret: StoredSecret): Promise<void> {
diff --git a/hog/src/posthog/credential-store/helpers.ts b/hog/src/posthog/credential-store/helpers.ts
index 6b31affe4569..91efb83e74f6 100644
--- a/hog/src/posthog/credential-store/helpers.ts
+++ b/hog/src/posthog/credential-store/helpers.ts
@@ -2,22 +2,7 @@ import { z } from 'zod'
import { OAuthAccessTokenSchema, OAuthRefreshTokenSchema, PersonalApiKeySchema } from '@/posthog/tokens'
-import type { StoredSecret } from './types'
-
-export function parseJson(text: string): unknown {
- try {
- return JSON.parse(text)
- } catch {
- return undefined
- }
-}
-
-const StoredSecretSchema = z.union([
+export const StoredSecretSchema = z.union([
z.object({ apiKey: PersonalApiKeySchema }),
z.object({ accessToken: OAuthAccessTokenSchema, refreshToken: OAuthRefreshTokenSchema }),
])
-
-// A stored value hog doesn't recognize, such as one a later version wrote, reads as no login.
-export function parseSecret(value: unknown): StoredSecret | undefined {
- return StoredSecretSchema.safeParse(value).data
-}
diff --git a/hog/src/posthog/credential-store/keychain.ts b/hog/src/posthog/credential-store/keychain.ts
index b2717af0924a..49b0fc271824 100644
--- a/hog/src/posthog/credential-store/keychain.ts
+++ b/hog/src/posthog/credential-store/keychain.ts
@@ -1,10 +1,11 @@
import { randomUUID } from 'node:crypto'
import { HogError } from '@/core/errors'
+import { parseJsonOrUndefined } from '@/core/json'
import type { Runtime } from '@/core/runtime'
import { hostKey, type Host } from '@/posthog/host'
-import { parseJson, parseSecret } from './helpers'
+import { StoredSecretSchema } from './helpers'
import type { CredentialStore, StoredSecret } from './types'
// The keychain dialog on macOS never times out by itself, and a person may not notice it, so hog
@@ -78,7 +79,7 @@ export class KeychainStore implements CredentialStore {
async read(host: Host): Promise<StoredSecret | undefined> {
const value = await keychainCall(this.runtime, () => this.runtime.secrets.get(hostKey(host)))
- return value === null ? undefined : parseSecret(parseJson(value))
+ return value === null ? undefined : parseJsonOrUndefined(value, StoredSecretSchema)
}
async write(host: Host, secret: StoredSecret, options: { removeIfLate?: boolean } = {}): Promise<void> {
diff --git a/hog/src/posthog/credentials.test.ts b/hog/src/posthog/credentials.test.ts
index ac0c97682721..607ff85df830 100644
--- a/hog/src/posthog/credentials.test.ts
+++ b/hog/src/posthog/credentials.test.ts
@@ -172,7 +172,7 @@ describe('resolveCredentials', () => {
'a login in the file store',
{
...settings(EU, { [EU]: 'file' }),
- '/home/test/.config/hog/credentials.json': JSON.stringify({ [EU]: JSON.parse(login('phx_file')) }),
+ '/home/test/.config/hog/credentials.json': JSON.stringify({ [EU]: { apiKey: 'phx_file' } }),
},
{},
{},
diff --git a/hog/src/posthog/device-auth.ts b/hog/src/posthog/device-auth.ts
index 25df43731cd2..9e86b155cafc 100644
--- a/hog/src/posthog/device-auth.ts
+++ b/hog/src/posthog/device-auth.ts
@@ -1,9 +1,11 @@
-import type { PendingApproval, PollOutcome } from '@/core/approval'
+import { z } from 'zod'
+
+import { PendingApprovalSchema, type PollOutcome } from '@/core/approval'
import { HogError } from '@/core/errors'
import { escapeControls } from '@/core/output'
import type { Runtime } from '@/core/runtime'
-import { hostKey, hostUrl, type Host } from './host'
+import { hostKey, HostSchema, hostUrl, type Host } from './host'
import { parseProjectId } from './ids'
import { HOG_CLIENT_ID, OAuthLogin, oauthPost, type TokenResponse } from './oauth'
@@ -86,12 +88,14 @@ export const READ_ONLY_LOGIN_SCOPES: readonly string[] = [
// A login the person still has to approve in the browser. `deviceCode` redeems the tokens once
// they approve, so it stays in a private file and never reaches the output.
-export interface PendingLogin extends PendingApproval {
- host: Host
- deviceCode: string
- userCode: string
- scopes: string[]
-}
+export const PendingLoginSchema = PendingApprovalSchema.extend({
+ host: HostSchema,
+ deviceCode: z.string(),
+ userCode: z.string(),
+ scopes: z.array(z.string()),
+})
+
+export type PendingLogin = z.output<typeof PendingLoginSchema>
// PostHog refuses a client it hasn't verified with `unauthorized_client`, and its description says why.
function unauthorizedClient(host: Host, body: { error?: string; error_description?: unknown }): HogError | undefined {
diff --git a/hog/src/posthog/login/flow.ts b/hog/src/posthog/login/flow.ts
index 49dcff03b22e..7e3cd8e866b4 100644
--- a/hog/src/posthog/login/flow.ts
+++ b/hog/src/posthog/login/flow.ts
@@ -6,7 +6,7 @@ import { FileStore } from '@/posthog/credential-store/file'
import { probeKeychain } from '@/posthog/credential-store/keychain'
import { openStore } from '@/posthog/credential-store/open'
import type { StoreChoice } from '@/posthog/credential-store/types'
-import { pollDeviceLogin, startDeviceLogin, type PendingLogin } from '@/posthog/device-auth'
+import { pollDeviceLogin, PendingLoginSchema, startDeviceLogin, type PendingLogin } from '@/posthog/device-auth'
import type { Host } from '@/posthog/host'
import type { ProjectId } from '@/posthog/ids'
import { withLoginLock, type OAuthLogin } from '@/posthog/oauth'
@@ -32,6 +32,7 @@ export function loginWait(runtime: Runtime): ApprovalWait<PendingLogin, OAuthLog
return {
command: LOGIN_COMMAND,
noun: 'login',
+ pending: PendingLoginSchema,
poll: (pending, timeoutMs) => pollDeviceLogin(runtime, pending, timeoutMs),
}
}
@@ -46,7 +47,7 @@ export function loginFlow(
return {
...loginWait(runtime),
start: () => startDeviceLogin(runtime, host, scopes),
- reuse: (existing) => existing.host === host && existing.scopes?.join(' ') === scopes.join(' '),
+ reuse: (existing) => existing.host === host && existing.scopes.join(' ') === scopes.join(' '),
}
}
diff --git a/hog/src/posthog/login/stored.test.ts b/hog/src/posthog/login/stored.test.ts
index e75291904cbd..9aa6b8668911 100644
--- a/hog/src/posthog/login/stored.test.ts
+++ b/hog/src/posthog/login/stored.test.ts
@@ -1,11 +1,12 @@
import { describe, expect, test } from 'bun:test'
+import { z } from 'zod'
import { PersonalAPIKey } from '@/posthog/access-key'
import { FileStore } from '@/posthog/credential-store/file'
import { parseHost, type Host } from '@/posthog/host'
import { parseProjectId } from '@/posthog/ids'
import { parsePersonalApiKey } from '@/posthog/tokens'
-import { fakeRuntime } from '@/testing'
+import { fakeRuntime, readJson } from '@/testing'
import { writeLogin } from './stored'
@@ -26,8 +27,12 @@ describe('writeLogin', () => {
writeLogin(runtime, store, login(US, 'phx_us')),
])
- expect(Object.keys(JSON.parse(runtime.files[store.path] ?? '{}')).sort()).toEqual([EU, US])
- const settings = JSON.parse(runtime.files['/home/test/.config/hog/settings.json'] ?? '{}')
+ const credentials = readJson(runtime.files[store.path] ?? '{}', z.record(z.string(), z.unknown()))
+ expect(Object.keys(credentials).sort()).toEqual([EU, US])
+ const settings = readJson(
+ runtime.files['/home/test/.config/hog/settings.json'] ?? '{}',
+ z.object({ logins: z.record(z.string(), z.unknown()) })
+ )
expect(Object.keys(settings.logins).sort()).toEqual([EU, US])
})
})
diff --git a/hog/src/posthog/oauth.test.ts b/hog/src/posthog/oauth.test.ts
index 89e3f3fa597e..c39889c0ab79 100644
--- a/hog/src/posthog/oauth.test.ts
+++ b/hog/src/posthog/oauth.test.ts
@@ -1,9 +1,10 @@
import { describe, expect, test } from 'bun:test'
import { createHash } from 'node:crypto'
+import { z } from 'zod'
import { HogError } from '@/core/errors'
import type { Secrets } from '@/core/runtime'
-import { fakeRuntime, type FakeRuntime } from '@/testing'
+import { fakeRuntime, readJson, type FakeRuntime } from '@/testing'
import { PostHogApi } from './api'
import { resolveCredentials, type Credentials } from './credentials'
@@ -11,6 +12,8 @@ import { parseHost } from './host'
import { KeySession } from './key-session'
import { HOG_CLIENT_ID } from './oauth'
+const SettingsLogins = z.object({ logins: z.record(z.string(), z.record(z.string(), z.unknown())) })
+
const EU = 'https://eu.posthog.com'
const START = Date.UTC(2026, 0, 1)
const SETTINGS_FILE = '/home/test/.config/hog/settings.json'
@@ -77,7 +80,7 @@ describe('OAuth credentials', () => {
expect(outcome).toMatchObject(expected)
expect(tokenRequests(runtime)).toHaveLength(1)
expect(runtime.requests.filter((request) => request.url.includes('/api/'))).toHaveLength(apiCalls)
- expect(JSON.parse(runtime.keychain[EU] ?? '{}').accessToken).toBe(renewedToken)
+ expect(readJson(runtime.keychain[EU] ?? '{}')).toMatchObject({ accessToken: renewedToken })
})
test('a 401 that a renewed token gets too stops renewing for the rest of the command', async () => {
@@ -106,7 +109,7 @@ describe('OAuth credentials', () => {
respond: () => ({ status: 401, body: { detail: 'Invalid token' } }),
})
const api = new PostHogApi(sessionFor(runtime, await resolveCredentials(runtime, {})), runtime.fetch)
- const replaced = JSON.parse(settings(3_600_000))
+ const replaced = readJson(settings(3_600_000), SettingsLogins)
replaced.logins[EU] = { ...replaced.logins[EU], projectId: '99', loginId: 'login-2' }
runtime.files[SETTINGS_FILE] = JSON.stringify(replaced)
runtime.keychain[EU] = secret('pha_other')
@@ -145,8 +148,8 @@ describe('OAuth credentials', () => {
expect(tokenRequests(runtime).map((request) => request.body)).toEqual([
{ grant_type: 'refresh_token', refresh_token: 'phr_old', client_id: HOG_CLIENT_ID },
])
- expect(JSON.parse(runtime.keychain[EU] ?? '{}')).toEqual({ accessToken: 'pha_new', refreshToken: 'phr_old' })
- expect(JSON.parse(runtime.files[SETTINGS_FILE] ?? '{}').logins[EU]).toMatchObject({
+ expect(readJson(runtime.keychain[EU] ?? '{}')).toEqual({ accessToken: 'pha_new', refreshToken: 'phr_old' })
+ expect(readJson(runtime.files[SETTINGS_FILE] ?? '{}', SettingsLogins).logins[EU]).toMatchObject({
expiresAt: START + 3_600_000,
scopes: ['openid'],
loginId: 'login-1',
@@ -171,7 +174,7 @@ describe('OAuth credentials', () => {
expect(credentials.key).toMatchObject({ token: 'pha_new' })
expect(tokenRequests(runtime)).toHaveLength(2)
- expect(JSON.parse(runtime.keychain[EU] ?? '{}')).toEqual({ accessToken: 'pha_new', refreshToken: 'phr_new' })
+ expect(readJson(runtime.keychain[EU] ?? '{}')).toEqual({ accessToken: 'pha_new', refreshToken: 'phr_new' })
})
test('use a token with time left as it is', async () => {
diff --git a/hog/src/posthog/settings.ts b/hog/src/posthog/settings.ts
index 28fc28172b10..ee35fb6556e7 100644
--- a/hog/src/posthog/settings.ts
+++ b/hog/src/posthog/settings.ts
@@ -1,5 +1,6 @@
import { z } from 'zod'
+import { parseJsonOrUndefined } from '@/core/json'
import { hogFile } from '@/core/paths'
import type { Runtime } from '@/core/runtime'
import { StoreChoiceSchema } from '@/posthog/credential-store/types'
@@ -42,6 +43,12 @@ export interface HogSettings {
host?: Host
}
+// Each part of the file is read on its own, so one entry hog doesn't recognize loses only itself.
+const SettingsFileSchema = z.looseObject({
+ logins: z.record(z.string(), z.unknown()).optional().catch(undefined),
+ host: HostSchema.optional().catch(undefined),
+})
+
const SETTINGS_FILE = 'settings.json'
function settingsPath(runtime: Runtime): string {
@@ -68,23 +75,15 @@ export async function readSettings(runtime: Runtime): Promise<HogSettings> {
}
throw error
}
- let parsed: unknown
- try {
- parsed = JSON.parse(text)
- } catch {
- return {}
- }
- const raw = (typeof parsed === 'object' && parsed !== null ? parsed : {}) as Record<string, unknown>
+ const raw = parseJsonOrUndefined(text, SettingsFileSchema) ?? {}
const logins: Partial<Record<HostKey, LoginRecord>> = {}
- if (typeof raw.logins === 'object' && raw.logins !== null) {
- for (const [host, value] of Object.entries(raw.logins)) {
- const record = parseLoginRecord(host, value)
- if (record) {
- logins[hostKey(record.host)] = record
- }
+ for (const [host, value] of Object.entries(raw.logins ?? {})) {
+ const record = parseLoginRecord(host, value)
+ if (record) {
+ logins[hostKey(record.host)] = record
}
}
- return { logins, host: HostSchema.safeParse(raw.host).data }
+ return { logins, host: raw.host }
}
export function loginFor(settings: HogSettings, host: Host): LoginRecord | undefined {
diff --git a/hog/src/testing.ts b/hog/src/testing.ts
index 9b33ef8f2ff8..3b01c895d3df 100644
--- a/hog/src/testing.ts
+++ b/hog/src/testing.ts
@@ -1,7 +1,9 @@
import { existsSync, readFileSync, statSync } from 'node:fs'
+import { z } from 'zod'
import type { ApiToolLibrary } from '@posthog/mcp/src/cli/library-types'
+import { parseJson, parseJsonOrUndefined } from '@/core/json'
import type { Prompts } from '@/core/prompts'
import type { Env, Runtime, Secrets } from '@/core/runtime'
@@ -60,11 +62,7 @@ function fakePrompts(answers: string[]): Prompts {
// Requests carry JSON, except OAuth requests, which are form encoded.
function parseBody(body: string): unknown {
- try {
- return JSON.parse(body)
- } catch {
- return Object.fromEntries(new URLSearchParams(body))
- }
+ return parseJsonOrUndefined(body, z.unknown()) ?? Object.fromEntries(new URLSearchParams(body))
}
function memorySecrets(items: Record<string, string>): Secrets {
@@ -270,8 +268,32 @@ export interface HogRun {
elapsedMs: number
tls: { verify: boolean }
keychain: Record<string, string>
- json: () => any
- error: () => any
+ json(): unknown
+ json<S extends z.ZodType>(schema: S): z.output<S>
+ error(): ErrorOutput
+}
+
+const ErrorOutputSchema = z.object({
+ error: z.object({ code: z.string(), message: z.string(), hint: z.string().optional() }),
+})
+
+type ErrorOutput = z.output<typeof ErrorOutputSchema>['error']
+
+const UNREADABLE_OUTPUT = { code: 'unreadable_test_output' }
+
+export function readJson(text: string): unknown
+export function readJson<S extends z.ZodType>(text: string, schema: S): z.output<S>
+export function readJson(text: string, schema: z.ZodType = z.unknown()): unknown {
+ return parseJson(text, schema, UNREADABLE_OUTPUT)
+}
+
+function jsonReader(text: () => string): HogRun['json'] {
+ function read(): unknown
+ function read<S extends z.ZodType>(schema: S): z.output<S>
+ function read(schema: z.ZodType = z.unknown()): unknown {
+ return readJson(text(), schema)
+ }
+ return read
}
export async function runHog(argv: string[], options: FakeRuntimeOptions = {}): Promise<HogRun> {
@@ -288,7 +310,7 @@ export async function runHog(argv: string[], options: FakeRuntimeOptions = {}):
elapsedMs: runtime.now() - start,
tls: runtime.tls,
keychain: runtime.keychain,
- json: () => JSON.parse(runtime.stdout()),
- error: () => JSON.parse(runtime.stderr()).error,
+ json: jsonReader(() => runtime.stdout()),
+ error: () => readJson(runtime.stderr(), ErrorOutputSchema).error,
}
}
diff --git a/hog/test/parity/e2e.test.ts b/hog/test/parity/e2e.test.ts
index c572d589ac88..54b9b67af988 100644
--- a/hog/test/parity/e2e.test.ts
+++ b/hog/test/parity/e2e.test.ts
@@ -1,8 +1,6 @@
import { describe, expect, test } from 'bun:test'
-import { readFileSync } from 'node:fs'
-import { join } from 'node:path'
-import { runScenario, withoutRandomIds, type ParityResult } from './run'
+import { readGolden, runScenario, withoutRandomIds } from './run'
import { SCENARIOS } from './scenarios'
// Each golden is what posthog-cli sent to the fake server for the same files and flags
@@ -10,9 +8,7 @@ import { SCENARIOS } from './scenarios'
// exit with the same code.
describe('hog error-tracking sends what posthog-cli sends', () => {
test.each(SCENARIOS.map((scenario) => [scenario.name, scenario] as const))('%s', async (name, scenario) => {
- const golden = JSON.parse(
- readFileSync(join(import.meta.dir, 'golden', 'e2e', `${name}.json`), 'utf8')
- ) as ParityResult
+ const golden = readGolden(name)
const result = await runScenario(scenario, 'hog')
if (scenario.randomIds) {
expect(withoutRandomIds(result)).toEqual(withoutRandomIds(golden))
diff --git a/hog/test/parity/fake-server.ts b/hog/test/parity/fake-server.ts
index d9af70e257f8..50ca1a729d6f 100644
--- a/hog/test/parity/fake-server.ts
+++ b/hog/test/parity/fake-server.ts
@@ -1,4 +1,7 @@
import { createHash } from 'node:crypto'
+import { z } from 'zod'
+
+import { readJson } from '@/testing'
// A fake PostHog API plus object store that records every request. Parity tests run posthog-cli and
// hog against it and compare the recordings. Server-side ids are derived from the request (UUIDv5),
@@ -6,6 +9,29 @@ import { createHash } from 'node:crypto'
export type Scenario = 'fresh' | 'post-only' | 'hash-mismatch' | 'release-mismatch' | 'check-403' | 'start-500-once'
+const ReleaseBody = z.looseObject({
+ hash_id: z.string(),
+ version: z.string(),
+ project: z.string(),
+ metadata: z.unknown().optional(),
+})
+const CheckUploadBody = z.looseObject({
+ symbol_sets: z.array(z.looseObject({ chunk_id: z.string(), content_hash: z.string() })),
+})
+const StartUploadBody = z.looseObject({
+ symbol_sets: z.array(
+ z.looseObject({
+ chunk_id: z.string(),
+ release_id: z.string().nullable(),
+ content_hash: z.string(),
+ content_length: z.number().optional(),
+ })
+ ),
+ force: z.boolean(),
+ skip_on_conflict: z.boolean(),
+})
+const FinishUploadBody = z.looseObject({ content_hashes: z.record(z.string(), z.string()) })
+
export interface RecordedRequest {
method: string
path: string
@@ -113,12 +139,7 @@ export function startFakeServer(scenario: Scenario): FakeServer {
)
}
if (/^\/api\/projects\/\d+\/error_tracking\/releases$/.test(path) && request.method === 'POST') {
- const parsed = JSON.parse(body) as {
- hash_id: string
- version: string
- project: string
- metadata?: unknown
- }
+ const parsed = readJson(body, ReleaseBody)
const release = {
id: uuid5(`release:${parsed.hash_id}`),
hash_id: parsed.hash_id,
@@ -152,7 +173,7 @@ export function startFakeServer(scenario: Scenario): FakeServer {
})
)
}
- const parsed = JSON.parse(body) as { symbol_sets: { chunk_id: string; content_hash: string }[] }
+ const parsed = readJson(body, CheckUploadBody)
const needed = parsed.symbol_sets
.filter((set) => symbolSets.get(set.chunk_id)?.hash !== set.content_hash)
.map((set) => set.chunk_id)
@@ -163,16 +184,7 @@ export function startFakeServer(scenario: Scenario): FakeServer {
if (scenario === 'start-500-once' && startCalls === 1) {
return respond(json(500, { detail: 'Server error.' }))
}
- const parsed = JSON.parse(body) as {
- symbol_sets: {
- chunk_id: string
- release_id: string | null
- content_hash: string
- content_length?: number
- }[]
- force: boolean
- skip_on_conflict: boolean
- }
+ const parsed = readJson(body, StartUploadBody)
// The stored copy has other content: an error unless the request says what to do.
if (scenario === 'hash-mismatch' && !parsed.force) {
if (!parsed.skip_on_conflict) {
@@ -214,7 +226,7 @@ export function startFakeServer(scenario: Scenario): FakeServer {
return respond(json(201, { id_map: idMap }))
}
if (path.endsWith('/symbol_sets/bulk_finish_upload')) {
- const parsed = JSON.parse(body) as { content_hashes: Record<string, string> }
+ const parsed = readJson(body, FinishUploadBody)
for (const [id, hash] of Object.entries(parsed.content_hashes)) {
for (const set of symbolSets.values()) {
if (set.id === id) {
diff --git a/hog/test/parity/normalize.ts b/hog/test/parity/normalize.ts
index db39d40b90ff..bbb815135e9b 100644
--- a/hog/test/parity/normalize.ts
+++ b/hog/test/parity/normalize.ts
@@ -1,3 +1,7 @@
+import { z } from 'zod'
+
+import { readJson } from '@/testing'
+
import type { RecordedRequest } from './fake-server'
// Rewrites only what is random by design, so the rest can be compared byte for byte:
@@ -36,7 +40,7 @@ export function normalize(requests: RecordedRequest[]): NormalizedRequest[] {
}
let body = request.body
if (body) {
- const parsed = JSON.parse(body) as Record<string, unknown>
+ const parsed = readJson(body, z.record(z.string(), z.unknown()))
if ('upload_run_id' in parsed) {
parsed.upload_run_id = '<run>'
}
diff --git a/hog/test/parity/run.ts b/hog/test/parity/run.ts
index 13a5d169f5d2..f5ff6bb9026a 100644
--- a/hog/test/parity/run.ts
+++ b/hog/test/parity/run.ts
@@ -2,20 +2,45 @@ import { createHash } from 'node:crypto'
import { mkdtempSync, readdirSync, readFileSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join, relative } from 'node:path'
+import { z } from 'zod'
import { run } from '@/cli'
import { processRuntime } from '@/core/runtime'
+import { readJson } from '@/testing'
import { startFakeServer } from './fake-server'
import { write } from './fixtures'
-import { normalize, type NormalizedRequest } from './normalize'
+import { normalize } from './normalize'
import type { ParityScenario } from './scenarios'
-export interface ParityResult {
- exitCode: number
- requests: NormalizedRequest[]
+// Strict, so a golden with a field hog doesn't produce fails the comparison instead of losing it.
+const ParityResultSchema = z.strictObject({
+ exitCode: z.number(),
+ requests: z.array(
+ z.strictObject({
+ method: z.string(),
+ path: z.string(),
+ headers: z.record(z.string(), z.string()),
+ body: z.string().optional(),
+ upload: z
+ .strictObject({
+ sha512: z.string(),
+ length: z.number(),
+ fields: z.record(z.string(), z.string()).optional(),
+ filename: z.string().optional(),
+ })
+ .optional(),
+ status: z.number(),
+ })
+ ),
// The files the run created or changed, and null for the ones it deleted.
- files?: Record<string, string | null>
+ files: z.record(z.string(), z.string().nullable()).optional(),
+})
+
+export type ParityResult = z.output<typeof ParityResultSchema>
+
+export function readGolden(name: string): ParityResult {
+ return readJson(readFileSync(join(import.meta.dir, 'golden', 'e2e', `${name}.json`), 'utf8'), ParityResultSchema)
}
// posthog-cli reads the POSTHOG_CLI_ names and hog the HOG_ ones, so both CLIs get the same credentials.
diff --git a/hog/test/parity/scenarios.ts b/hog/test/parity/scenarios.ts
index 26ebd9e8dbb2..48f8f43e537a 100644
--- a/hog/test/parity/scenarios.ts
+++ b/hog/test/parity/scenarios.ts
@@ -1,4 +1,4 @@
-import { readFileSync, rmSync } from 'node:fs'
+import { rmSync } from 'node:fs'
import { join } from 'node:path'
import type { Scenario, StoredObject } from './fake-server'
@@ -6,7 +6,7 @@ import { GIT_REPO, mappingOfSize, readTree, SMALL_MAPPING, write } from './fixtu
import { EXPO_BUILD, HERMES_BUILD, HERMES_BUNDLES } from './hermes-fixtures'
import { bundles, EDGE_CASES, MALFORMED_MAP_DEBUG_ID, PUBLIC_PATH, STYLESHEET_LOOKUP, STYLESHEETS } from './js-fixtures'
import { INFO_PLIST_BINARY, INFO_PLIST_SETTINGS, INFO_PLIST_XML, XCODE_SETTINGS } from './plist-fixtures'
-import { decodeFile, type ParityResult } from './run'
+import { decodeFile, readGolden } from './run'
import { SYMBOL_SETS } from './symbol-set-fixtures'
type Files = Record<string, string | Uint8Array>
@@ -39,9 +39,7 @@ export interface ParityScenario {
function filesFrom(name: string): () => Files {
return () => {
const scenario = SCENARIOS.find((candidate) => candidate.name === name)!
- const golden = JSON.parse(
- readFileSync(join(import.meta.dir, 'golden', 'e2e', `${name}.json`), 'utf8')
- ) as ParityResult
+ const golden = readGolden(name)
const files: Files = { ...scenario.files() }
for (const [path, text] of Object.entries(golden.files ?? {})) {
if (text === null) {