Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions runtime/flybot-image/model-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DEFAULT_FILES = [
'/data/secrets/model-api.json',
]
export const ENV_FILE = 'FLYBOT_WORKSPACE_MODEL_API_FILE'
const ALLOWED_PREFIXES = ['/run/secrets/', '/data/secrets/']
export const ALLOWED_PREFIXES = ['/run/secrets/', '/data/secrets/']
const MAX_BYTES = 16 * 1024
const PROVIDER = /^[a-z0-9][a-z0-9-]{0,63}$/
const MODEL = /^[A-Za-z0-9._:/-]{1,128}$/
Expand Down Expand Up @@ -123,10 +123,10 @@ export function readModelApiFile(path) {
return contentsToModelApi(readFileSync(assertAllowedPath(path), 'utf8'))
}

export function resolveModelApiPath(env = process.env) {
export function resolveModelApiPath(env = process.env, defaults = DEFAULT_FILES) {
const configured = env[ENV_FILE]
if (configured && configured.trim() !== '') return configured.trim()
for (const candidate of DEFAULT_FILES) {
for (const candidate of defaults) {
try {
statSync(candidate)
return candidate
Expand All @@ -135,8 +135,8 @@ export function resolveModelApiPath(env = process.env) {
return undefined
}

export function loadModelApi(env = process.env) {
const path = resolveModelApiPath(env)
export function loadModelApi(env = process.env, defaults = DEFAULT_FILES) {
const path = resolveModelApiPath(env, defaults)
if (path === undefined) return undefined
return readModelApiFile(path)
}
Expand Down
13 changes: 10 additions & 3 deletions runtime/flybot-image/test/model-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, chmodSync, rmSync }
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { test } from 'node:test'
import { applyModelApi, contentsToModelApi, keyRefFor, loadModelApi, parseModelApi, readModelApiFile, redact, resolveModelApiPath, ModelApiError } from '../model-api.js'
import { ALLOWED_PREFIXES, applyModelApi, contentsToModelApi, DEFAULT_FILES, keyRefFor, loadModelApi, parseModelApi, readModelApiFile, redact, resolveModelApiPath, ModelApiError } from '../model-api.js'

const sample = {
provider: 'deepseek',
Expand Down Expand Up @@ -77,8 +77,15 @@ test('reads only allowlisted secret paths and skips a missing file', () => {
const sneaky = join(secrets, 'link.json')
symlinkSync(outside, sneaky)
assert.throws(() => readModelApiFile(sneaky), ModelApiError)
assert.equal(resolveModelApiPath({}), undefined)
assert.equal(loadModelApi({}), undefined)
// The default file list is host state, not recipe state: a provisioned host
// really does have /run/secrets/flybot-workspace-model-api.json. Pass an
// explicit empty list so this test stays hermetic, then check the defaults
// themselves are allowlisted.
assert.equal(resolveModelApiPath({}, []), undefined)
assert.equal(loadModelApi({}, []), undefined)
for (const path of DEFAULT_FILES) {
assert.ok(ALLOWED_PREFIXES.some((prefix) => path.startsWith(prefix)), `${path} must live under an allowlisted secret directory`)
}
rmSync(root, { recursive: true, force: true })
})

Expand Down
Loading