@imbue-ai/detent 1.13.0 → 1.15.0

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.
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Data-driven verification that the tailscale-* builtin scopes cover exactly
3
+ * the Tailscale admin API's real endpoint surface, by driving Detent's real
4
+ * matcher over every operation in the Tailscale OpenAPI spec.
5
+ *
6
+ * The endpoint inventory is vendored from the spec (see
7
+ * `fixtures/tailscale-openapi-endpoints.json`); path parameters are expanded to
8
+ * sample values and prefixed with the spec's server base path `/api/v2/`.
9
+ *
10
+ * For each scope we compare the matcher's verdict over the full inventory to an
11
+ * independently computed intended set, so a side-effecting GET or a read-only
12
+ * write (the class of anomaly that would make a read/write split unsound) is
13
+ * caught as a mismatch. The granular scopes (users, devices, keys) are checked
14
+ * the same way, narrowed by path pattern. Mirrors the verification the ngrok
15
+ * scopes shipped with.
16
+ */
17
+ import { describe, it, expect } from 'vitest';
18
+ import { readFileSync } from 'node:fs';
19
+ import { fileURLToPath } from 'node:url';
20
+ import { dirname, join } from 'node:path';
21
+ import { SchemaRegistry, getAllBuiltinSchemas } from '../src/schemas/requestSchema.js';
22
+ const __filename = fileURLToPath(import.meta.url);
23
+ const __dirname = dirname(__filename);
24
+ const FIXTURE_PATH = join(__dirname, 'fixtures', 'tailscale-openapi-endpoints.json');
25
+ const endpoints = JSON.parse(readFileSync(FIXTURE_PATH, 'utf8'));
26
+ const registry = new SchemaRegistry(getAllBuiltinSchemas());
27
+ function request(method, path) {
28
+ return {
29
+ protocol: 'https',
30
+ domain: 'api.tailscale.com',
31
+ port: 443,
32
+ path,
33
+ method,
34
+ headers: {},
35
+ queryParams: {},
36
+ body: undefined,
37
+ };
38
+ }
39
+ const READ_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
40
+ const WRITE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
41
+ // Independently computed path patterns for the three granular areas. These are
42
+ // the same patterns the schemas declare, written here from scratch so a typo in
43
+ // the schema that narrows the wrong surface is caught as a mismatch.
44
+ const AREA_PATTERNS = {
45
+ users: /^\/api\/v2\/(users\/|user-invites\/|tailnet\/[^/]+\/users|tailnet\/[^/]+\/user-invites)/,
46
+ devices: /^\/api\/v2\/(device-invites\/|device\/|tailnet\/[^/]+\/devices|tailnet\/[^/]+\/device-attributes)/,
47
+ keys: /^\/api\/v2\/tailnet\/[^/]+\/keys/,
48
+ };
49
+ function verifyScopeMatchesExactly(scopeName, isIntended) {
50
+ const scope = registry.get(scopeName);
51
+ expect(scope, `scope ${scopeName} should exist`).toBeDefined();
52
+ for (const endpoint of endpoints) {
53
+ const intended = isIntended(endpoint);
54
+ const matched = scope.match(request(endpoint.method, endpoint.path));
55
+ expect(matched === intended, `${scopeName}: ${endpoint.method} ${endpoint.path} should ${intended ? 'match' : 'NOT match'}`).toBe(true);
56
+ }
57
+ }
58
+ describe('tailscale scopes vs the real Tailscale OpenAPI surface', () => {
59
+ it('the vendored inventory covers every operation exactly once', () => {
60
+ const seen = new Set();
61
+ for (const endpoint of endpoints) {
62
+ const key = `${endpoint.method} ${endpoint.path}`;
63
+ expect(seen.has(key), `duplicate endpoint ${key}`).toBe(false);
64
+ seen.add(key);
65
+ }
66
+ expect(endpoints.length).toBe(93);
67
+ });
68
+ it('tailscale-api matches every operation on api.tailscale.com', () => {
69
+ const scope = registry.get('tailscale-api');
70
+ expect(scope).toBeDefined();
71
+ for (const endpoint of endpoints) {
72
+ expect(scope.match(request(endpoint.method, endpoint.path)), `tailscale-api should match ${endpoint.method} ${endpoint.path}`).toBe(true);
73
+ }
74
+ });
75
+ it('tailscale-api rejects lookalike domains and the console/login hosts', () => {
76
+ const scope = registry.get('tailscale-api');
77
+ expect(scope.match({
78
+ ...request('GET', '/api/v2/tailnet/example.com/users'),
79
+ domain: 'api.tailscale.com.example.com',
80
+ })).toBe(false);
81
+ expect(scope.match({ ...request('GET', '/admin/users'), domain: 'console.tailscale.com' })).toBe(false);
82
+ expect(scope.match({ ...request('GET', '/admin'), domain: 'login.tailscale.com' })).toBe(false);
83
+ });
84
+ it('tailscale-read-all matches exactly the safe-method operations', () => {
85
+ verifyScopeMatchesExactly('tailscale-read-all', (e) => READ_METHODS.has(e.method));
86
+ });
87
+ it('tailscale-write-all matches exactly the unsafe-method operations', () => {
88
+ verifyScopeMatchesExactly('tailscale-write-all', (e) => WRITE_METHODS.has(e.method));
89
+ });
90
+ it('the read/write umbrella split is exhaustive (no side-effecting GETs, no read-only writes)', () => {
91
+ // The read and write scopes partition the surface with no remainder and no
92
+ // overlap: every operation is matched by exactly one of them.
93
+ const read = registry.get('tailscale-read-all');
94
+ const write = registry.get('tailscale-write-all');
95
+ for (const endpoint of endpoints) {
96
+ const isRead = read.match(request(endpoint.method, endpoint.path));
97
+ const isWrite = write.match(request(endpoint.method, endpoint.path));
98
+ expect(isRead === !isWrite, `${endpoint.method} ${endpoint.path} split between read and write`).toBe(true);
99
+ }
100
+ });
101
+ // The granular scopes narrow the surface by area (users, devices, keys),
102
+ // split read/write within each. Each is verified against its independently
103
+ // computed intended set over the full real endpoint inventory.
104
+ describe('granular scopes: users', () => {
105
+ it('tailscale-read-users matches exactly the safe-method user operations', () => {
106
+ verifyScopeMatchesExactly('tailscale-read-users', (e) => READ_METHODS.has(e.method) && AREA_PATTERNS.users.test(e.path));
107
+ });
108
+ it('tailscale-write-users matches exactly the unsafe-method user operations', () => {
109
+ verifyScopeMatchesExactly('tailscale-write-users', (e) => WRITE_METHODS.has(e.method) && AREA_PATTERNS.users.test(e.path));
110
+ });
111
+ });
112
+ describe('granular scopes: devices', () => {
113
+ it('tailscale-read-devices matches exactly the safe-method device operations', () => {
114
+ verifyScopeMatchesExactly('tailscale-read-devices', (e) => READ_METHODS.has(e.method) && AREA_PATTERNS.devices.test(e.path));
115
+ });
116
+ it('tailscale-write-devices matches exactly the unsafe-method device operations', () => {
117
+ verifyScopeMatchesExactly('tailscale-write-devices', (e) => WRITE_METHODS.has(e.method) && AREA_PATTERNS.devices.test(e.path));
118
+ });
119
+ });
120
+ describe('granular scopes: keys', () => {
121
+ it('tailscale-read-keys matches exactly the safe-method key operations', () => {
122
+ verifyScopeMatchesExactly('tailscale-read-keys', (e) => READ_METHODS.has(e.method) && AREA_PATTERNS.keys.test(e.path));
123
+ });
124
+ it('tailscale-write-keys matches exactly the unsafe-method key operations', () => {
125
+ verifyScopeMatchesExactly('tailscale-write-keys', (e) => WRITE_METHODS.has(e.method) && AREA_PATTERNS.keys.test(e.path));
126
+ });
127
+ });
128
+ it('the three granular areas partition the surface with no overlap', () => {
129
+ for (const endpoint of endpoints) {
130
+ const areas = [
131
+ AREA_PATTERNS.users.test(endpoint.path),
132
+ AREA_PATTERNS.devices.test(endpoint.path),
133
+ AREA_PATTERNS.keys.test(endpoint.path),
134
+ ];
135
+ const count = areas.filter(Boolean).length;
136
+ expect(count <= 1, `${endpoint.method} ${endpoint.path} matched ${String(count)} areas (should be 0 or 1)`).toBe(true);
137
+ }
138
+ });
139
+ });
140
+ //# sourceMappingURL=tailscaleOpenapiCoverage.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tailscaleOpenapiCoverage.test.js","sourceRoot":"","sources":["../../tests/tailscaleOpenapiCoverage.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvF,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,OAAO,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E;QACxE,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,MAAM,KAAK,GAAG;gBACZ,aAAa,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACvC,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACzC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;aACvC,CAAC;YACF,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC;YAC3C,MAAM,CACJ,KAAK,IAAI,CAAC,EACV,GAAG,QAAQ,CAAC,MAAM,IAAI,QAAQ,CAAC,IAAI,YAAY,MAAM,CAAC,KAAK,CAAC,2BAA2B,CACxF,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imbue-ai/detent",
3
- "version": "1.13.0",
3
+ "version": "1.15.0",
4
4
  "description": "Fine-grained HTTP permissions for AI agents.",
5
5
  "author": "Imbue <hynek@imbue.com>",
6
6
  "repository": {