@walkhi/code-relax 0.1.0-beta.82 → 0.1.0-beta.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"layout":"npm-platform-packages","version":"0.1.0-beta.82"}
1
+ {"layout":"npm-platform-packages","version":"0.1.0-beta.83"}
@@ -115,6 +115,38 @@
115
115
  } catch { return null; }
116
116
  }
117
117
  function elicitationResult(action, content = null) { return { action, content: action === 'accept' ? content : null, _meta: null }; }
118
+ function browserAuth(request) {
119
+ const p = request.params || {}, meta = p._meta || {};
120
+ if (request.method !== elicitation || p.mode !== 'form' || meta.codex_approval_kind !== 'browser_auth'
121
+ || p.requestedSchema?.type !== 'object' || !p.requestedSchema.properties || Object.keys(p.requestedSchema.properties).length
122
+ || !/^[A-Za-z0-9_-]{32,128}$/.test(meta.browser_auth_challenge_id || '')
123
+ || !Array.isArray(meta.fields) || !meta.fields.length || meta.fields.length > 6
124
+ || meta.options != null && (!Array.isArray(meta.options) || meta.options.length)
125
+ || meta.qrCode || meta.qrCodePayload || meta.qrCodeUrl || meta.webauthn != null) return null;
126
+ try {
127
+ const origin = new URL(meta.origin);
128
+ if (!['https:', 'http:'].includes(origin.protocol) || origin.origin !== meta.origin) return null;
129
+ } catch { return null; }
130
+ const ids = new Set();
131
+ for (const field of meta.fields) {
132
+ if (!field || typeof field.id !== 'string' || !/^[A-Za-z0-9_-]{1,48}$/.test(field.id) || ['__proto__', 'constructor', 'prototype'].includes(field.id)
133
+ || ids.has(field.id) || typeof field.label !== 'string' || typeof field.required !== 'boolean'
134
+ || !['email', 'number', 'password', 'search', 'tel', 'text', 'url'].includes(field.type)) return null;
135
+ ids.add(field.id);
136
+ }
137
+ return { origin: meta.origin, challengeId: meta.browser_auth_challenge_id, fields: meta.fields,
138
+ accountUsername: typeof meta.account_username === 'string' ? meta.account_username : null };
139
+ }
140
+ function validateBrowserAuthFields(auth, fields) {
141
+ if (!auth || !fields || typeof fields !== 'object' || Array.isArray(fields)
142
+ || !Object.keys(fields).length || Object.keys(fields).some(id => !auth.fields.some(field => field.id === id))) throw new Error('安全登录字段无效');
143
+ for (const field of auth.fields) {
144
+ const entry = fields[field.id];
145
+ if (entry === undefined && !field.required) continue;
146
+ if (!entry || Object.keys(entry).length !== 1 || typeof entry.value !== 'string'
147
+ || !entry.value.length) throw new Error('请填写有效的安全登录信息');
148
+ }
149
+ }
118
150
  function fieldOptions(field) {
119
151
  const schema = field.type === 'array' ? field.items : field;
120
152
  if (Array.isArray(schema?.enum)) return schema.enum.map((value, index) => ({ value, label: schema.enumNames?.[index] || value }));
@@ -191,6 +223,10 @@
191
223
  }
192
224
  function validate(request, result) {
193
225
  if (request.method === elicitation && equal(result, elicitationResult('cancel'))) return;
226
+ if (browserAuth(request)) {
227
+ if (!['decline', 'cancel'].some(action => equal(result, { action }))) throw new Error('安全登录必须通过专用输入入口提交');
228
+ return;
229
+ }
194
230
  if (externalAction(request)) {
195
231
  if (!['accept', 'decline'].some(action => equal(result, elicitationResult(action)))) throw new Error('外部操作结果无效');
196
232
  return;
@@ -307,5 +343,5 @@
307
343
  return { submitted: true };
308
344
  }
309
345
  }
310
- return { Store, decisions, validate, command, file, input, elicitation, permissions, externalAction, elicitationResult, formSchema, fieldOptions, validateForm, permissionResult, blocksComposer, browserAccess, browserAction, browserActionResult, computerAppAccess, computerAppResult, mcpToolApproval, mcpToolResult, confirmation, confirmationResult };
346
+ return { Store, decisions, validate, command, file, input, elicitation, permissions, externalAction, elicitationResult, formSchema, fieldOptions, validateForm, permissionResult, blocksComposer, browserAccess, browserAction, browserActionResult, browserAuth, validateBrowserAuthFields, computerAppAccess, computerAppResult, mcpToolApproval, mcpToolResult, confirmation, confirmationResult };
311
347
  });
@@ -0,0 +1,26 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { createHash, randomUUID } from 'node:crypto';
4
+
5
+ // Only submission markers are durable. Credentials never enter this store.
6
+ export class BrowserAuthOperations {
7
+ constructor(stateRoot) { this.directory = path.join(stateRoot, 'browser-auth-operations'); }
8
+ file(key) { return path.join(this.directory, createHash('sha256').update(JSON.stringify(key)).digest('hex') + '.json'); }
9
+ async read(key) {
10
+ try { return JSON.parse(await fs.readFile(this.file(key), 'utf8')).status; }
11
+ catch (error) { if (error.code === 'ENOENT') return null; throw error; }
12
+ }
13
+ async claim(key) {
14
+ await fs.mkdir(this.directory, { recursive: true });
15
+ let handle;
16
+ try { handle = await fs.open(this.file(key), 'wx'); }
17
+ catch (error) { if (error.code === 'EEXIST') throw new Error('此安全登录已提交或结果待核对,请勿重复提交'); throw error; }
18
+ try { await handle.writeFile(JSON.stringify({ status: 'unknown' })); await handle.sync(); }
19
+ finally { await handle.close(); }
20
+ }
21
+ async complete(key, status) {
22
+ const file = this.file(key), temporary = file + '.' + randomUUID() + '.tmp';
23
+ await fs.writeFile(temporary, JSON.stringify({ status }));
24
+ await fs.rename(temporary, file);
25
+ }
26
+ }