Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

2.7 KB
Raw Blame
import { chmodSync, mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { hookCredentialPath, loadOrCreateHookCredential } from './hookAuth.js'
import { localCredentialHeaders, localCredentialMatches, readLocalCredential, suppliedLocalCredential } from './localCredential.js'

const dirs: string[] = []
afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })))
function dataDir(): string {
  const dir = mkdtempSync(join(tmpdir(), 'local-cred-'))
  dirs.push(dir)
  return dir
}

describe('the daemon\'s local credential', () => {
  it('is the hook credential, read by a process that can read it, and put on no request on Windows', () => {
    const dir = dataDir()
    const credential = loadOrCreateHookCredential(dir)
    expect(readLocalCredential(dir)).toBe(credential)
    // Windows clients use the daemon's pipe (audit W1). On macOS and Linux see team/localCredential.spec.ts.
    expect(localCredentialHeaders(dir, 'win32')).toEqual({})
  })

  it('adds no header when there is no credential to read, or one another user could have written', () => {
    const dir = dataDir()
    expect(localCredentialHeaders(dir, 'win32')).toEqual({})
    loadOrCreateHookCredential(dir)
    chmodSync(hookCredentialPath(dir), 0o644)
    expect(readLocalCredential(dir)).toBeNull()
    expect(localCredentialHeaders(dir)).toEqual({})
  })

  it('is read from the named header first, else from a bearer', () => {
    expect(suppliedLocalCredential({ headers: { 'x-harness-local': 'named', authorization: 'Bearer other' } })).toBe('named')
    expect(suppliedLocalCredential({ headers: { authorization: 'Bearer bearer' } })).toBe('bearer')
    expect(suppliedLocalCredential({ headers: { 'x-harness-local': '', authorization: 'Bearer bearer' } })).toBe('bearer')
    expect(suppliedLocalCredential({ headers: { authorization: 'Basic abc' } })).toBeUndefined()
    expect(suppliedLocalCredential({ headers: {} })).toBeUndefined()
  })

  it('matches only the exact credential', () => {
    const credential = loadOrCreateHookCredential(dataDir())
    expect(localCredentialMatches(credential, { headers: { 'x-harness-local': credential } })).toBe(true)
    expect(localCredentialMatches(credential, { headers: { authorization: `Bearer ${credential}` } })).toBe(true)
    const wrong = `${credential.slice(0, -1)}${credential.endsWith('x') ? 'y' : 'x'}`
    expect(localCredentialMatches(credential, { headers: { 'x-harness-local': wrong } })).toBe(false)
    expect(localCredentialMatches(credential, { headers: { 'x-harness-local': credential.slice(1) } })).toBe(false)
    expect(localCredentialMatches(credential, { headers: {} })).toBe(false)
  })
})