Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

9.0 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import crypto from 'node:crypto'
import { verifyAllowanceToken } from './project-allowance-token'
import { PROJECT_ALLOWANCE_PUBLIC_KEY } from './project-allowance-key'
import { ENTITLEMENT_PUBLIC_KEY } from './entitlement-key'
import { machineHashOf } from './machine-id'

// Interop vector: a THROWAWAY Ed25519 pair, and one token minted by nodeterm-server's REAL signer
// (`createAllowanceSigner` in src/lib/project-allowance-token.ts, commit 31efe09) via `npx tsx -e`.
// Only the public half was kept; the private half was never written down. If the server's wire
// format drifts (key order, signed bytes, padding), this is the test that goes red.
const SERVER_TEST_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAUAfhrO24p425RoZib+zQeTo9vfDbHijmPY5/qKBWS9Q=
-----END PUBLIC KEY-----
`
const SERVER_TOKEN =
  'eyJ0eXAiOiJwcm9qZWN0LWFsbG93YW5jZSIsImRldmljZUlkIjoiM2YwYzZhNTItOWQ3ZS00YjhhLWExYzQtNWUyZjdkOWIwYzE4IiwibWFjaGluZSI6ImRlMTY4MTkyYzlhNWZhNDEwZjgxZmQwMzA4NmFmZDUwNTM1NDgzYWVkNTc3ODIxMDZjMTdkMmE5YjIwZjFmMGYiLCJhbGxvd2FuY2UiOjcsImVwb2NoIjozLCJpYXQiOjE3OTMzNjI4ODl9' +
  '.PHXlrBVGe9Itc4fffJMqZc6vWZk-M3mh34POPm0Y-JgmyQMK_M6EnNSptPklBqtvIQDkwRfz2U8SvAYNSob7BA'
const SERVER_DEVICE = '3f0c6a52-9d7e-4b8a-a1c4-5e2f7d9b0c18'
const SERVER_RAW_MACHINE = 'C0FFEE00-1234-4ABC-9DEF-0123456789AB'

// Locally signed tokens use their own throwaway pair, generated per run.
const { publicKey, privateKey } = crypto.generateKeyPairSync('ed25519')
const PUB = publicKey.export({ type: 'spki', format: 'pem' }).toString()

const DEVICE = 'device-1'
const MACHINE = machineHashOf('0123456789abcdef0123456789abcdef')
const EXPECT = { deviceId: DEVICE, machine: MACHINE }

/** Sign like the server: base64url(text), then a signature over that base64url TEXT (not the JSON). */
function signText(text: string, key: crypto.KeyObject = privateKey): string {
  const p = Buffer.from(text).toString('base64url')
  return `${p}.${crypto.sign(null, Buffer.from(p), key).toString('base64url')}`
}

const signRaw = (payload: unknown, key?: crypto.KeyObject): string => signText(JSON.stringify(payload), key)

function mint(over: Record<string, unknown> = {}, key: crypto.KeyObject = privateKey): string {
  return signRaw(
    {
      typ: 'project-allowance',
      deviceId: DEVICE,
      machine: MACHINE,
      allowance: 12,
      epoch: 2,
      iat: Math.floor(Date.now() / 1000),
      ...over
    },
    key
  )
}

describe('verifyAllowanceToken', () => {
  it('verifies a token minted by the server signer', () => {
    expect(
      verifyAllowanceToken(
        SERVER_TOKEN,
        { deviceId: SERVER_DEVICE, machine: machineHashOf(SERVER_RAW_MACHINE), epoch: 3 },
        SERVER_TEST_PUBLIC_KEY
      )
    ).toEqual({ allowance: 7, epoch: 3 })
  })

  it('accepts a valid token and returns only { allowance, epoch }', () => {
    expect(verifyAllowanceToken(mint(), EXPECT, PUB)).toEqual({ allowance: 12, epoch: 2 })
  })

  it('checks the epoch only when one is expected', () => {
    expect(verifyAllowanceToken(mint(), { ...EXPECT, epoch: 2 }, PUB)).toEqual({ allowance: 12, epoch: 2 })
    expect(verifyAllowanceToken(mint(), { ...EXPECT, epoch: 3 }, PUB)).toBeNull()
  })

  it('accepts allowance 0 (a non-negative integer)', () => {
    expect(verifyAllowanceToken(mint({ allowance: 0 }), EXPECT, PUB)).toEqual({ allowance: 0, epoch: 2 })
  })

  it('refuses a token signed by another key', () => {
    expect(verifyAllowanceToken(mint(), EXPECT, SERVER_TEST_PUBLIC_KEY)).toBeNull()
    const other = crypto.generateKeyPairSync('ed25519').privateKey
    expect(verifyAllowanceToken(mint({}, other), EXPECT, PUB)).toBeNull()
  })

  it.each([
    ['wrong typ', { typ: 'entitlement' }],
    ['no typ', { typ: undefined }],
    ['wrong deviceId', { deviceId: 'device-2' }],
    ['wrong machine', { machine: machineHashOf('another-machine') }],
    ['allowance -1', { allowance: -1 }],
    ['allowance 2.5', { allowance: 2.5 }],
    ['allowance as a string', { allowance: '12' }],
    ['no allowance', { allowance: undefined }],
    ['epoch not an integer', { epoch: 1.5 }],
    ['no epoch', { epoch: undefined }]
  ])('refuses a signed token with %s', (_label, over) => {
    expect(verifyAllowanceToken(mint(over), EXPECT, PUB)).toBeNull()
  })

  // `machine: null` = no OS-read hash known yet (core/project-limit.ts): the token is trusted
  // provisionally on everything but the machine. It must be an explicit null, never a missing key.
  it('skips only the machine match when machine is null', () => {
    const elsewhere = mint({ machine: machineHashOf('another-machine') })
    expect(verifyAllowanceToken(elsewhere, { deviceId: DEVICE, machine: null }, PUB)).toEqual({ allowance: 12, epoch: 2 })
    expect(verifyAllowanceToken(elsewhere, EXPECT, PUB)).toBeNull()
  })

  it.each([
    ['wrong typ', { typ: 'entitlement' }],
    ['wrong deviceId', { deviceId: 'device-2' }],
    ['allowance -1', { allowance: -1 }],
    ['no epoch', { epoch: undefined }]
  ])('with machine null, still refuses a signed token with %s', (_label, over) => {
    expect(verifyAllowanceToken(mint(over), { deviceId: DEVICE, machine: null }, PUB)).toBeNull()
  })

  it('with machine null, still checks the signature and the expected epoch', () => {
    const other = crypto.generateKeyPairSync('ed25519').privateKey
    expect(verifyAllowanceToken(mint({}, other), { deviceId: DEVICE, machine: null }, PUB)).toBeNull()
    expect(verifyAllowanceToken(mint(), { deviceId: DEVICE, machine: null, epoch: 3 }, PUB)).toBeNull()
  })

  it('refuses an entitlement-shaped token signed with the same key', () => {
    // Everything an allowance needs, minus the kind gate: proves `typ` is checked, not inferred.
    const entitlement = signRaw({
      deviceId: DEVICE,
      tier: 'pro',
      licenseId: 'lic_test',
      machine: MACHINE,
      allowance: 12,
      epoch: 2,
      iat: Math.floor(Date.now() / 1000),
      exp: Math.floor(Date.now() / 1000) + 3600
    })
    expect(verifyAllowanceToken(entitlement, EXPECT, PUB)).toBeNull()
  })

  it('refuses a tampered payload', () => {
    const [p, s] = mint().split('.')
    const forged = Buffer.from(
      JSON.stringify({ ...JSON.parse(Buffer.from(p, 'base64url').toString('utf-8')), allowance: 50 })
    ).toString('base64url')
    expect(verifyAllowanceToken(`${forged}.${s}`, EXPECT, PUB)).toBeNull()
  })

  it.each([
    ['undefined', undefined],
    ['empty', ''],
    ['no dot', 'abc'],
    ['empty payload segment', '.abc'],
    ['empty signature', `${mint().split('.')[0]}.`],
    ['a signed non-object payload', signRaw(42)],
    ['a signed null payload', signRaw(null)],
    ['a signed non-JSON payload', signText('not json')],
    // A hand-edited or corrupted token file can hand over any JSON value, whatever the type says.
    ['a number', 123 as unknown as string],
    ['an object', {} as unknown as string],
    ['null', null as unknown as string]
  ])('returns null (never throws) for %s', (_label, token) => {
    expect(verifyAllowanceToken(token, EXPECT, PUB)).toBeNull()
  })

  it('returns null for an unusable public key', () => {
    expect(verifyAllowanceToken(mint(), EXPECT, '')).toBeNull()
    expect(verifyAllowanceToken(mint(), EXPECT, 'not a pem')).toBeNull()
    const rsa = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 }).publicKey
    expect(verifyAllowanceToken(mint(), EXPECT, rsa.export({ type: 'spki', format: 'pem' }).toString())).toBeNull()
  })

  it('with the default key, returns null and never throws', () => {
    // Until O1 the shipped key is empty, so nothing verifies; after O1 a token from THIS test pair
    // must still be refused by the production key.
    expect(() => verifyAllowanceToken(mint(), EXPECT)).not.toThrow()
    expect(verifyAllowanceToken(mint(), EXPECT)).toBeNull()
  })
})

describe('PROJECT_ALLOWANCE_PUBLIC_KEY', () => {
  // Owner step O1 pastes the production key. Until then it is '' — and the FLIP release is refused
  // while it is (scripts/check-project-limit-deadline.mjs), so an empty key can be merged but never
  // flipped on. Anything else in that constant must be the real thing.
  it("is '' before owner step O1, else an Ed25519 public-key PEM distinct from ENTITLEMENT_PUBLIC_KEY", () => {
    if (PROJECT_ALLOWANCE_PUBLIC_KEY !== '') {
      // A pasted PRIVATE key also parses (createPublicKey derives the public half) — refuse it by text.
      expect(PROJECT_ALLOWANCE_PUBLIC_KEY).toMatch(/-----BEGIN PUBLIC KEY-----/)
      expect(PROJECT_ALLOWANCE_PUBLIC_KEY).not.toMatch(/PRIVATE KEY/)
      const key = crypto.createPublicKey(PROJECT_ALLOWANCE_PUBLIC_KEY)
      expect(key.type).toBe('public')
      expect(key.asymmetricKeyType).toBe('ed25519')
      const entitlement = crypto.createPublicKey(ENTITLEMENT_PUBLIC_KEY)
      expect(
        key.export({ type: 'spki', format: 'der' }).equals(entitlement.export({ type: 'spki', format: 'der' }))
      ).toBe(false)
    }
  })

  it('an empty key verifies nothing, not even a valid token (the pre-O1 state)', () => {
    expect(verifyAllowanceToken(mint(), EXPECT, PUB)).not.toBeNull() // control: the token is valid
    expect(verifyAllowanceToken(mint(), EXPECT, '')).toBeNull()
  })
})