dsh-permission-rules 0.7.1 → 0.7.3

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.
package/src/allow-host.ts CHANGED
@@ -1,9 +1,16 @@
1
1
  /**
2
- * The settings-page "allow this host" action, as pure pieces: normalizing
2
+ * The settings-page "allow this host" action, host-only half: normalizing
3
3
  * and validating a blocked host, inserting ONE minimal
4
- * `match.network.domains` allow rule at the HEAD of a rule document, choosing
5
- * which rule file the rule belongs in, and mapping the action's result onto
6
- * the notice the page renders.
4
+ * `match.network.domains` allow rule at the HEAD of a rule document, and
5
+ * choosing which rule file the rule belongs in.
6
+ *
7
+ * The workspace choices (`allowHostWorkspaces`) and the notice mapping
8
+ * (`allowHostNotice`) live in `./allow-host-notice.ts`: the settings page
9
+ * imports them from the BROWSER bundle, whose bundler inlines every value
10
+ * import — keeping them next to the `node:path`/`node:net`/`yaml` code here
11
+ * emitted `require("process")`/`require("buffer")` calls the shell's frozen
12
+ * module table cannot answer, failing the whole plugin load. They are
13
+ * re-exported below so existing host-side imports keep working.
7
14
  *
8
15
  * Three invariants hold here and are asserted by `test/allow-host.spec.ts`:
9
16
  *
@@ -27,7 +34,11 @@ import { isIP } from 'node:net'
27
34
  import { isMap, isSeq, parseDocument } from 'yaml'
28
35
  import type { YAMLSeq } from 'yaml'
29
36
  import { RuleError, normalizeHost } from './rules.ts'
30
- import type { AllowHostResult, RuleSourceView } from './wire.ts'
37
+
38
+ // Client-safe pieces re-exported from their own module — see the module doc
39
+ // above for why they must not live in this host-only file.
40
+ export { allowHostNotice, allowHostWorkspaces } from './allow-host-notice.ts'
41
+ export type { AllowHostNotice, AllowHostNoticeKey } from './allow-host-notice.ts'
31
42
 
32
43
  /**
33
44
  * Why a generated rule may legitimately live in a rule file the parser would
@@ -251,57 +262,3 @@ function samePath(a: string, b: string): boolean {
251
262
  const right = resolve(b)
252
263
  return process.platform === 'win32' ? left.toLowerCase() === right.toLowerCase() : left === right
253
264
  }
254
-
255
- /**
256
- * The workspaces a host-level block may be allowed in: the distinct non-null
257
- * `cwd`s of the loaded rule sources.
258
- *
259
- * A block with no cwd was judged by the proxy's session-less host chain, which
260
- * any loaded workspace chain outranks the moment one exists — so writing the
261
- * host file can leave the connection blocked while the rule sits in a file
262
- * nothing consults. The settings page therefore offers these workspaces and
263
- * sends the chosen one as the request's cwd; when there is exactly one (or
264
- * none), there is nothing to choose between.
265
- * @param sources - the snapshot's rule sources.
266
- * @returns the distinct workspace roots, in source order.
267
- */
268
- export function allowHostWorkspaces(sources: readonly RuleSourceView[]): string[] {
269
- const seen = new Set<string>()
270
- const workspaces: string[] = []
271
- for (const source of sources) {
272
- if (source.cwd === null || seen.has(source.cwd)) continue
273
- seen.add(source.cwd)
274
- workspaces.push(source.cwd)
275
- }
276
- return workspaces
277
- }
278
-
279
- /** The locale keys the allow action reports through. */
280
- export type AllowHostNoticeKey = 'allowHostSaved' | 'allowHostAlready' | 'allowHostStillBlocked' | 'allowHostFailed'
281
-
282
- /** The notice one action result maps onto: a locale key plus its interpolation values. */
283
- export interface AllowHostNotice {
284
- readonly ok: boolean
285
- readonly key: AllowHostNoticeKey
286
- readonly vars: Record<string, string | number>
287
- }
288
-
289
- /**
290
- * Map an action result onto the page notice.
291
- *
292
- * A non-`allow` outcome outranks `alreadyAllowed`: the whole point of
293
- * returning the RECOMPUTED decision is that the page can never say "allowed"
294
- * while the connection is still blocked (a nearer chain with an earlier deny,
295
- * or a mode default, still wins). `alreadyAllowed` therefore only ever reads
296
- * as success when the decision really is `allow`.
297
- * @param result - the `permissionRules/allowHost` result.
298
- * @returns the notice to render.
299
- */
300
- export function allowHostNotice(result: AllowHostResult): AllowHostNotice {
301
- if (!result.ok) return { ok: false, key: 'allowHostFailed', vars: { error: result.error ?? 'unknown error' } }
302
- if (result.outcome !== 'allow') {
303
- return { ok: false, key: 'allowHostStillBlocked', vars: { outcome: result.outcome ?? 'unknown' } }
304
- }
305
- if (result.alreadyAllowed) return { ok: true, key: 'allowHostAlready', vars: {} }
306
- return { ok: true, key: 'allowHostSaved', vars: { path: result.path ?? '', reloaded: result.reloaded } }
307
- }
@@ -11,7 +11,7 @@
11
11
 
12
12
  import * as React from 'react'
13
13
  import type { ReactElement } from 'react'
14
- import { allowHostNotice, allowHostWorkspaces } from '../allow-host.ts'
14
+ import { allowHostNotice, allowHostWorkspaces } from '../allow-host-notice.ts'
15
15
  import type { PermissionRulesLocaleKey } from './locales.ts'
16
16
  import type { AllowHostRequest, AllowHostResult, NetworkBlockView, PermissionRulesSnapshot, RulesReadResult, RulesReloadResult, RulesSaveResult } from '../wire.ts'
17
17
 
package/src/proxy.ts CHANGED
@@ -342,12 +342,21 @@ export class NetworkProxy {
342
342
  // The forwarded connection is pinned to the addresses the decision was
343
343
  // made on, so a hostname that re-resolves between adjudication and
344
344
  // connect (DNS rebinding) cannot reach an address the rules never
345
- // approved.
345
+ // approved. An allow decision with NO adjudicated address (the name
346
+ // failed to resolve during adjudication) fails closed here — issue #23:
347
+ // dialing the name would be a second resolution whose answer the rules
348
+ // never saw, the same invariant connectUpstream enforces for CONNECT.
346
349
  const pinned = pinnedAddresses(adjudicated)
350
+ if (pinned.length === 0) {
351
+ this.options.logger.warn(`permission-rules: plain-HTTP upstream refused: no adjudicated address for ${adjudicated.host}`)
352
+ res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' })
353
+ res.end(`[network: upstream error] no adjudicated address for ${adjudicated.host}\n`)
354
+ return
355
+ }
347
356
  const options: RequestOptions = {
348
357
  method: req.method,
349
358
  headers: req.headers,
350
- ...(pinned.length === 0 ? {} : { lookup: pinnedLookup(pinned) }),
359
+ lookup: pinnedLookup(pinned),
351
360
  }
352
361
  proxyReq = (upstream.protocol === 'https:' ? httpsRequest : httpRequest)(upstream, options, onResponse)
353
362
  } else {
package/src/runtime.ts CHANGED
@@ -1653,6 +1653,14 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
1653
1653
  return next()
1654
1654
  })
1655
1655
  attachSettingsSection(ctx, runtime, config)
1656
+ // Register before the first await (A02 T10): an uninstall landing while
1657
+ // attachNetworkProxy() is pending would otherwise lose the /rules command.
1658
+ ctx.commands.register({
1659
+ name: 'rules',
1660
+ description: 'list, reload, audit, dry-test, or inspect the network policy of the active permission rules for this workspace',
1661
+ input: { hint: '[list | reload | network | decisions [n] | test [--cwd <dir>] [--env K=V] [--agent <sel>] [--platform <name>] <tool> <json-args>]' },
1662
+ handler: invocation => runtime.command(invocation),
1663
+ })
1656
1664
  if (resolved.network.enabled) await runtime.attachNetworkProxy()
1657
1665
  ctx.inject(['systemPrompt'], (scope) => {
1658
1666
  const systemPrompt = scope.get('systemPrompt') as { context?: (entry: { name: string; order?: number; text: string }) => void } | undefined
@@ -1664,12 +1672,6 @@ export async function apply(ctx: Context, config: Config): Promise<void> {
1664
1672
  })
1665
1673
  })
1666
1674
  await ctx.plugin(PermissionRulesRemoteService, { runtime })
1667
- ctx.commands.register({
1668
- name: 'rules',
1669
- description: 'list, reload, audit, dry-test, or inspect the network policy of the active permission rules for this workspace',
1670
- input: { hint: '[list | reload | network | decisions [n] | test [--cwd <dir>] [--env K=V] [--agent <sel>] [--platform <name>] <tool> <json-args>]' },
1671
- handler: invocation => runtime.command(invocation),
1672
- })
1673
1675
  }
1674
1676
 
1675
1677
  /**
package/src/wire.ts CHANGED
@@ -11,6 +11,17 @@
11
11
  import { z } from 'zod'
12
12
  import type { InvocationDescriptor } from '@deepseek-ai/dsh-typert-protocol'
13
13
 
14
+ /**
15
+ * Strict wire codec carrying BOTH published and checkout faces: the `schema`
16
+ * field feeds the npm-published 0.1.5-rc.2 line, while `create` feeds the
17
+ * 0.1.6-alpha.2+ line, whose typert-loader requires the factory on every
18
+ * codec and materializes schemas lazily on first use. Built through a
19
+ * variable, so neither typecheck ruler flags the other face's field as excess.
20
+ */
21
+ function strictWire<T>(typeSymbol: string, schema: T) {
22
+ return Object.freeze({ ...{ mode: 'strict' as const, typeSymbol, schema }, create: () => schema })
23
+ }
24
+
14
25
  /** The three network policy modes (wire vocabulary). */
15
26
  export type NetworkModeWire = 'deny-all' | 'whitelist' | 'allow-all'
16
27
 
@@ -230,11 +241,7 @@ export const NETWORK_STATUS_DESCRIPTOR = Object.freeze({
230
241
  method: 'networkStatus',
231
242
  invocation: Object.freeze({ kind: 'direct' }),
232
243
  parameters: Object.freeze([]),
233
- result: Object.freeze({
234
- mode: 'strict',
235
- typeSymbol: 'dsh-permission-rules/types#PermissionRulesSnapshot',
236
- schema: PERMISSION_RULES_SNAPSHOT_SCHEMA,
237
- }),
244
+ result: strictWire('dsh-permission-rules/types#PermissionRulesSnapshot', PERMISSION_RULES_SNAPSHOT_SCHEMA),
238
245
  sourceLocation: Object.freeze({ file: 'src/wire.ts', line: 1, column: 1 }),
239
246
  } as const) satisfies InvocationDescriptor
240
247
 
@@ -249,17 +256,9 @@ export const RULES_READ_DESCRIPTOR = Object.freeze({
249
256
  name: 'path',
250
257
  wire: 'path',
251
258
  source: 'json',
252
- codec: Object.freeze({
253
- mode: 'strict',
254
- typeSymbol: 'dsh-permission-rules/types#RulesReadRequestPath',
255
- schema: z.string(),
256
- }),
259
+ codec: strictWire('dsh-permission-rules/types#RulesReadRequestPath', z.string()),
257
260
  } satisfies InvocationDescriptor['parameters'][number])]),
258
- result: Object.freeze({
259
- mode: 'strict',
260
- typeSymbol: 'dsh-permission-rules/types#RulesReadResult',
261
- schema: RULES_READ_SCHEMA,
262
- }),
261
+ result: strictWire('dsh-permission-rules/types#RulesReadResult', RULES_READ_SCHEMA),
263
262
  sourceLocation: Object.freeze({ file: 'src/wire.ts', line: 1, column: 1 }),
264
263
  } as const) satisfies InvocationDescriptor
265
264
 
@@ -275,28 +274,16 @@ export const RULES_SAVE_DESCRIPTOR = Object.freeze({
275
274
  name: 'path',
276
275
  wire: 'path',
277
276
  source: 'json',
278
- codec: Object.freeze({
279
- mode: 'strict',
280
- typeSymbol: 'dsh-permission-rules/types#RulesSaveRequestPath',
281
- schema: z.string(),
282
- }),
277
+ codec: strictWire('dsh-permission-rules/types#RulesSaveRequestPath', z.string()),
283
278
  } satisfies InvocationDescriptor['parameters'][number]),
284
279
  Object.freeze({
285
280
  name: 'text',
286
281
  wire: 'text',
287
282
  source: 'json',
288
- codec: Object.freeze({
289
- mode: 'strict',
290
- typeSymbol: 'dsh-permission-rules/types#RulesSaveRequestText',
291
- schema: z.string(),
292
- }),
283
+ codec: strictWire('dsh-permission-rules/types#RulesSaveRequestText', z.string()),
293
284
  } satisfies InvocationDescriptor['parameters'][number]),
294
285
  ]),
295
- result: Object.freeze({
296
- mode: 'strict',
297
- typeSymbol: 'dsh-permission-rules/types#RulesSaveResult',
298
- schema: RULES_SAVE_SCHEMA,
299
- }),
286
+ result: strictWire('dsh-permission-rules/types#RulesSaveResult', RULES_SAVE_SCHEMA),
300
287
  sourceLocation: Object.freeze({ file: 'src/wire.ts', line: 1, column: 1 }),
301
288
  } as const) satisfies InvocationDescriptor
302
289
 
@@ -308,11 +295,7 @@ export const RULES_RELOAD_DESCRIPTOR = Object.freeze({
308
295
  method: 'reload',
309
296
  invocation: Object.freeze({ kind: 'direct' }),
310
297
  parameters: Object.freeze([]),
311
- result: Object.freeze({
312
- mode: 'strict',
313
- typeSymbol: 'dsh-permission-rules/types#RulesReloadResult',
314
- schema: RULES_RELOAD_SCHEMA,
315
- }),
298
+ result: strictWire('dsh-permission-rules/types#RulesReloadResult', RULES_RELOAD_SCHEMA),
316
299
  sourceLocation: Object.freeze({ file: 'src/wire.ts', line: 1, column: 1 }),
317
300
  } as const) satisfies InvocationDescriptor
318
301
 
@@ -330,17 +313,9 @@ export const ALLOW_HOST_DESCRIPTOR = Object.freeze({
330
313
  name: 'request',
331
314
  wire: 'request',
332
315
  source: 'json',
333
- codec: Object.freeze({
334
- mode: 'strict',
335
- typeSymbol: 'dsh-permission-rules/types#AllowHostRequest',
336
- schema: ALLOW_HOST_REQUEST_SCHEMA,
337
- }),
316
+ codec: strictWire('dsh-permission-rules/types#AllowHostRequest', ALLOW_HOST_REQUEST_SCHEMA),
338
317
  } satisfies InvocationDescriptor['parameters'][number])]),
339
- result: Object.freeze({
340
- mode: 'strict',
341
- typeSymbol: 'dsh-permission-rules/types#AllowHostResult',
342
- schema: ALLOW_HOST_RESULT_SCHEMA,
343
- }),
318
+ result: strictWire('dsh-permission-rules/types#AllowHostResult', ALLOW_HOST_RESULT_SCHEMA),
344
319
  sourceLocation: Object.freeze({ file: 'src/wire.ts', line: 1, column: 1 }),
345
320
  } as const) satisfies InvocationDescriptor
346
321