diff --git a/agents/pi/admitted.json b/agents/pi/admitted.json index 528f891..920eb9e 100644 --- a/agents/pi/admitted.json +++ b/agents/pi/admitted.json @@ -29,7 +29,7 @@ { "kind": "tree", "path": "lib", - "sha256": "fbe13e2031ba2dda14a42634ec1d1cb710bfe5a6f87c3d105e9f71fe2e041e64" + "sha256": "560063b996a32ad9cf90de165cfe2eb248ca138b30e73f359fb9d6e8d997a5d3" }, { "kind": "skill", diff --git a/docs/README.md b/docs/README.md index 60e64c1..fd66289 100644 --- a/docs/README.md +++ b/docs/README.md @@ -13,6 +13,7 @@ live service. | Prepare and qualify a distribution | [Release procedure](release.md) | | Exercise the source-only gift wrapper | [Development gift trial](gift-trial.md), [schema](coupon-first-use.schema.json) and [template](coupon-first-use.example.json) | | Integrate coupon redemption | [Coupon protocol](protocols/coupon.md) and [NEAR adapter](protocols/near-adapter.md) | +| Qualify native NEAR tool encryption | [Experimental qualification](protocols/near-native-qualification.md) | | Inspect trace encryption and recovery | [Trace format](protocols/trace-store.md) | | Integrate delegated tasks and verification | [Task protocol](protocols/task-v1.md), [runner interface](protocols/runner-task.md) and [receipts](protocols/receipt-v1.md) | | Inspect the CDP browser interface | [Browser grants](protocols/browser-grant.md) | diff --git a/docs/protocols/near-adapter.md b/docs/protocols/near-adapter.md index 3bf195c..57acdf8 100644 --- a/docs/protocols/near-adapter.md +++ b/docs/protocols/near-adapter.md @@ -200,3 +200,10 @@ re-enable those automatic requests while the coupon socket is active. A failed request may already have been charged; a later explicit user request is a new request, not proof that repeating the earlier one is safe. Tool continuations requested by the model remain part of normal work and may make further requests. + + +## Native-tool qualification + +The normal provider still carries local tools in encrypted text. The experimental +[native-tool qualification](near-native-qualification.md) records live field and +model-signature checks without enabling that route in user configuration. diff --git a/docs/protocols/near-native-qualification.md b/docs/protocols/near-native-qualification.md new file mode 100644 index 0000000..fa789cb --- /dev/null +++ b/docs/protocols/near-native-qualification.md @@ -0,0 +1,222 @@ +# NEAR native encrypted-tool qualification + +This is an experimental qualification tool, separate from the normal NEAR and +coupon providers. It does not enable native tools in a user's configuration. +Tracking: [issue 73](https://github.com/amiller/abalone/issues/73), +[research issue 23](https://github.com/amiller/awesome-private-inference/issues/23), +[request-origin sprint 77](https://github.com/amiller/abalone/issues/77), +and the [DevProof supplement](https://github.com/amiller/devproof-audits-guide/pull/5). + +## Evidence from 14 October 2026 + +The configured model, `z-ai/glm-5.3-flash`, accepted encrypted native function +schemas, names, arguments and tool-result history. Two consecutive native tool +calls completed with the expected synthetic arguments. A separate complete +call/result/final-answer round trip passed. Every accepted reply had a genuine attested **model** signature (`provider_tee`) +whose declared request hash matched the client's wire request and whose response +hash matched the received encrypted bytes. A gateway signature is insufficient. +The request-hash authority dependency below limits what this proves about input origin. + +Six synthetic chat POSTs were made in total. Catalog prices applied to signed +reported token usage give an estimated total of **$0.00022808**, within the +user-authorized $0.25 cap. This is a usage-priced estimate, not an invoice or a +provider account-balance observation. No provider credentials, private files or +external search queries were given to the model. The only executable tool was +a local echo of an exact expected synthetic value. + +Strict admission requiring `UpToDate` for both gateway and model failed. The +model TCB was `UpToDate`; the gateway was `OutOfDate`, with INTEL-SA-01192, +INTEL-SA-01245, INTEL-SA-01312 and INTEL-SA-01313. The protocol-only test used the +already-configured policy that reports TCB status, while retaining quote, +fresh nonce, key binding, GPU signature, measured compose, Base registry and +pinned gateway TLS checks. This does not qualify the stricter profile. + +Observed compose hashes: + +- Gateway: `41e18f38978b031431dfc8a4fcf6d8c38da62b1b093fd1d0e39e26ee6fad030c`. +- Model: `c82b1a2eaf6996154a5f39ae621643f034b082d5e51edd3d2ba6009273881d86`. + +These identify the observed workloads. They do not close the inner serving +code/model supply chain to the reviewed source. The existing verifier reads +NEAR's compose registry through a pinned Base RPC; it is not an independent +light-client finality proof. + +A later read-only capture verified the compose-manager quote with Intel PCS +collateral: its TCB was `UpToDate`, its report data bound the action digest and +our fresh nonce, and its MRTD and RTMR0–3 matched the model CVM. Its 186 recorded +actions were authenticated under the canonical action encoding described below. +The last recorded compose file also matched the bytes fetched at its reported +commit. The fresh report contains different model outer-compose bytes from +the earlier native-tool observations; those earlier interoperability results +are not silently carried forward to this capture. This authenticates reported +action history, not the current running state. +The [manager source](https://github.com/nearai/compose-manager/blob/aa9de34419c753c432259ecbcc717b6e2a7357c6/src/main.rs#L300) +explicitly notes that `compose_up` can be recorded before success; an attempted +or historical deployment cannot substitute for evidence of active containers. + +Current [manager source](https://github.com/nearai/compose-manager/blob/aa9de34419c753c432259ecbcc717b6e2a7357c6/src/main.rs#L357) +defines sorted-key, compact UTF-8 JSON with string-valued action fields and +string-array services, omitting absent optional fields and empty services. +That canonical contract allows a digest to survive reformatting. The earlier +claim that reformatted records necessarily prevent digest verification was too +broad. Original bytes remain useful for legacy exact-byte variants and review; +the collector retains them and requires an explicit encoding rather than trying +alternate hashes until one passes. The inspected public source is not yet bound +to the deployed manager image. + +The verified file was +[`prod/GLM-5.3-Flash-SGL-TP2x4-W4AFP8-V8KdaCanary.yaml`](https://github.com/nearai/cvm-compose-files/blob/46bfd83ec22c179693e06308e40950d540154b4d/prod/GLM-5.3-Flash-SGL-TP2x4-W4AFP8-V8KdaCanary.yaml), +SHA-256 `c7c1bf4d82e137b8d2e0788f41cd6637b38bd08fdc74f38e87b4571ce0c8562e`. +It configures the inference proxy's `TOKEN` from `PROXY_TOKEN` and gives the +registrar that same inference token. These placeholders establish a source-level +configuration path; they do not establish who can read the deployed credential. + +Source-to-deployment closure, gateway request-hash authority/credential custody, +independent review and normal-provider integration remain outstanding. + +## Field and response contract + +The codec follows the ECDSA all-fields implementation at +[inference-proxy efd600a4](https://github.com/nearai/inference-proxy/blob/efd600a46c4327c8d8d038ab8b01c25a0e74b5bd/src/encryption.rs). +Both `X-Encryption-Version: 2` and `X-Encrypt-All-Fields: true` are explicit. +Version alone does not enable tool-field encryption. Extended server decryption +accepts plaintext for compatibility, so client-side wire canaries matter. + +| Surface | Qualification behavior | +| --- | --- | +| Text content, reasoning, refusal and message names | Encrypt before sending; require ciphertext before accepting replies | +| Function definitions | Encrypt name, description and serialized parameters | +| Named function selection | Encrypt the selected name | +| Native assistant history | Encrypt function names and argument strings | +| Tool-result history | Encrypt result text and name; keep the opaque call ID as metadata | +| Streamed tool fragments | Authenticate the whole response, decrypt every field, then assemble calls | +| IDs, roles, type labels, model name and usage | Visible metadata | +| Legacy functions, images/audio, structured-output schemas, logprobs, server-side search and unknown fields | Refuse; not qualified by this codec | + +A successful ciphertext decryption is not sender authentication: anyone with +the public client key can encrypt a fabricated command. The codec therefore +holds the full response until the attested model's signature verifies over the +declared request and response hashes. It refuses wrong signers, altered requests, spliced +responses, missing completion, malformed fields, invalid argument JSON and +unsupported shapes. Returned results are immutable and internally marked; +only those results can be converted into Pi's native tool stream. + +The upstream proxy excludes the exact `: keep-alive` transport-padding frame +from its signed bytes. The codec handles that defined exception without +ignoring arbitrary comments or changing signed JSON. Numeric SGLang stop-token +metadata is accepted; plaintext stop strings are refused. + +Pi expects the complete function name in its first tool delta. The codec +consolidates authenticated name/argument fragments before returning a native +OpenAI-compatible stream. Its Pi compatibility settings disable `store`, select +`max_tokens`, and use the model's chat-template thinking parameter. GLM still +emits an inline reasoning prefix ending in `` with thinking disabled; +the synthetic final-answer check strips that known prefix. This is a rendering +behavior, separate from encryption or signature verification. + +## Request-integrity trust dependency + +The reviewed [chat dispatcher](https://github.com/nearai/inference-proxy/blob/efd600a46c4327c8d8d038ab8b01c25a0e74b5bd/src/routes/chat.rs) +lets a caller authenticated with the trusted service token (`config.token`) +supply `X-Request-Hash`, including a hash different from the backend's received +body. The [authentication extractor](https://github.com/nearai/inference-proxy/blob/efd600a46c4327c8d8d038ab8b01c25a0e74b5bd/src/auth.rs#L354) +compares configured service tokens first; a match sets `cloud_api_key` to +`None`, while ordinary validated cloud keys set it to `Some`. Multiple tokens +can be configured for rotation. End-user cloud API keys cannot use that override. This supports gateways +that re-serialize requests, but places that gateway authority in the integrity +trust boundary. + +Consequently, a model signature matching the client's request hash does not by +itself prove which body the model received: a privileged gateway could supply +that hash while substituting another encrypted request. Anyone can encrypt new +fields for the public model key; reply authentication alone does not authenticate +the client as the request's author. The regression suite demonstrates this +signature-level limitation using synthetic model signatures. It does not use a +real service credential or demonstrate an attack on the deployed service. + +Gateway-independent tool execution needs request binding enforced inside the +model enclave without a gateway-asserted hash, or a verified client-authorized +request signature. Alternatively the gateway, its code and privileged service +credential custody must be explicitly included and qualified in the trust model. +The public [cloud-api forwarding source](https://github.com/nearai/cloud-api/blob/752e2976a7ee2a8790163974ba4a70dd218bbf3b/crates/inference_providers/src/attested/nearai/mod.rs#L1941) +sets `X-Request-Hash` from the gateway's request-hash argument while rewriting +stream options and sending a configured upstream bearer credential. +Its [body-hash middleware](https://github.com/nearai/cloud-api/blob/752e2976a7ee2a8790163974ba4a70dd218bbf3b/crates/api/src/middleware/body_hash.rs) +computes SHA-256 over the received request bytes. This explains the intended +honest-gateway path; a gateway compromise is still inside the integrity TCB. +The gateway pool receives its bearer key from +[`config.inference_api_key`](https://github.com/nearai/cloud-api/blob/752e2976a7ee2a8790163974ba4a70dd218bbf3b/crates/api/src/lib.rs#L849), +loaded from [`INFERENCE_API_KEY`](https://github.com/nearai/cloud-api/blob/752e2976a7ee2a8790163974ba4a70dd218bbf3b/crates/config/src/types.rs#L63). +This is a source configuration path, not proof that its deployed value equals +`PROXY_TOKEN`. The registrar has the inference token in its environment, but the +inspected registration requests do not send it in their request bodies. +The gateway image-to-source mapping and deployed token custody remain open. +The current observations establish neither integrity alternative. Keep +private-work migration gated on this question as well as source/deployment review. + +## Reproducing the checks + +After the repository's normal dependency/build setup, offline checks are: + +```sh +node --test tests/near-native-qualification.test.mjs +``` + +The fifteen tests cover real ECDSA encryption/signatures, wire canaries, +unsupported inputs, corrupt/plaintext fields, stream completeness, signer +substitution, authenticated-result immutability, and actual Pi tool calls with +encrypted result history. They make no provider calls. Local SDK evidence used +the existing compiled Pi build; it is not fresh installed-release evidence. + +Live qualification requires a separately authorized budget and synthetic data: + +```sh +node tools/qualify-near-native.mjs --model z-ai/glm-5.3-flash \ + --budget-usd 0.25 --out local-data/native-check/run-1 +``` + +Use `NEARAI_API_KEY` in the environment. `--configured-key` reads the existing +local node keystore without copying the credential to logs. Strict TCB admission +is the default. `--protocol-only` explicitly selects reported TCB for synthetic +interoperability evidence; it cannot claim private-work eligibility. + +By default there are two echo calls and one final completion; `--rounds 2` uses +one echo and one completion. At most six POSTs are allowed across runs sharing +an output parent. Before each POST, a locked ledger reserves the catalog cost +of an entire context window plus the requested output cap. A failed/unknown +charge keeps its reservation; only authenticated token usage releases it. +Requests are never automatically retried. A crash leaves the ledger lock for +reconciliation. Do not reset or move the ledger to bypass its authorized cap. + +Parsed attestation records, encrypted wires, signatures, decoded synthetic results and +pricing evidence remain under the ignored output directory with owner-only +permissions. Review them before any publication. Re-run against a fresh source +revision/model deployment before claiming broader or future qualification. + +## Read-only deployment evidence + +These tools make no chat POSTs and do not use or reset the paid-check ledger: + +```sh +python3 tools/near-deployment-evidence.py --model z-ai/glm-5.3-flash \ + --configured-key --out local-data/deployment/run-1 +node tools/verify-near-manager-quote.mjs --evidence local-data/deployment/run-1 +python3 tests/near-deployment-evidence.test.py +``` + +The collector uses ordinary HTTPS for the initial report fetch. Evidence becomes +authenticated only through nonce-bound quote verification; this check alone does +not establish the full model signing-key/TLS/compose admission profile. The Node +check uses the repository's installed `@phala/dcap-qvl` dependency and Intel PCS. +It reports TCB status rather than granting private-work admission. It requires +matching manager/model measurements and recomputes the action digest from the +captured evidence. A separate negative check altered a byte in the captured manager quote and +confirmed refusal without writing a success verdict. Seven offline tests cover digest corruption, nonce swaps, +report-data mismatch, duplicate keys, unsupported action schemas and exact-byte +versus canonical encoding. They do not replace cryptographic quote tests. + +To inspect a historical exact-byte manager contract, explicitly supply +`--action-encoding exact` to both tools. Do not use exact-mode success or a +canonical-mode success to infer the deployed manager source revision. Active +container/image evidence, model artifact integrity and privileged credential +custody are still separate checks. diff --git a/lib/near-native-qualification.mjs b/lib/near-native-qualification.mjs new file mode 100644 index 0000000..e1380e6 --- /dev/null +++ b/lib/near-native-qualification.mjs @@ -0,0 +1,195 @@ +// Experimental qualification codec. Normal providers do not import this module. +// Sensitive fields follow inference-proxy efd600a4's all-fields ECDSA path. +import { encrypt, decrypt, chatSignatureFails } from './near.mjs'; + +export const NEAR_NATIVE_PI_COMPAT = Object.freeze({ supportsStore: false, supportsDeveloperRole: false, maxTokensField: 'max_tokens', + thinkingFormat: 'chat-template', chatTemplateKwargs: Object.freeze({ enable_thinking: Object.freeze({ $var: 'thinking.enabled' }) }) }); + +const authenticatedResults = new WeakSet(); +const deepFreeze = value => { if (value && typeof value === 'object') { Object.values(value).forEach(deepFreeze); Object.freeze(value); } return value; }; + +const refuse = (code) => { throw Object.assign(new Error(`NEAR native qualification: ${code}`), { code }); }; +const record = (v) => v && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype; +const shape = (v, required, optional = []) => { + if (!record(v) || required.some(k => !Object.hasOwn(v, k)) || Object.keys(v).some(k => ![...required, ...optional].includes(k))) refuse('unsupported_shape'); +}; +const text = (v) => { if (typeof v !== 'string') refuse('unsupported_text'); return v; }; +const publicId = (v) => { if (typeof v !== 'string' || !/^[A-Za-z0-9_.:-]{1,200}$/.test(v)) refuse('invalid_metadata_id'); }; +const number = (v, min, max) => { if (!Number.isFinite(v) || v < min || v > max) refuse('invalid_parameter'); }; +const fields = ['content', 'reasoning_content', 'reasoning', 'refusal', 'name']; + +export function encryptNearNativeRequest(input, modelPublicKey) { + const body = structuredClone(input); + shape(body, ['model', 'messages', 'stream', 'max_tokens'], ['tools', 'tool_choice', 'parallel_tool_calls', 'stream_options', 'chat_template_kwargs', 'temperature', 'top_p']); + publicId(body.model.replaceAll('/', ':')); + if (body.stream !== true || !Number.isSafeInteger(body.max_tokens) || body.max_tokens < 1 || body.max_tokens > 512 || !Array.isArray(body.messages) || !body.messages.length || body.messages.length > 64) refuse('invalid_request'); + if (Object.hasOwn(body, 'temperature')) number(body.temperature, 0, 2); + if (Object.hasOwn(body, 'top_p')) number(body.top_p, 0, 1); + if (Object.hasOwn(body, 'parallel_tool_calls') && typeof body.parallel_tool_calls !== 'boolean') refuse('invalid_parameter'); + if (body.stream_options) { shape(body.stream_options, ['include_usage']); if (body.stream_options.include_usage !== true) refuse('invalid_parameter'); } + if (body.chat_template_kwargs) { shape(body.chat_template_kwargs, ['enable_thinking']); if (typeof body.chat_template_kwargs.enable_thinking !== 'boolean') refuse('invalid_parameter'); } + const seal = v => v === '' ? '' : encrypt(text(v), modelPublicKey); + for (const message of body.messages) { + shape(message, ['role'], [...fields, 'tool_calls', 'tool_call_id']); + if (!['system', 'developer', 'user', 'assistant', 'tool'].includes(message.role)) refuse('unsupported_role'); + if (message.content == null && message.role !== 'assistant') refuse('missing_content'); + if (message.role === 'tool' && !Object.hasOwn(message, 'tool_call_id')) refuse('missing_tool_id'); + if (Object.hasOwn(message, 'tool_call_id')) publicId(message.tool_call_id); + for (const field of fields) if (message[field] != null) message[field] = seal(message[field]); + if (message.tool_calls != null) { + if (message.role !== 'assistant' || !Array.isArray(message.tool_calls) || !message.tool_calls.length || message.tool_calls.length > 8) refuse('invalid_tool_history'); + for (const call of message.tool_calls) { + shape(call, ['id', 'type', 'function']); publicId(call.id); + if (call.type !== 'function') refuse('unsupported_tool_type'); + shape(call.function, ['name', 'arguments']); + call.function.name = seal(call.function.name); call.function.arguments = seal(call.function.arguments); + } + } + } + if (body.tools != null) { + if (!Array.isArray(body.tools) || body.tools.length > 8) refuse('invalid_tools'); + for (const tool of body.tools) { + shape(tool, ['type', 'function']); + if (tool.type !== 'function') refuse('unsupported_tool_type'); + shape(tool.function, ['name', 'parameters'], ['description', 'strict']); + if (!record(tool.function.parameters)) refuse('invalid_schema'); + if (Object.hasOwn(tool.function, 'strict') && typeof tool.function.strict !== 'boolean') refuse('invalid_parameter'); + tool.function.name = seal(tool.function.name); + if (tool.function.description != null) tool.function.description = seal(tool.function.description); + tool.function.parameters = seal(JSON.stringify(tool.function.parameters)); + } + } + if (body.tool_choice != null && !['none', 'auto', 'required'].includes(body.tool_choice)) { + shape(body.tool_choice, ['type', 'function']); + if (body.tool_choice.type !== 'function') refuse('unsupported_tool_type'); + shape(body.tool_choice.function, ['name']); body.tool_choice.function.name = seal(body.tool_choice.function.name); + } + return body; +} + +export const nearNativeHeaders = (clientPublicKey, modelPublicKey) => ({ + 'X-Signing-Algo': 'ecdsa', 'X-Client-Pub-Key': clientPublicKey.replace(/^04(?=.{128}$)/, ''), + 'X-Model-Pub-Key': modelPublicKey, 'X-Encryption-Version': '2', 'X-Encrypt-All-Fields': 'true', +}); + +// Public metadata is deliberately separate: model/call IDs, roles, types, +// token counts and timing remain visible. This is not arbitrary-JSON encryption. +const counts = (value, allowed) => { + if (value == null) return; + shape(value, [], allowed); + if (Object.values(value).some(n => !Number.isSafeInteger(n) || n < 0)) refuse('invalid_usage_metadata'); +}; +const usageMetadata = usage => { + if (usage == null) return; + shape(usage, [], ['prompt_tokens', 'completion_tokens', 'total_tokens', 'reasoning_tokens', 'prompt_tokens_details', 'completion_tokens_details']); + counts(Object.fromEntries(Object.entries(usage).filter(([key]) => !key.endsWith('_details'))), ['prompt_tokens', 'completion_tokens', 'total_tokens', 'reasoning_tokens']); + counts(usage.prompt_tokens_details, ['cached_tokens', 'audio_tokens']); + counts(usage.completion_tokens_details, ['reasoning_tokens', 'audio_tokens', 'accepted_prediction_tokens', 'rejected_prediction_tokens']); +}; + +export function parseNearNativeSse(bytes, model) { + let raw; + try { raw = new TextDecoder('utf-8', { fatal: true }).decode(bytes); } catch { refuse('invalid_utf8'); } + if (!raw.endsWith('\n\n') && !raw.endsWith('\r\n\r\n')) refuse('truncated_stream'); + const chunks = []; let done = false, id = null, finish = null; + for (const event of raw.split(/\r?\n\r?\n/)) { + if (!event.trim()) continue; + // The upstream proxy adds this exact transport padding without hashing it. + // No executable or sensitive field can be placed in a skipped comment. + if (event === ': keep-alive') continue; + const lines = event.split(/\r?\n/).filter(line => line !== ''); + if (lines.some(line => !line.startsWith('data:'))) refuse('unsupported_sse_event'); + const value = lines.map(line => line.slice(5).trimStart()).join('\n').trim(); + if (done) refuse('data_after_done'); + if (value === '[DONE]') { done = true; continue; } + let chunk; try { chunk = JSON.parse(value); } catch { refuse('invalid_json'); } + shape(chunk, ['choices'], ['id', 'object', 'created', 'model', 'usage', 'system_fingerprint']); + if ((chunk.object != null && chunk.object !== 'chat.completion.chunk') || (chunk.created != null && (!Number.isSafeInteger(chunk.created) || chunk.created < 0))) refuse('invalid_public_metadata'); + if (chunk.system_fingerprint != null) publicId(chunk.system_fingerprint); + usageMetadata(chunk.usage); + if (!Array.isArray(chunk.choices)) refuse('invalid_choices'); + if (chunk.id != null) { publicId(chunk.id); if (id && chunk.id !== id) refuse('changed_chat_id'); id = chunk.id; } + if (chunk.model != null && chunk.model !== model) refuse('changed_model'); + if (chunk.choices.length > 1) refuse('unsupported_choices'); + for (const choice of chunk.choices) { + shape(choice, ['index', 'delta'], ['finish_reason', 'logprobs', 'matched_stop']); + if (choice.index !== 0 || choice.logprobs != null || (choice.matched_stop != null && (!Number.isSafeInteger(choice.matched_stop) || choice.matched_stop < 0))) refuse('unsupported_choices'); + shape(choice.delta, [], ['role', ...fields.slice(0, 4), 'tool_calls']); + if (choice.delta.role != null && choice.delta.role !== 'assistant') refuse('invalid_role'); + if (finish && Object.keys(choice.delta).length) refuse('data_after_finish'); + if (choice.finish_reason != null) { + if (finish || !['stop', 'tool_calls', 'length', 'content_filter'].includes(choice.finish_reason)) refuse('invalid_finish'); + finish = choice.finish_reason; + } + } + chunks.push(chunk); + } + if (!done || !id || !finish || ['length', 'content_filter'].includes(finish)) refuse('incomplete_response'); + // Signature verification uses original bytes. Any unhashable keepalive is + // removed only as a complete exact frame, never by searching inside JSON. + const signatureBytes = Buffer.from(raw.replace(/(^|\r?\n\r?\n): keep-alive\r?\n\r?\n/g, '$1')); + return { chunks, id, finish, signatureBytes }; +} + +// This verifies model reply authenticity and its declared request hash. The +// server can delegate that hash to a trusted gateway; client input origin needs +// separate qualification before using these calls for private actions. +export async function authenticateNearNativeResponse({ bytes, requestBytes, profile, clientKey, getSignature }) { + profile = structuredClone(profile); + const parsed = parseNearNativeSse(bytes, profile.model); + const signature = await getSignature(parsed.id); + if (signature?.signature_kind !== 'provider_tee' || signature.signing_algo !== 'ecdsa') refuse('wrong_signer_kind'); + try { if (chatSignatureFails(signature, requestBytes, parsed.signatureBytes, profile)) refuse('signature_refused'); } + catch { refuse('signature_refused'); } + const open = v => { + if (v === '') return ''; + if (typeof v !== 'string' || !/^(?:[0-9a-fA-F]{2}){93,}$/.test(v)) refuse('plaintext_or_malformed_field'); + try { return decrypt(v, clientKey); } catch { refuse('ciphertext_refused'); } + }; + const chunks = structuredClone(parsed.chunks), calls = new Map(); let content = '', reasoning = '', refusal = '', usage = null; + for (const chunk of chunks) { + if (chunk.usage) usage = chunk.usage; + for (const choice of chunk.choices) { + const delta = choice.delta; + for (const field of fields.slice(0, 4)) if (delta[field] != null) delta[field] = open(delta[field]); + content += delta.content ?? ''; refusal += delta.refusal ?? ''; reasoning += delta.reasoning_content ?? delta.reasoning ?? ''; + if (delta.tool_calls != null) { + if (!Array.isArray(delta.tool_calls) || !delta.tool_calls.length) refuse('invalid_tool_calls'); + for (const call of delta.tool_calls) { + shape(call, ['index'], ['id', 'type', 'function']); + if (!Number.isSafeInteger(call.index) || call.index < 0 || call.index > 7 || (call.type != null && call.type !== 'function')) refuse('invalid_tool_calls'); + let combined = calls.get(call.index); + if (!combined) { combined = { id: null, type: 'function', function: { name: '', arguments: '' } }; calls.set(call.index, combined); } + if (call.id != null) { publicId(call.id); if (combined.id && combined.id !== call.id) refuse('changed_tool_id'); combined.id = call.id; } + if (call.function != null) { + shape(call.function, [], ['name', 'arguments']); + for (const field of ['name', 'arguments']) if (call.function[field] != null) { + call.function[field] = open(call.function[field]); combined.function[field] += call.function[field]; + } + } + } + } + } + } + if ((parsed.finish === 'tool_calls') !== (calls.size > 0)) refuse('tool_finish_mismatch'); + const ids = new Set(); + for (const call of calls.values()) { + if (!call.id || ids.has(call.id) || !call.function.name) refuse('incomplete_tool_call'); + ids.add(call.id); + try { if (!record(JSON.parse(call.function.arguments))) refuse('invalid_tool_arguments'); } catch { refuse('invalid_tool_arguments'); } + } + const result = deepFreeze({ model: profile.model, id: parsed.id, finish: parsed.finish, content, reasoning, refusal, calls: [...calls.values()], usage, chunks }); + authenticatedResults.add(result); return result; +} + +// Pi expects the complete function name in the first tool delta. Consolidate +// authenticated fragments before handing them to its ordinary tool parser. +export function authenticatedNearNativeSse(result) { + if (!authenticatedResults.has(result)) refuse('unauthenticated_result'); + const delta = { role: 'assistant', content: result.content }; + if (result.reasoning) delta.reasoning_content = result.reasoning; + if (result.refusal) delta.refusal = result.refusal; + if (result.calls.length) delta.tool_calls = result.calls.map((call, index) => ({ index, ...call })); + const chunk = { id: result.id, model: result.model, object: 'chat.completion.chunk', choices: [{ index: 0, delta, finish_reason: result.finish }], usage: result.usage }; + return `data: ${JSON.stringify(chunk)}\n\ndata: [DONE]\n\n`; +} diff --git a/tests/near-deployment-evidence.test.py b/tests/near-deployment-evidence.test.py new file mode 100644 index 0000000..a2d2b58 --- /dev/null +++ b/tests/near-deployment-evidence.test.py @@ -0,0 +1,84 @@ +"""Synthetic digest/nonce negative fixtures; no network or credentials.""" +import importlib.util +import hashlib +import json +from pathlib import Path +import unittest + +spec = importlib.util.spec_from_file_location('evidence', Path(__file__).resolve().parents[1] / 'tools/near-deployment-evidence.py') +evidence = importlib.util.module_from_spec(spec) +spec.loader.exec_module(evidence) +NONCE = 'ab' * 32 + + +def fixture(actions=None, encoding='canonical-string-v1'): + actions = actions or [{'timestamp': 'now', 'action': 'compose_up', 'file': 'résumé[\\"].yaml', 'services': ['one', 'two']}] + encoded = evidence.canonical_actions(actions) if encoding == 'canonical-string-v1' else json.dumps(actions) + digest = hashlib.sha256(encoded.encode()).hexdigest() + cm = {'actions': actions, 'actions_hash': digest, 'nonce': NONCE, 'nonce_source': 'client', 'report_data': digest + NONCE} + return {'gateway_attestation': {'request_nonce': NONCE}, 'model_attestations': [{'request_nonce': NONCE, 'compose_manager_attestation': cm}]} + + +class EvidenceTests(unittest.TestCase): + def inspect(self, report, nonce=NONCE, encoding='canonical-string-v1'): + return evidence.inspect(json.dumps(report, indent=2, ensure_ascii=False).encode(), nonce, encoding) + + def test_canonical_digest_survives_key_order_and_whitespace(self): + report = fixture() + cm = report['model_attestations'][0]['compose_manager_attestation'] + cm['actions'][0] = dict(reversed(list(cm['actions'][0].items()))) + result = self.inspect(report) + self.assertFalse(result['managers'][0]['quote_verified']) + self.assertFalse(result['private_work_enabled']) + + def test_altered_actions_rejected(self): + report = fixture() + report['model_attestations'][0]['compose_manager_attestation']['actions'][0]['file'] = 'other' + with self.assertRaisesRegex(ValueError, 'digest'): + self.inspect(report) + + def test_nonce_swaps_rejected(self): + for who in ['gateway', 'model', 'manager']: + with self.subTest(who=who): + report = fixture() + if who == 'gateway': + report['gateway_attestation']['request_nonce'] = 'cd' * 32 + elif who == 'model': + report['model_attestations'][0]['request_nonce'] = 'cd' * 32 + else: + report['model_attestations'][0]['compose_manager_attestation']['nonce'] = 'cd' * 32 + with self.assertRaisesRegex(ValueError, 'nonce'): + self.inspect(report) + + def test_report_data_rejected(self): + report = fixture() + report['model_attestations'][0]['compose_manager_attestation']['report_data'] = '00' * 64 + with self.assertRaisesRegex(ValueError, 'report-data'): + self.inspect(report) + + def test_duplicates_and_numeric_schema_rejected(self): + with self.assertRaisesRegex(ValueError, 'duplicate'): + evidence.inspect(b'{"x":1,"x":2}', NONCE) + for value in [1, 1.5, True, {'nested': 'value'}]: + with self.subTest(value=value), self.assertRaises(ValueError): + evidence.canonical_actions([{'timestamp': 'now', 'action': 'compose_up', 'tag': value}]) + with self.assertRaisesRegex(ValueError, 'schema'): + evidence.canonical_actions([{'timestamp': 'now', 'action': 'compose_up', 'unknown': 'value'}]) + + def test_raw_path_handles_escaped_brackets_and_nested_values(self): + text = '{"outside":[],"models":[{"actions": [ {"name":"a]\\\"b", "values":[1,2]} ]}]}' + evidence.DECODER.decode(text) + self.assertEqual(evidence.raw_path(text, ['models', 0, 'actions']), '[ {"name":"a]\\\"b", "values":[1,2]} ]') + + def test_exact_mode_is_explicit_and_reformatting_refused(self): + report = fixture(encoding='exact') + raw = json.dumps(report).encode() + self.assertTrue(evidence.inspect(raw, NONCE, 'exact')['managers'][0]['nonce_matches']) + with self.assertRaisesRegex(ValueError, 'digest'): + self.inspect(report, encoding='exact') + with self.assertRaises(ValueError): + evidence.inspect(raw, NONCE, 'unknown') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/near-native-qualification.test.mjs b/tests/near-native-qualification.test.mjs new file mode 100644 index 0000000..b3f0668 --- /dev/null +++ b/tests/near-native-qualification.test.mjs @@ -0,0 +1,186 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import crypto from 'node:crypto'; +import { secp256k1 } from '@noble/curves/secp256k1.js'; +import { keccak256 } from '../lib/keccak.mjs'; +import { encrypt, decrypt } from '../lib/near.mjs'; +import { NEAR_NATIVE_PI_COMPAT, encryptNearNativeRequest, nearNativeHeaders, authenticateNearNativeResponse, authenticatedNearNativeSse } from '../lib/near-native-qualification.mjs'; + +const modelKey = crypto.createECDH('secp256k1'); modelKey.generateKeys(); +const clientKey = crypto.createECDH('secp256k1'); clientKey.generateKeys(); +const address = key => '0x' + keccak256(key.getPublicKey().subarray(1)).subarray(12).toString('hex'); +const profile = { model: 'synthetic/model', model_signing_address: address(modelKey), gateway_signing_address: '0x' + '11'.repeat(20) }; +const seal = value => encrypt(value, clientKey.getPublicKey('hex')); +const schemaCanary = 'synthetic-schema-canary', promptCanary = 'synthetic-prompt-canary', argumentCanary = 'synthetic-argument-canary', resultCanary = 'synthetic-result-canary'; +const body = () => ({ model: profile.model, stream: true, max_tokens: 128, + messages: [{ role: 'system', content: promptCanary }, { role: 'user', content: promptCanary }, + { role: 'assistant', content: null, reasoning_content: promptCanary, refusal: promptCanary, tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'synthetic_echo', arguments: JSON.stringify({ value: argumentCanary }) } }] }, + { role: 'tool', tool_call_id: 'call_1', name: 'synthetic_echo', content: resultCanary }], + tools: [{ type: 'function', function: { name: 'synthetic_echo', description: schemaCanary, + parameters: { type: 'object', properties: { value: { type: 'string', description: schemaCanary } }, required: ['value'] } } }], + tool_choice: { type: 'function', function: { name: 'synthetic_echo' } }, parallel_tool_calls: false, stream_options: { include_usage: true }, chat_template_kwargs: { enable_thinking: false } }); +const requestBytes = Buffer.from('synthetic encrypted request identity'); +const signature = (request, response, key = modelKey) => { + const hash = bytes => crypto.createHash('sha256').update(bytes).digest('hex'); + const text = `${profile.model}:${hash(request)}:${hash(response)}`; + const digest = keccak256(Buffer.concat([Buffer.from(`\x19Ethereum Signed Message:\n${text.length}`), Buffer.from(text)])); + const recovered = secp256k1.sign(digest, key.getPrivateKey(), { prehash: false, format: 'recovered' }); + return { signing_algo: 'ecdsa', signature_kind: 'provider_tee', text, + signature: '0x' + Buffer.concat([Buffer.from(recovered.subarray(1)), Buffer.from([recovered[0] + 27])]).toString('hex') }; +}; +const sse = (chunks, suffix = 'data: [DONE]\n\n') => Buffer.from(chunks.map(chunk => `data:${JSON.stringify({ id: 'synthetic_chat', model: profile.model, choices: [{ index: 0, ...chunk }] })}\n\n`).join('') + suffix); +const response = () => sse([ + { delta: { reasoning_content: seal('synthetic reasoning'), tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: seal('synthetic_'), arguments: seal('{"value":') } }] } }, + { delta: { tool_calls: [{ index: 0, function: { name: seal('echo'), arguments: seal('"synthetic-result"}') } }] }, finish_reason: 'tool_calls' }, +]); +const authenticate = (bytes, sig = signature(requestBytes, bytes), extra = {}) => authenticateNearNativeResponse({ bytes, requestBytes, profile, clientKey, getSignature: async () => sig, ...extra }); + +test('all sensitive request fields are encrypted and round-trip, including schemas and history', () => { + const original = body(), encoded = encryptNearNativeRequest(original, modelKey.getPublicKey('hex')); + const wire = JSON.stringify(encoded); + for (const canary of [schemaCanary, promptCanary, argumentCanary, resultCanary, 'synthetic_echo']) assert.ok(!wire.includes(canary)); + assert.equal(decrypt(encoded.messages[0].content, modelKey), promptCanary); + assert.equal(decrypt(encoded.messages[2].tool_calls[0].function.arguments, modelKey), original.messages[2].tool_calls[0].function.arguments); + assert.equal(decrypt(encoded.messages[2].reasoning_content, modelKey), promptCanary); + assert.equal(decrypt(encoded.messages[2].refusal, modelKey), promptCanary); + assert.equal(decrypt(encoded.messages[3].content, modelKey), resultCanary); + assert.equal(decrypt(encoded.messages[3].name, modelKey), 'synthetic_echo'); + assert.deepEqual(JSON.parse(decrypt(encoded.tools[0].function.parameters, modelKey)), original.tools[0].function.parameters); + assert.equal(decrypt(encoded.tools[0].function.description, modelKey), schemaCanary); + assert.equal(decrypt(encoded.tool_choice.function.name, modelKey), 'synthetic_echo'); + assert.equal(encoded.messages[3].tool_call_id, 'call_1'); + assert.equal(original.messages[0].content, promptCanary, 'caller history is not mutated'); +}); + +test('unsupported legacy, multimodal, structured-output and server-side tools refuse before a request', () => { + for (const changed of [ + { ...body(), functions: [{ name: schemaCanary }] }, { ...body(), response_format: { schema: schemaCanary } }, + { ...body(), logprobs: true }, { ...body(), tools: [{ type: 'web_context_search' }] }, + { ...body(), messages: [{ role: 'user', content: [{ type: 'image_url', image_url: { url: schemaCanary } }] }] }, + { ...body(), messages: [{ role: 'assistant', function_call: { arguments: argumentCanary } }] }, + ]) assert.throws(() => encryptNearNativeRequest(changed, modelKey.getPublicKey('hex'))); +}); + +test('both protocol headers are explicit; version alone is not the all-fields switch', () => { + const headers = nearNativeHeaders(clientKey.getPublicKey('hex'), modelKey.getPublicKey('hex')); + assert.equal(headers['X-Encrypt-All-Fields'], 'true'); assert.equal(headers['X-Encryption-Version'], '2'); + assert.equal(headers['X-Client-Pub-Key'].length, 128); +}); + +test('a genuine attested-model signature releases complete encrypted tool fragments', async () => { + const result = await authenticate(response()); + assert.equal(result.calls[0].function.name, 'synthetic_echo'); + assert.deepEqual(JSON.parse(result.calls[0].function.arguments), { value: 'synthetic-result' }); + assert.equal(result.reasoning, 'synthetic reasoning'); +}); + +test('a gateway signature, another signer, changed request or edited reply cannot release tools', async () => { + const bytes = response(), sig = signature(requestBytes, bytes), other = crypto.createECDH('secp256k1'); other.generateKeys(); + for (const [value, proof, extra] of [ + [bytes, { ...sig, signature_kind: 'gateway' }, {}], [bytes, signature(requestBytes, bytes, other), {}], + [bytes, sig, { requestBytes: Buffer.from('another request') }], + [Buffer.from(bytes.toString().replace('call_1', 'call_2')), sig, {}], + ]) await assert.rejects(authenticate(value, proof, extra)); +}); + +test('ciphertext created by anyone for the public client key still needs model authentication', async () => { + const forged = sse([{ delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: seal('synthetic_echo'), arguments: seal('{"value":"forged"}') } }] }, finish_reason: 'tool_calls' }]); + await assert.rejects(authenticate(forged, signature(requestBytes, response()))); +}); + +test('plaintext and corrupt ciphertext are refused even if the model signs them', async () => { + for (const name of ['synthetic_echo', '00'.repeat(100)]) { + const bytes = sse([{ delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name, arguments: seal('{}') } }] }, finish_reason: 'tool_calls' }]); + await assert.rejects(authenticate(bytes)); + } +}); + +test('truncated streams, changed chat IDs, duplicate DONE and post-finish data refuse', async () => { + const bytes = response(); + for (const bad of [bytes.subarray(0, bytes.length - 2), Buffer.from(bytes.toString().replace('data: [DONE]\n\n', '')), Buffer.from(bytes.toString().replace('data: [DONE]\n\n', 'data: [DONE]\n\ndata: [DONE]\n\n')), Buffer.from(bytes.toString().replace('data: [DONE]', 'data: {"id":"another","choices":[]}\n\ndata: [DONE]'))]) { + await assert.rejects(authenticate(bad)); + } +}); + +test('length truncation and invalid argument JSON never release an executable call', async () => { + for (const reason of ['length', 'tool_calls']) { + const bytes = sse([{ delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: seal('synthetic_echo'), arguments: seal('{"unfinished":') } }] }, finish_reason: reason }]); + await assert.rejects(authenticate(bytes)); + } +}); + +test('the exact unsigned keepalive frame is excluded without altering signed JSON', async () => { + const bytes = response(), padded = Buffer.concat([Buffer.from(': keep-alive\n\n'), bytes]); + const result = await authenticate(padded, signature(requestBytes, bytes)); + assert.equal(result.calls.length, 1); + const injected = Buffer.concat([Buffer.from(': keep-alive with payload\n\n'), bytes]); + await assert.rejects(authenticate(injected, signature(requestBytes, bytes))); +}); + +test('SGLang numeric stop-token metadata is allowed; plaintext stop strings are refused', async () => { + const valid = sse([{ delta: { content: seal('synthetic answer') }, finish_reason: 'stop', matched_stop: 154829 }]); + assert.equal((await authenticate(valid)).content, 'synthetic answer'); + const invalid = sse([{ delta: { content: seal('synthetic answer') }, finish_reason: 'stop', matched_stop: 'synthetic-sensitive-stop' }]); + await assert.rejects(authenticate(invalid)); +}); + +test('actual Pi accepts authenticated native calls and encrypts its tool-result history', async () => { + const { streamSimple } = await import('../vendor/pi/packages/ai/dist/api/openai-completions.js'); + const model = { id: profile.model, name: 'Synthetic', provider: 'nearai', api: 'openai-completions', baseUrl: 'http://synthetic.invalid/v1', + reasoning: false, input: ['text'], compat: NEAR_NATIVE_PI_COMPAT, contextWindow: 4096, maxTokens: 128, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } }; + const context = { messages: [{ role: 'system', content: 'Synthetic tool qualification.', timestamp: 0, toolsAdded: [{ name: 'synthetic_echo', description: schemaCanary, parameters: { type: 'object', properties: { value: { type: 'string' } }, required: ['value'] } }] }, + { role: 'user', content: promptCanary, timestamp: Date.now() }] }; + const wires = []; let encodeFailure = null; + const fetch = async (_url, init) => { + const input = JSON.parse(init.body); let outgoing; + try { outgoing = encryptNearNativeRequest(input, modelKey.getPublicKey('hex')); } + catch (error) { encodeFailure = { code: error.code, keys: Object.keys(input), messageKeys: input.messages.map(m => Object.keys(m)), functionKeys: input.tools?.map(t => Object.keys(t.function)) }; throw error; } + const request = JSON.stringify(outgoing); wires.push(request); + for (const secret of [promptCanary, schemaCanary, resultCanary, 'synthetic_echo']) assert.ok(!request.includes(secret)); + const bytes = response(), sig = signature(request, bytes); + const verified = await authenticateNearNativeResponse({ bytes, requestBytes: request, profile, clientKey, getSignature: async () => sig }); + const decoded = authenticatedNearNativeSse(verified); + return new Response(decoded, { headers: { 'content-type': 'text/event-stream' } }); + }; + const reply = await streamSimple(model, context, { apiKey: 'synthetic', maxTokens: 128, fetch }).result(); + assert.equal(encodeFailure, null, JSON.stringify(encodeFailure)); + assert.equal(reply.stopReason, 'toolUse', reply.errorMessage); + const call = reply.content.find(part => part.type === 'toolCall'); + assert.equal(call.name, 'synthetic_echo'); assert.deepEqual(call.arguments, { value: 'synthetic-result' }); + context.messages.push(reply, { role: 'toolResult', toolCallId: call.id, toolName: call.name, content: [{ type: 'text', text: resultCanary }], isError: false, timestamp: Date.now() }); + const second = await streamSimple(model, context, { apiKey: 'synthetic', maxTokens: 128, fetch }).result(); + assert.equal(encodeFailure, null, JSON.stringify(encodeFailure)); + assert.equal(second.stopReason, 'toolUse', second.errorMessage); + assert.equal(wires.length, 2); +}); + +test('only immutable authenticated results can be released to the native tool parser', async () => { + assert.throws(() => authenticatedNearNativeSse({ calls: [] }), /unauthenticated_result/); + const verified = await authenticate(response()); + assert.throws(() => { verified.calls[0].function.name = 'another_tool'; }, TypeError); + const altered = structuredClone(profile), bytes = response(), other = crypto.createECDH('secp256k1'); other.generateKeys(); + await assert.rejects(authenticateNearNativeResponse({ bytes, requestBytes, profile: altered, clientKey, + getSignature: async () => { altered.model_signing_address = address(other); return signature(requestBytes, bytes, other); } })); +}); + +test('public metadata cannot contain unexpected plaintext payload fields', async () => { + for (const added of [{ usage: { prompt_tokens: 1, private_text: 'synthetic secret' } }, { created: 'synthetic secret' }, { object: 'synthetic secret' }]) { + const valid = sse([{ delta: { content: seal('synthetic answer') }, finish_reason: 'stop' }]); + const event = JSON.parse(valid.toString().split('\n')[0].slice(5)); Object.assign(event, added); + const bytes = Buffer.from(`data:${JSON.stringify(event)}\n\ndata: [DONE]\n\n`); + await assert.rejects(authenticate(bytes)); + } +}); + +test('a model signature cannot prove input origin when a trusted gateway supplies the claimed hash', async () => { + // Expected protocol limitation, not a deployed exploit. The reviewed server + // lets config.token callers override request_hash with X-Request-Hash. + // The signature alone cannot distinguish its own wire hash from that claim. + const differentModelInput = Buffer.from('synthetic gateway-selected model input'); + assert.notDeepEqual(differentModelInput, requestBytes); + const reply = sse([{ delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', + function: { name: seal('synthetic_echo'), arguments: seal('{"value":"gateway-selected"}') } }] }, finish_reason: 'tool_calls' }]); + const genuineModelSignature = signature(requestBytes, reply); + const result = await authenticate(reply, genuineModelSignature); + assert.equal(JSON.parse(result.calls[0].function.arguments).value, 'gateway-selected'); +}); diff --git a/tests/verify.sh b/tests/verify.sh index 026fe89..e447adb 100755 --- a/tests/verify.sh +++ b/tests/verify.sh @@ -34,6 +34,8 @@ suites=( "offline|chat-signature (NEAR per-chat signature, recorded reply and forgeries)|node --test tests/chat-signature.test.mjs" "offline|mac-coupon-issuer (Apple issuer binding, synthetic contract only)|node --test tests/mac-coupon-issuer.test.mjs" "offline|coupon-staging (signed service staging admission, exact schema)|node --test tests/coupon-staging.test.mjs" + "offline|NEAR native qualification (synthetic, no provider calls)|node --test tests/near-native-qualification.test.mjs" + "offline|NEAR deployment evidence (synthetic, no provider calls)|python3 tests/near-deployment-evidence.test.py" "offline|intools (in-content tool call parser)|node --test tests/intools.test.mjs" "offline|regress (tests/regress fixtures replayed)|tests/regress/run.sh" "offline|report (/report, abalone report)|tests/report.sh" "offline|fetch (/fetch routes, stub arXiv)|tests/fetch.sh" "offline|tor (built-in Tor transport, stub client)|node --test tests/tor.test.mjs" diff --git a/tools/near-deployment-evidence.py b/tools/near-deployment-evidence.py new file mode 100644 index 0000000..62d0848 --- /dev/null +++ b/tools/near-deployment-evidence.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +"""Collect exact NEAR attestation JSON and inspect manager action bindings. + +Read-only: no chat requests, policy changes, or private-work admission. Digest +and claimed report-data checks are not Intel quote verification. +""" +import argparse +import hashlib +import json +import os +from pathlib import Path +import re +import secrets +import urllib.parse +import urllib.request + +MAX_BYTES = 8 * 1024 * 1024 + + +def unique_object(pairs): + result = {} + for key, value in pairs: + if key in result: + raise ValueError('duplicate JSON key') + result[key] = value + return result + + +DECODER = json.JSONDecoder(object_pairs_hook=unique_object, + parse_constant=lambda _: (_ for _ in ()).throw(ValueError('nonfinite JSON'))) + + +def skip_space(text, start): + while start < len(text) and text[start] in ' \t\r\n': + start += 1 + return start + + +def raw_path(text, route, start=0): + """Return an exact value slice at an object/array path; full JSON validated first.""" + start = skip_space(text, start) + if not route: + _, end = DECODER.raw_decode(text, start) + return text[start:end] + target, *rest = route + if text[start] == '{' and isinstance(target, str): + pos = skip_space(text, start + 1) + while text[pos] != '}': + key, pos = DECODER.raw_decode(text, pos) + pos = skip_space(text, pos) + if text[pos] != ':': + raise ValueError('invalid object') + pos = skip_space(text, pos + 1) + if key == target: + return raw_path(text, rest, pos) + _, pos = DECODER.raw_decode(text, pos) + pos = skip_space(text, pos) + if text[pos] == ',': + pos = skip_space(text, pos + 1) + elif text[start] == '[' and isinstance(target, int): + pos, index = skip_space(text, start + 1), 0 + while text[pos] != ']': + if index == target: + return raw_path(text, rest, pos) + _, pos = DECODER.raw_decode(text, pos) + pos = skip_space(text, pos) + if text[pos] == ',': + pos = skip_space(text, pos + 1) + index += 1 + raise ValueError('missing raw JSON path') + + +def canonical_actions(actions): + allowed = {'timestamp', 'action', 'tag', 'commit', 'file', 'file_sha256', 'services', 'container', 'image'} + normalized = [] + for action in actions: + if not isinstance(action, dict) or not {'timestamp', 'action'} <= action.keys() or action.keys() - allowed: + raise ValueError('unsupported action schema') + item = {} + for key, value in action.items(): + if key == 'services': + if not isinstance(value, list) or any(not isinstance(v, str) for v in value): + raise ValueError('unsupported action value') + if value: + item[key] = value + elif isinstance(value, str): + item[key] = value + elif value is not None or key in {'timestamp', 'action'}: + raise ValueError('unsupported action value') + normalized.append(item) + return json.dumps(normalized, sort_keys=True, separators=(',', ':'), ensure_ascii=False) + + +def inspect(raw, nonce, encoding='canonical-string-v1'): + if encoding not in {'exact', 'canonical-string-v1'} or len(raw) > MAX_BYTES or not isinstance(nonce, str) or not re.fullmatch('[0-9a-f]{64}', nonce): + raise ValueError('invalid bounds or nonce') + text = raw.decode('utf-8', errors='strict') + report = DECODER.decode(text) + if report['gateway_attestation']['request_nonce'] != nonce: + raise ValueError('gateway nonce mismatch') + models = report['model_attestations'] + if not isinstance(models, list) or not models: + raise ValueError('missing models') + managers = [] + for i, model in enumerate(models): + if model['request_nonce'] != nonce: + raise ValueError('model nonce mismatch') + cm = model.get('compose_manager_attestation') + if not isinstance(cm, dict): + raise ValueError('missing compose-manager evidence') + actions = cm['actions'] + if not isinstance(actions, list): + raise ValueError('invalid actions') + exact = raw_path(text, ['model_attestations', i, 'compose_manager_attestation', 'actions']) + encoded = exact if encoding == 'exact' else canonical_actions(actions) + digest = hashlib.sha256(encoded.encode('utf-8')).hexdigest() + if cm['actions_hash'] != digest: + raise ValueError('action digest mismatch') + if cm['nonce'] != nonce or cm['nonce_source'] != 'client': + raise ValueError('manager nonce mismatch') + if cm['report_data'].lower() != digest + nonce: + raise ValueError('claimed report-data mismatch') + managers.append({'model_index': i, 'actions_sha256': digest, 'action_encoding': encoding, + 'action_count': len(actions), 'nonce_matches': True, + 'claimed_report_data_matches': True, + 'quote_verified': False, 'deployment_source_closed': False}) + return {'raw_sha256': hashlib.sha256(raw).hexdigest(), 'nonce': nonce, + 'managers': managers, 'private_work_enabled': False} + + +def save_new(path, data): + fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, 'wb') as output: + output.write(data) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--raw', type=Path) + parser.add_argument('--nonce') + parser.add_argument('--action-encoding', choices=['exact', 'canonical-string-v1'], default='canonical-string-v1') + parser.add_argument('--model') + parser.add_argument('--configured-key', action='store_true') + parser.add_argument('--out', type=Path) + parser.add_argument('--inspect-only', action='store_true') + args = parser.parse_args() + if args.inspect_only: + if not args.raw or not args.nonce or args.out or args.model or args.configured_key: + parser.error('--inspect-only requires --raw and --nonce only') + with args.raw.open('rb') as source: + result = inspect(source.read(MAX_BYTES + 1), args.nonce, args.action_encoding) + print(json.dumps(result)) + return + if not args.out: + parser.error('--out is required for capture or saved inspection') + args.out.mkdir(mode=0o700, parents=True, exist_ok=False) + if args.raw: + if not args.nonce or args.model or args.configured_key: + parser.error('offline inspection requires --raw and --nonce only') + with args.raw.open('rb') as source: + raw, nonce = source.read(MAX_BYTES + 1), args.nonce + else: + if not args.model or args.nonce: + parser.error('capture requires --model; nonce is generated locally') + key = os.environ.get('NEARAI_API_KEY') + if not key and args.configured_key: + records = json.loads((Path.home() / '.abalone/node/keys.json').read_text()) + key = next((k['key'] for k in records.get('keys', []) + if k['provider'] == 'nearai' and k['label'] == records.get('active', {}).get('nearai')), None) + if not key: + raise ValueError('configured key unavailable') + nonce = secrets.token_hex(32) + query = urllib.parse.urlencode({'model': args.model, 'nonce': nonce, + 'signing_algo': 'ecdsa', 'include_tls_fingerprint': 'true'}) + request = urllib.request.Request('https://cloud-api.near.ai/v1/attestation/report?' + query, + headers={'Authorization': 'Bearer ' + key}) + # Refuse redirects rather than forwarding a credential to another origin. + class NoRedirect(urllib.request.HTTPRedirectHandler): + def redirect_request(self, *unused, **kwargs): + return None + with urllib.request.build_opener(NoRedirect).open(request, timeout=45) as response: + raw = response.read(MAX_BYTES + 1) + key = None + save_new(args.out / 'attestation.raw.json', raw) + save_new(args.out / 'capture.json', (json.dumps({'nonce': nonce, 'model': args.model, + 'no_chat_posts': True, 'raw_sha256': hashlib.sha256(raw).hexdigest()}, indent=2) + '\n').encode()) + result = inspect(raw, nonce, args.action_encoding) + save_new(args.out / 'manager-bindings.json', (json.dumps(result, indent=2) + '\n').encode()) + print(json.dumps({'manager_count': len(result['managers']), 'action_digests_match': True, + 'quote_verified': False, 'no_chat_posts': True})) + + +if __name__ == '__main__': + main() diff --git a/tools/qualify-near-native.mjs b/tools/qualify-near-native.mjs new file mode 100644 index 0000000..98e2eb2 --- /dev/null +++ b/tools/qualify-near-native.mjs @@ -0,0 +1,118 @@ +#!/usr/bin/env node +// Three synthetic requests at most, with a pre-reserved catalog-price budget. +// This tool does not enable native tools in the normal client. +import crypto from 'node:crypto'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { verifyNearModel } from '../lib/near.mjs'; +import { encryptNearNativeRequest, nearNativeHeaders, authenticateNearNativeResponse } from '../lib/near-native-qualification.mjs'; + +const args = process.argv.slice(2), arg = name => { const i = args.indexOf(name); return i < 0 ? null : args[i + 1]; }; +const model = arg('--model'), budget = Number(arg('--budget-usd')), directory = arg('--out'); +const protocolOnly = args.includes('--protocol-only'), roundCount = Number(arg('--rounds') ?? 3); +if (![2, 3].includes(roundCount)) throw new Error('--rounds must be 2 or 3'); +if (!model || !directory || !Number.isFinite(budget) || budget <= 0 || budget > 0.25) throw new Error('Specify --model, --budget-usd (at most 0.25), and a fresh --out directory'); +const output = path.resolve(directory); fs.mkdirSync(output, { recursive: true, mode: 0o700 }); +const file = name => path.join(output, name); +const write = (name, value) => fs.writeFileSync(file(name), JSON.stringify(value, null, 2), { mode: 0o600 }); +const journal = { model, budget_usd: budget, max_posts: roundCount, posts: 0, reserved_usd: 0, observed_usage_cost_usd: 0, mode: protocolOnly ? 'protocol-only' : 'strict', started_at: new Date().toISOString() }; +fs.writeFileSync(file('journal.json'), JSON.stringify(journal, null, 2), { flag: 'wx', mode: 0o600 }); + +let key = process.env.NEARAI_API_KEY, admitted, phase = 'credential', ledgerLock = null, ledgerLockFile = null; +try { + if (!key && args.includes('--configured-key')) { + const records = JSON.parse(fs.readFileSync(path.join(os.homedir(), '.abalone/node/keys.json'), 'utf8')); + key = records.keys?.find(k => k.provider === 'nearai' && k.label === records.active?.nearai)?.key; + } + if (!key) throw Object.assign(new Error('key unavailable'), { code: 'configured_key_unavailable' }); + const baseUrl = 'https://cloud-api.near.ai/v1'; + const deadline = AbortSignal.timeout(120000); + const get = async p => { + const response = await fetch(new URL(baseUrl).origin + p, { headers: { authorization: `Bearer ${key}` }, signal: deadline, redirect: 'error' }); + if (!response.ok) throw Object.assign(new Error('read refused'), { code: `read_http_${response.status}` }); + return response; + }; + phase = 'catalog'; + const catalog = await (await get('/v1/models')).json(), selected = catalog.data?.find(m => m.id === model); + if (!selected || selected.owned_by !== 'nearai' || !selected.supported_features?.includes('tools')) throw Object.assign(new Error('unsupported model'), { code: 'catalog_capability_refused' }); + const inputRate = Number(selected.pricing?.prompt), outputRate = Number(selected.pricing?.completion), context = selected.context_length; + if (!Number.isFinite(inputRate) || inputRate < 0 || !Number.isFinite(outputRate) || outputRate < 0 || Number(selected.pricing?.request) !== 0 || !Number.isSafeInteger(context) || context < 1024 || context > 2097152) throw Object.assign(new Error('unbounded pricing'), { code: 'pricing_refused' }); + const perPostReserve = context * inputRate + 128 * outputRate; + if (perPostReserve > budget) throw Object.assign(new Error('budget too small'), { code: 'budget_refused' }); + write('catalog.json', selected); + // Reserve an entire context window before each POST. Release only after + // signed usage establishes its charge. Unknown failures retain their reserve. + const ledgerFile = path.join(path.dirname(output), 'budget-ledger.json'); + ledgerLockFile = ledgerFile + '.lock'; ledgerLock = fs.openSync(ledgerLockFile, 'wx', 0o600); + const ledger = fs.existsSync(ledgerFile) ? JSON.parse(fs.readFileSync(ledgerFile, 'utf8')) : { model, budget_usd: budget, posts: 0, unknown_reserved_usd: 0, observed_usage_cost_usd: 0 }; + if (ledger.model !== model || ledger.budget_usd !== budget || !Number.isSafeInteger(ledger.posts) || ledger.posts < 0 || !Number.isFinite(ledger.unknown_reserved_usd) || ledger.unknown_reserved_usd < 0 || !Number.isFinite(ledger.observed_usage_cost_usd) || ledger.observed_usage_cost_usd < 0) throw Object.assign(new Error('invalid ledger'), { code: 'budget_ledger_refused' }); + const saveLedger = () => fs.writeFileSync(ledgerFile, JSON.stringify(ledger, null, 2), { mode: 0o600 }); + saveLedger(); + phase = 'attestation'; + let report; + const getReport = async p => { const response = await get(p); report = await response.clone().json(); return response; }; + const policy = { gatewayTcb: protocolOnly ? 'reported' : 'UpToDate', modelTcb: protocolOnly ? 'reported' : 'UpToDate', gpuSignature: 'required', composeRegistry: { rpc: 'https://mainnet.base.org', chainId: 8453 } }; + admitted = await verifyNearModel(baseUrl, model, getReport, policy); + write('attestation.json', report); write('admission.json', admitted.profile); + const strictTcbEligible = admitted.profile.gateway_tcb_status === 'UpToDate' && admitted.profile.model_tcb_statuses.every(status => status === 'UpToDate'); + const canaries = Array.from({ length: roundCount }, (_, i) => `synthetic_near_native_${i + 1}_${crypto.randomBytes(8).toString('hex')}`); + const description = `Synthetic echo qualification only. ${canaries[0]}`; + const tools = [{ type: 'function', function: { name: 'synthetic_echo', description, + parameters: { type: 'object', properties: { value: { type: 'string', description } }, required: ['value'], additionalProperties: false } } }]; + const messages = [{ role: 'system', content: 'Use only the synthetic_echo tool. Its input and result are synthetic test values.' }]; + const rounds = []; + for (let i = 0; i < roundCount; i++) { + phase = `round_${i + 1}_request`; + messages.push({ role: 'user', content: i < roundCount - 1 ? `Call synthetic_echo once with value exactly ${canaries[i]}.` : `Reply with exactly ${canaries[i]}. Do not call a tool.` }); + const input = { model, messages, tools, tool_choice: i < roundCount - 1 ? { type: 'function', function: { name: 'synthetic_echo' } } : 'none', parallel_tool_calls: false, + stream: true, stream_options: { include_usage: true }, max_tokens: 128, chat_template_kwargs: { enable_thinking: false }, temperature: 0 }; + const body = encryptNearNativeRequest(input, admitted.signingPublicKey), request = JSON.stringify(body); + if (Buffer.byteLength(JSON.stringify(input)) > 32768 || canaries.some(c => request.includes(c)) || request.includes('synthetic_echo')) throw Object.assign(new Error('wire canary'), { code: 'wire_canary_refused' }); + const clientKey = crypto.createECDH('secp256k1'); clientKey.generateKeys(); + const headers = { authorization: `Bearer ${key}`, 'content-type': 'application/json', ...nearNativeHeaders(clientKey.getPublicKey('hex'), admitted.signingPublicKey) }; + if (journal.posts >= roundCount || ledger.posts >= 6 || ledger.observed_usage_cost_usd + ledger.unknown_reserved_usd + perPostReserve > budget) throw Object.assign(new Error('budget exhausted'), { code: 'budget_refused' }); + ledger.posts++; ledger.unknown_reserved_usd += perPostReserve; saveLedger(); + journal.posts++; journal.reserved_usd += perPostReserve; write('journal.json', journal); // count before POST; no retry + fs.writeFileSync(file(`round-${i + 1}-request.json`), request, { mode: 0o600 }); + const response = await admitted.transport.fetch(baseUrl + '/chat/completions', { method: 'POST', headers, body: request, signal: deadline, redirect: 'error' }); + if (!response.ok || !response.headers.get('content-type')?.startsWith('text/event-stream')) throw Object.assign(new Error('response refused'), { code: `chat_http_${response.status}` }); + phase = `round_${i + 1}_response`; + const blocks = []; let bytes = 0; + for await (const block of response.body) { bytes += block.length; if (bytes > 1048576) throw Object.assign(new Error('too much output'), { code: 'response_bounds_refused' }); blocks.push(Buffer.from(block)); } + const raw = Buffer.concat(blocks); fs.writeFileSync(file(`round-${i + 1}-response.sse`), raw, { mode: 0o600 }); + let signature; + const result = await authenticateNearNativeResponse({ bytes: raw, requestBytes: request, profile: admitted.profile, clientKey, + getSignature: async id => { + const response = await admitted.transport.fetch(baseUrl + `/signature/${encodeURIComponent(id)}?model=${encodeURIComponent(model)}&signing_algo=ecdsa`, { headers: { authorization: `Bearer ${key}` }, signal: deadline, redirect: 'error' }); + if (!response.ok) throw Object.assign(new Error('signature refused'), { code: `signature_http_${response.status}` }); + signature = await response.json(); + write(`round-${i + 1}-signature.json`, signature); return signature; + } }); + write(`round-${i + 1}-decoded.json`, { content: result.content, reasoning: result.reasoning, calls: result.calls }); + if (!result.usage || !Number.isSafeInteger(result.usage.prompt_tokens) || !Number.isSafeInteger(result.usage.completion_tokens) || result.usage.prompt_tokens < 0 || result.usage.completion_tokens < 0) throw Object.assign(new Error('usage absent'), { code: 'usage_refused' }); + const cost = result.usage.prompt_tokens * inputRate + result.usage.completion_tokens * outputRate; + journal.reserved_usd = Math.max(0, journal.reserved_usd - perPostReserve); + journal.observed_usage_cost_usd += cost; write('journal.json', journal); + ledger.unknown_reserved_usd = Math.max(0, ledger.unknown_reserved_usd - perPostReserve); ledger.observed_usage_cost_usd += cost; saveLedger(); + if (cost > perPostReserve || journal.observed_usage_cost_usd > budget) throw Object.assign(new Error('observed budget exceeded'), { code: 'usage_budget_refused' }); + phase = `round_${i + 1}_echo`; + if (i < roundCount - 1) { + if (result.calls.length !== 1 || result.calls[0].function.name !== 'synthetic_echo') throw Object.assign(new Error('tool unexpected'), { code: 'unexpected_tool' }); + const args = JSON.parse(result.calls[0].function.arguments); + if (Object.keys(args).join() !== 'value' || args.value !== canaries[i]) throw Object.assign(new Error('argument changed'), { code: 'echo_argument_refused' }); + messages.push({ role: 'assistant', content: result.content || null, ...(result.reasoning ? { reasoning_content: result.reasoning } : {}), tool_calls: result.calls }); + messages.push({ role: 'tool', tool_call_id: result.calls[0].id, name: 'synthetic_echo', content: JSON.stringify({ value: args.value }) }); + } else if (result.calls.length || result.content.slice(result.content.indexOf('') < 0 ? 0 : result.content.indexOf('') + 8).trim() !== canaries[i]) throw Object.assign(new Error('final answer incorrect'), { code: 'final_answer_refused' }); + rounds.push({ round: i + 1, chat_id: result.id, finish: result.finish, tool_calls: result.calls.length, model_signature_verified: true, encrypted_fields_verified: true, usage: result.usage, usage_cost_usd: cost }); + write('rounds.json', rounds); + } + write('result.json', { protocol_qualified: true, strict_tcb_eligible: strictTcbEligible, private_work_enabled: false, + model, gateway_compose: admitted.profile.gateway_compose, model_composes: admitted.profile.compose_hashes, rounds, observed_usage_cost_usd: journal.observed_usage_cost_usd, + gateway_independent_request_integrity_qualified: false, limits: ['trusted gateway request-hash authority not qualified', 'source-to-deployment closure not established', 'normal provider integration and independent review outstanding'] }); + console.log(JSON.stringify({ protocol_qualified: true, strict_tcb_eligible: strictTcbEligible, model, posts: journal.posts, observed_usage_cost_usd: journal.observed_usage_cost_usd })); +} catch (error) { + const code = typeof error.code === 'string' && /^[a-zA-Z0-9_]+$/.test(error.code) ? error.code : 'qualification_refused'; + write('result.json', { protocol_qualified: false, phase, code, posts: journal.posts, reserved_usd: journal.reserved_usd, observed_usage_cost_usd: journal.observed_usage_cost_usd }); + console.log(JSON.stringify({ protocol_qualified: false, phase, code, posts: journal.posts })); process.exitCode = 1; +} finally { key = null; await admitted?.transport.close(); if (ledgerLock !== null) { fs.closeSync(ledgerLock); fs.unlinkSync(ledgerLockFile); } } diff --git a/tools/verify-near-manager-quote.mjs b/tools/verify-near-manager-quote.mjs new file mode 100644 index 0000000..23d268e --- /dev/null +++ b/tools/verify-near-manager-quote.mjs @@ -0,0 +1,45 @@ +#!/usr/bin/env node +// Read-only experimental evidence check; does not admit a provider or prove running state. +import fs from 'node:fs'; +import path from 'node:path'; +import crypto from 'node:crypto'; +import { createRequire } from 'node:module'; +import { execFileSync } from 'node:child_process'; +import { fileURLToPath } from 'node:url'; +const root = path.dirname(path.dirname(fileURLToPath(import.meta.url))); +const require = createRequire(path.join(root, 'vendor/phala-aci/package.json')); +const { getCollateralAndVerify, INTEL_PCS_URL } = require('@phala/dcap-qvl'); +const args = process.argv.slice(2); +const option = name => args[args.indexOf(name) + 1]; +if (!args.includes('--evidence') || !option('--evidence')) throw new Error('Specify --evidence capture-directory'); +const directory = path.resolve(option('--evidence')); +const capture = JSON.parse(fs.readFileSync(path.join(directory, 'capture.json'))); +const rawPath = path.join(directory, 'attestation.raw.json'); +const raw = fs.readFileSync(rawPath); +if (raw.length > 8 * 1024 * 1024 || !/^[0-9a-f]{64}$/.test(capture.nonce)) throw new Error('Invalid capture bounds or nonce'); +if (crypto.createHash('sha256').update(raw).digest('hex') !== capture.raw_sha256) throw new Error('Capture hash mismatch'); +const encoding = args.includes('--action-encoding') ? option('--action-encoding') : 'canonical-string-v1'; +// Recompute from raw evidence, never trust a pre-written manager-bindings verdict. +const summary = JSON.parse(execFileSync('python3', [path.join(root, 'tools/near-deployment-evidence.py'), '--inspect-only', '--raw', rawPath, '--nonce', capture.nonce, '--action-encoding', encoding], { maxBuffer: 1048576 })); +const report = JSON.parse(raw), models = []; +for (const binding of summary.managers) { + const model = report.model_attestations[binding.model_index], cm = model.compose_manager_attestation; + if (!/^(?:[0-9a-f]{2})+$/i.test(cm.quote) || !/^(?:[0-9a-f]{2})+$/i.test(model.intel_quote)) throw new Error('Invalid quote encoding'); + const verified = await getCollateralAndVerify(Buffer.from(cm.quote, 'hex'), INTEL_PCS_URL); + const td = verified.report.asTd10(); + if (Buffer.from(td.reportData).toString('hex') !== binding.actions_sha256 + capture.nonce) throw new Error('Manager quote binding mismatch'); + const outer = await getCollateralAndVerify(Buffer.from(model.intel_quote, 'hex'), INTEL_PCS_URL); + const ot = outer.report.asTd10(); + if (Buffer.from(ot.reportData).subarray(32).toString('hex') !== capture.nonce) throw new Error('Model quote nonce mismatch'); + const measurements = ['mrTd', 'rtMr0', 'rtMr1', 'rtMr2', 'rtMr3'].map(field => { + if (!td[field] || !ot[field] || !Buffer.from(td[field]).equals(Buffer.from(ot[field]))) throw new Error(`Manager/model ${field} mismatch`); + return { field, equal: true }; + }); + models.push({ model_index: binding.model_index, quote_verified: true, actions_digest_bound: true, nonce_bound: true, + tcb_status: verified.status, advisory_ids: verified.advisory_ids, model_tcb_status: outer.status, + measurements, action_count: binding.action_count, action_encoding: binding.action_encoding, + action_history_is_running_state: false, deployment_source_closed: false }); +} +const result = { raw_sha256: summary.raw_sha256, checked_at: new Date().toISOString(), models, private_work_enabled: false }; +fs.writeFileSync(path.join(directory, 'manager-quote-verification.json'), JSON.stringify(result, null, 2) + '\n', { flag: 'wx', mode: 0o600 }); +console.log(JSON.stringify(result));