@imbue-ai/detent 1.0.1
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/README.md +246 -0
- package/dist/src/builtinPatterns/any.json +3 -0
- package/dist/src/builtinPatterns/builtinPatterns/any.json +3 -0
- package/dist/src/builtinPatterns.d.ts +5 -0
- package/dist/src/builtinPatterns.d.ts.map +1 -0
- package/dist/src/builtinPatterns.js +50 -0
- package/dist/src/builtinPatterns.js.map +1 -0
- package/dist/src/check.d.ts +2 -0
- package/dist/src/check.d.ts.map +1 -0
- package/dist/src/check.js +8 -0
- package/dist/src/check.js.map +1 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +64 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config.d.ts +18 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/config.js +157 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/curl.d.ts +5 -0
- package/dist/src/curl.d.ts.map +1 -0
- package/dist/src/curl.js +394 -0
- package/dist/src/curl.js.map +1 -0
- package/dist/src/decomposedRequest.d.ts +24 -0
- package/dist/src/decomposedRequest.d.ts.map +1 -0
- package/dist/src/decomposedRequest.js +51 -0
- package/dist/src/decomposedRequest.js.map +1 -0
- package/dist/src/detentConfig.d.ts +15 -0
- package/dist/src/detentConfig.d.ts.map +1 -0
- package/dist/src/detentConfig.js +169 -0
- package/dist/src/detentConfig.js.map +1 -0
- package/dist/src/dump.d.ts +6 -0
- package/dist/src/dump.d.ts.map +1 -0
- package/dist/src/dump.js +14 -0
- package/dist/src/dump.js.map +1 -0
- package/dist/src/environment.d.ts +3 -0
- package/dist/src/environment.d.ts.map +1 -0
- package/dist/src/environment.js +15 -0
- package/dist/src/environment.js.map +1 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +7 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/patterns/builtin/any.json +3 -0
- package/dist/src/patterns/builtin/builtin/any.json +3 -0
- package/dist/src/patterns/builtin/builtin/aws.json +326 -0
- package/dist/src/patterns/builtin/builtin/calendly.json +274 -0
- package/dist/src/patterns/builtin/builtin/coolify.json +238 -0
- package/dist/src/patterns/builtin/builtin/discord.json +157 -0
- package/dist/src/patterns/builtin/builtin/dropbox.json +115 -0
- package/dist/src/patterns/builtin/builtin/figma.json +228 -0
- package/dist/src/patterns/builtin/builtin/github.json +248 -0
- package/dist/src/patterns/builtin/builtin/gitlab.json +262 -0
- package/dist/src/patterns/builtin/builtin/google-analytics.json +155 -0
- package/dist/src/patterns/builtin/builtin/google-calendar.json +227 -0
- package/dist/src/patterns/builtin/builtin/google-directions.json +67 -0
- package/dist/src/patterns/builtin/builtin/google-docs.json +102 -0
- package/dist/src/patterns/builtin/builtin/google-drive.json +162 -0
- package/dist/src/patterns/builtin/builtin/google-gmail.json +257 -0
- package/dist/src/patterns/builtin/builtin/google-people.json +142 -0
- package/dist/src/patterns/builtin/builtin/google-sheets.json +115 -0
- package/dist/src/patterns/builtin/builtin/linear.json +12 -0
- package/dist/src/patterns/builtin/builtin/mailchimp.json +263 -0
- package/dist/src/patterns/builtin/builtin/notion.json +230 -0
- package/dist/src/patterns/builtin/builtin/sentry.json +172 -0
- package/dist/src/patterns/builtin/builtin/slack.json +221 -0
- package/dist/src/patterns/builtin/builtin/stripe.json +347 -0
- package/dist/src/patterns/builtin/builtin/telegram.json +89 -0
- package/dist/src/patterns/builtin/builtin/umami.json +172 -0
- package/dist/src/patterns/builtin/builtin/yelp.json +147 -0
- package/dist/src/patterns/builtin/builtin/zoom.json +192 -0
- package/dist/src/patterns/requestPattern.d.ts +33 -0
- package/dist/src/patterns/requestPattern.d.ts.map +1 -0
- package/dist/src/patterns/requestPattern.js +157 -0
- package/dist/src/patterns/requestPattern.js.map +1 -0
- package/dist/src/requestPattern.d.ts +15 -0
- package/dist/src/requestPattern.d.ts.map +1 -0
- package/dist/src/requestPattern.js +35 -0
- package/dist/src/requestPattern.js.map +1 -0
- package/dist/tests/builtinPatterns.test.d.ts +2 -0
- package/dist/tests/builtinPatterns.test.d.ts.map +1 -0
- package/dist/tests/builtinPatterns.test.js +1293 -0
- package/dist/tests/builtinPatterns.test.js.map +1 -0
- package/dist/tests/check.test.d.ts +2 -0
- package/dist/tests/check.test.d.ts.map +1 -0
- package/dist/tests/check.test.js +671 -0
- package/dist/tests/check.test.js.map +1 -0
- package/dist/tests/cli.test.d.ts +2 -0
- package/dist/tests/cli.test.d.ts.map +1 -0
- package/dist/tests/cli.test.js +77 -0
- package/dist/tests/cli.test.js.map +1 -0
- package/dist/tests/codeQuality.test.d.ts +2 -0
- package/dist/tests/codeQuality.test.d.ts.map +1 -0
- package/dist/tests/codeQuality.test.js +15 -0
- package/dist/tests/codeQuality.test.js.map +1 -0
- package/dist/tests/curl.test.d.ts +2 -0
- package/dist/tests/curl.test.d.ts.map +1 -0
- package/dist/tests/curl.test.js +241 -0
- package/dist/tests/curl.test.js.map +1 -0
- package/dist/tests/dump.test.d.ts +2 -0
- package/dist/tests/dump.test.d.ts.map +1 -0
- package/dist/tests/dump.test.js +154 -0
- package/dist/tests/dump.test.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { writeFileSync, mkdirSync, rmSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import { check } from '../src/check.js';
|
|
6
|
+
import { Config, ConfigError } from '../src/config.js';
|
|
7
|
+
import { RequestPattern, RequestPatternError } from '../src/patterns/requestPattern.js';
|
|
8
|
+
import { decomposeRequest } from '../src/decomposedRequest.js';
|
|
9
|
+
describe('decomposeRequest', () => {
|
|
10
|
+
it('extracts all fields from a simple GET request', async () => {
|
|
11
|
+
const request = new Request('https://api.example.com:8080/users?page=1', {
|
|
12
|
+
method: 'GET',
|
|
13
|
+
headers: { Authorization: 'Bearer token123' },
|
|
14
|
+
});
|
|
15
|
+
const data = await decomposeRequest(request);
|
|
16
|
+
expect(data.protocol).toBe('https');
|
|
17
|
+
expect(data.domain).toBe('api.example.com');
|
|
18
|
+
expect(data.port).toBe(8080);
|
|
19
|
+
expect(data.path).toBe('/users');
|
|
20
|
+
expect(data.method).toBe('GET');
|
|
21
|
+
expect(data.headers).toHaveProperty('authorization', 'Bearer token123');
|
|
22
|
+
expect(data.queryParams).toEqual({ page: '1' });
|
|
23
|
+
expect(data.body).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
it('uses default port 443 for https', async () => {
|
|
26
|
+
const request = new Request('https://example.com/path');
|
|
27
|
+
const data = await decomposeRequest(request);
|
|
28
|
+
expect(data.port).toBe(443);
|
|
29
|
+
});
|
|
30
|
+
it('uses default port 80 for http', async () => {
|
|
31
|
+
const request = new Request('http://example.com/path');
|
|
32
|
+
const data = await decomposeRequest(request);
|
|
33
|
+
expect(data.port).toBe(80);
|
|
34
|
+
});
|
|
35
|
+
it('extracts body from POST request', async () => {
|
|
36
|
+
const request = new Request('https://example.com/api', {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
body: '{"name":"alice"}',
|
|
39
|
+
});
|
|
40
|
+
const data = await decomposeRequest(request);
|
|
41
|
+
expect(data.body).toBe('{"name":"alice"}');
|
|
42
|
+
expect(data.method).toBe('POST');
|
|
43
|
+
});
|
|
44
|
+
it('uppercases the method even when the Request object does not normalize it', async () => {
|
|
45
|
+
// The Fetch API only auto-uppercases the six standard methods
|
|
46
|
+
// (DELETE, GET, HEAD, OPTIONS, POST, PUT). Others like PATCH
|
|
47
|
+
// are left as-is. decomposeRequest must uppercase all methods.
|
|
48
|
+
// We simulate this by overriding the method property to avoid
|
|
49
|
+
// an undici warning when constructing a Request with lowercase 'patch'.
|
|
50
|
+
const request = new Request('https://example.com/api', { method: 'PATCH' });
|
|
51
|
+
Object.defineProperty(request, 'method', { value: 'patch' });
|
|
52
|
+
// Confirm the Request object has the lowercase method
|
|
53
|
+
expect(request.method).toBe('patch');
|
|
54
|
+
const data = await decomposeRequest(request);
|
|
55
|
+
expect(data.method).toBe('PATCH');
|
|
56
|
+
});
|
|
57
|
+
it('lowercases header keys', async () => {
|
|
58
|
+
const request = new Request('https://example.com', {
|
|
59
|
+
headers: { 'X-Custom-Header': 'value', Authorization: 'Bearer tok' },
|
|
60
|
+
});
|
|
61
|
+
const data = await decomposeRequest(request);
|
|
62
|
+
expect(Object.keys(data.headers).every((k) => k === k.toLowerCase())).toBe(true);
|
|
63
|
+
expect(data.headers).toHaveProperty('x-custom-header', 'value');
|
|
64
|
+
expect(data.headers).toHaveProperty('authorization', 'Bearer tok');
|
|
65
|
+
});
|
|
66
|
+
it('lowercases protocol and domain', async () => {
|
|
67
|
+
// URL constructor already normalizes these, but verify the contract
|
|
68
|
+
const request = new Request('HTTPS://API.GITHUB.COM/path');
|
|
69
|
+
const data = await decomposeRequest(request);
|
|
70
|
+
expect(data.protocol).toBe('https');
|
|
71
|
+
expect(data.domain).toBe('api.github.com');
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
describe('RequestPattern', () => {
|
|
75
|
+
it('matches a request with const method', () => {
|
|
76
|
+
const pattern = new RequestPattern('get-only', {
|
|
77
|
+
properties: { method: { const: 'GET' } },
|
|
78
|
+
required: ['method'],
|
|
79
|
+
});
|
|
80
|
+
const data = {
|
|
81
|
+
protocol: 'https',
|
|
82
|
+
domain: 'example.com',
|
|
83
|
+
port: 443,
|
|
84
|
+
path: '/test',
|
|
85
|
+
method: 'GET',
|
|
86
|
+
headers: {},
|
|
87
|
+
queryParams: {},
|
|
88
|
+
body: undefined,
|
|
89
|
+
};
|
|
90
|
+
expect(pattern.match(data)).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
it('rejects a request that does not match', () => {
|
|
93
|
+
const pattern = new RequestPattern('get-only', {
|
|
94
|
+
properties: { method: { const: 'GET' } },
|
|
95
|
+
required: ['method'],
|
|
96
|
+
});
|
|
97
|
+
const data = {
|
|
98
|
+
protocol: 'https',
|
|
99
|
+
domain: 'example.com',
|
|
100
|
+
port: 443,
|
|
101
|
+
path: '/test',
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: {},
|
|
104
|
+
queryParams: {},
|
|
105
|
+
body: undefined,
|
|
106
|
+
};
|
|
107
|
+
expect(pattern.match(data)).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
it('matches with domain pattern', () => {
|
|
110
|
+
const pattern = new RequestPattern('github-api', {
|
|
111
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
112
|
+
required: ['domain'],
|
|
113
|
+
});
|
|
114
|
+
const data = {
|
|
115
|
+
protocol: 'https',
|
|
116
|
+
domain: 'api.github.com',
|
|
117
|
+
port: 443,
|
|
118
|
+
path: '/repos',
|
|
119
|
+
method: 'GET',
|
|
120
|
+
headers: {},
|
|
121
|
+
queryParams: {},
|
|
122
|
+
body: undefined,
|
|
123
|
+
};
|
|
124
|
+
expect(pattern.match(data)).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
it('matches with path regex pattern', () => {
|
|
127
|
+
const pattern = new RequestPattern('issues-path', {
|
|
128
|
+
properties: {
|
|
129
|
+
path: { type: 'string', pattern: '^/repos/[^/]+/[^/]+/issues(/[0-9]+)?$' },
|
|
130
|
+
},
|
|
131
|
+
required: ['path'],
|
|
132
|
+
});
|
|
133
|
+
const matching = {
|
|
134
|
+
protocol: 'https',
|
|
135
|
+
domain: 'api.github.com',
|
|
136
|
+
port: 443,
|
|
137
|
+
path: '/repos/octocat/Hello-World/issues/42',
|
|
138
|
+
method: 'GET',
|
|
139
|
+
headers: {},
|
|
140
|
+
queryParams: {},
|
|
141
|
+
body: undefined,
|
|
142
|
+
};
|
|
143
|
+
const nonMatching = {
|
|
144
|
+
...matching,
|
|
145
|
+
path: '/repos/octocat/Hello-World/pulls',
|
|
146
|
+
};
|
|
147
|
+
expect(pattern.match(matching)).toBe(true);
|
|
148
|
+
expect(pattern.match(nonMatching)).toBe(false);
|
|
149
|
+
});
|
|
150
|
+
it('empty schema matches everything', () => {
|
|
151
|
+
const pattern = new RequestPattern('any', {});
|
|
152
|
+
const data = {
|
|
153
|
+
protocol: 'https',
|
|
154
|
+
domain: 'anything.com',
|
|
155
|
+
port: 443,
|
|
156
|
+
path: '/whatever',
|
|
157
|
+
method: 'DELETE',
|
|
158
|
+
headers: {},
|
|
159
|
+
queryParams: {},
|
|
160
|
+
body: undefined,
|
|
161
|
+
};
|
|
162
|
+
expect(pattern.match(data)).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
it('throws RequestPatternError for unknown request property name', () => {
|
|
165
|
+
expect(() => new RequestPattern('bad', {
|
|
166
|
+
properties: { methd: { const: 'GET' } },
|
|
167
|
+
required: ['methd'],
|
|
168
|
+
})).toThrow(RequestPatternError);
|
|
169
|
+
expect(() => new RequestPattern('bad', {
|
|
170
|
+
properties: { methd: { const: 'GET' } },
|
|
171
|
+
required: ['methd'],
|
|
172
|
+
})).toThrow(/unknown request property "methd"/);
|
|
173
|
+
});
|
|
174
|
+
it('throws RequestPatternError for unknown property name inside anyOf', () => {
|
|
175
|
+
expect(() => new RequestPattern('bad', {
|
|
176
|
+
anyOf: [
|
|
177
|
+
{ properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
178
|
+
{ properties: { pth: { const: '/foo' } }, required: ['pth'] },
|
|
179
|
+
],
|
|
180
|
+
})).toThrow(/unknown request property "pth"/);
|
|
181
|
+
});
|
|
182
|
+
it('accepts all valid DecomposedRequest property names', () => {
|
|
183
|
+
expect(() => new RequestPattern('all-fields', {
|
|
184
|
+
properties: {
|
|
185
|
+
protocol: { const: 'https' },
|
|
186
|
+
domain: { const: 'example.com' },
|
|
187
|
+
port: { const: 443 },
|
|
188
|
+
path: { const: '/test' },
|
|
189
|
+
method: { const: 'GET' },
|
|
190
|
+
headers: { type: 'object' },
|
|
191
|
+
queryParams: { type: 'object' },
|
|
192
|
+
body: { type: 'string' },
|
|
193
|
+
},
|
|
194
|
+
required: ['protocol', 'domain', 'port', 'path', 'method', 'headers', 'queryParams'],
|
|
195
|
+
})).not.toThrow();
|
|
196
|
+
});
|
|
197
|
+
it('supports anyOf at the schema level', () => {
|
|
198
|
+
const pattern = new RequestPattern('read-or-search', {
|
|
199
|
+
anyOf: [
|
|
200
|
+
{ properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
201
|
+
{
|
|
202
|
+
properties: {
|
|
203
|
+
method: { const: 'POST' },
|
|
204
|
+
path: { type: 'string', pattern: '^/search$' },
|
|
205
|
+
},
|
|
206
|
+
required: ['method', 'path'],
|
|
207
|
+
},
|
|
208
|
+
],
|
|
209
|
+
});
|
|
210
|
+
const getData = {
|
|
211
|
+
protocol: 'https',
|
|
212
|
+
domain: 'example.com',
|
|
213
|
+
port: 443,
|
|
214
|
+
path: '/anything',
|
|
215
|
+
method: 'GET',
|
|
216
|
+
headers: {},
|
|
217
|
+
queryParams: {},
|
|
218
|
+
body: undefined,
|
|
219
|
+
};
|
|
220
|
+
const postSearchData = {
|
|
221
|
+
...getData,
|
|
222
|
+
method: 'POST',
|
|
223
|
+
path: '/search',
|
|
224
|
+
};
|
|
225
|
+
const postOtherData = {
|
|
226
|
+
...getData,
|
|
227
|
+
method: 'POST',
|
|
228
|
+
path: '/pages',
|
|
229
|
+
};
|
|
230
|
+
expect(pattern.match(getData)).toBe(true);
|
|
231
|
+
expect(pattern.match(postSearchData)).toBe(true);
|
|
232
|
+
expect(pattern.match(postOtherData)).toBe(false);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
describe('Config', () => {
|
|
236
|
+
let tempDir;
|
|
237
|
+
beforeEach(() => {
|
|
238
|
+
tempDir = join(tmpdir(), `detent-test-${String(Date.now())}-${Math.random().toString(36).slice(2)}`);
|
|
239
|
+
mkdirSync(tempDir, { recursive: true });
|
|
240
|
+
});
|
|
241
|
+
afterEach(() => {
|
|
242
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
243
|
+
});
|
|
244
|
+
function writeConfig(config) {
|
|
245
|
+
const configPath = join(tempDir, 'config.json');
|
|
246
|
+
writeFileSync(configPath, JSON.stringify(config));
|
|
247
|
+
return configPath;
|
|
248
|
+
}
|
|
249
|
+
it('rejects all requests when no rules are defined', async () => {
|
|
250
|
+
const configPath = writeConfig({ patterns: {}, rules: [] });
|
|
251
|
+
const config = new Config(configPath, true);
|
|
252
|
+
const request = new Request('https://example.com');
|
|
253
|
+
expect(await config.check(request)).toBe(false);
|
|
254
|
+
});
|
|
255
|
+
it('allows a request that matches a rule scope and permission', async () => {
|
|
256
|
+
const configPath = writeConfig({
|
|
257
|
+
patterns: {
|
|
258
|
+
'github-api': {
|
|
259
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
260
|
+
required: ['domain'],
|
|
261
|
+
},
|
|
262
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
263
|
+
},
|
|
264
|
+
rules: [{ 'github-api': ['get-only'] }],
|
|
265
|
+
});
|
|
266
|
+
const config = new Config(configPath, true);
|
|
267
|
+
const request = new Request('https://api.github.com/repos');
|
|
268
|
+
expect(await config.check(request)).toBe(true);
|
|
269
|
+
});
|
|
270
|
+
it('rejects a request that matches scope but not any permission', async () => {
|
|
271
|
+
const configPath = writeConfig({
|
|
272
|
+
patterns: {
|
|
273
|
+
'github-api': {
|
|
274
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
275
|
+
required: ['domain'],
|
|
276
|
+
},
|
|
277
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
278
|
+
},
|
|
279
|
+
rules: [{ 'github-api': ['get-only'] }],
|
|
280
|
+
});
|
|
281
|
+
const config = new Config(configPath, true);
|
|
282
|
+
const request = new Request('https://api.github.com/repos', { method: 'DELETE' });
|
|
283
|
+
expect(await config.check(request)).toBe(false);
|
|
284
|
+
});
|
|
285
|
+
it('rejects a request that does not match any rule scope', async () => {
|
|
286
|
+
const configPath = writeConfig({
|
|
287
|
+
patterns: {
|
|
288
|
+
'github-api': {
|
|
289
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
290
|
+
required: ['domain'],
|
|
291
|
+
},
|
|
292
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
293
|
+
},
|
|
294
|
+
rules: [{ 'github-api': ['get-only'] }],
|
|
295
|
+
});
|
|
296
|
+
const config = new Config(configPath, true);
|
|
297
|
+
const request = new Request('https://slack.com/api/chat.postMessage');
|
|
298
|
+
expect(await config.check(request)).toBe(false);
|
|
299
|
+
});
|
|
300
|
+
it('stops at first matching scope rule', async () => {
|
|
301
|
+
const configPath = writeConfig({
|
|
302
|
+
patterns: {
|
|
303
|
+
'all-https': { properties: { protocol: { const: 'https' } }, required: ['protocol'] },
|
|
304
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
305
|
+
'any-method': {},
|
|
306
|
+
},
|
|
307
|
+
rules: [{ 'all-https': ['get-only'] }, { 'all-https': ['any-method'] }],
|
|
308
|
+
});
|
|
309
|
+
const config = new Config(configPath, true);
|
|
310
|
+
// POST should be rejected by first rule, second rule never evaluated
|
|
311
|
+
const request = new Request('https://example.com', { method: 'POST' });
|
|
312
|
+
expect(await config.check(request)).toBe(false);
|
|
313
|
+
});
|
|
314
|
+
it('throws ConfigError when permissions value is a string instead of an array', () => {
|
|
315
|
+
const configPath = writeConfig({
|
|
316
|
+
patterns: {
|
|
317
|
+
'github-api': {
|
|
318
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
319
|
+
required: ['domain'],
|
|
320
|
+
},
|
|
321
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
322
|
+
},
|
|
323
|
+
rules: [{ 'github-api': 'get-only' }],
|
|
324
|
+
});
|
|
325
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
326
|
+
});
|
|
327
|
+
it('treats missing patterns as implicitly empty', async () => {
|
|
328
|
+
const configPath = writeConfig({
|
|
329
|
+
rules: [],
|
|
330
|
+
});
|
|
331
|
+
const config = new Config(configPath, true);
|
|
332
|
+
const request = new Request('https://example.com');
|
|
333
|
+
expect(await config.check(request)).toBe(false);
|
|
334
|
+
});
|
|
335
|
+
it('treats missing rules as implicitly empty', async () => {
|
|
336
|
+
const configPath = writeConfig({
|
|
337
|
+
patterns: {
|
|
338
|
+
scope: { properties: { domain: { const: 'example.com' } }, required: ['domain'] },
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
const config = new Config(configPath, true);
|
|
342
|
+
const request = new Request('https://example.com');
|
|
343
|
+
expect(await config.check(request)).toBe(false);
|
|
344
|
+
});
|
|
345
|
+
it('treats completely empty object as valid config', async () => {
|
|
346
|
+
const configPath = writeConfig({});
|
|
347
|
+
const config = new Config(configPath, true);
|
|
348
|
+
const request = new Request('https://example.com');
|
|
349
|
+
expect(await config.check(request)).toBe(false);
|
|
350
|
+
});
|
|
351
|
+
it('throws ConfigError for unknown top-level keys', () => {
|
|
352
|
+
const configPath = writeConfig({
|
|
353
|
+
patterns: {},
|
|
354
|
+
rules: [],
|
|
355
|
+
unknown: 'value',
|
|
356
|
+
});
|
|
357
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
358
|
+
});
|
|
359
|
+
it('throws ConfigError when rules is not an array', () => {
|
|
360
|
+
const configPath = writeConfig({
|
|
361
|
+
rules: 'not-an-array',
|
|
362
|
+
});
|
|
363
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
364
|
+
});
|
|
365
|
+
it('throws ConfigError when patterns is not an object', () => {
|
|
366
|
+
const configPath = writeConfig({
|
|
367
|
+
patterns: 'not-an-object',
|
|
368
|
+
});
|
|
369
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
370
|
+
});
|
|
371
|
+
it('throws ConfigError when include contains non-strings', () => {
|
|
372
|
+
const configPath = writeConfig({
|
|
373
|
+
include: [42],
|
|
374
|
+
});
|
|
375
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
376
|
+
});
|
|
377
|
+
it('throws ConfigError when config is an array instead of an object', () => {
|
|
378
|
+
const configPath = join(tempDir, 'config.json');
|
|
379
|
+
writeFileSync(configPath, JSON.stringify([1, 2, 3]));
|
|
380
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
381
|
+
});
|
|
382
|
+
it('validates included config files as well', () => {
|
|
383
|
+
const includedPath = join(tempDir, 'included.json');
|
|
384
|
+
writeFileSync(includedPath, JSON.stringify({ rules: 'not-an-array' }));
|
|
385
|
+
const configPath = writeConfig({
|
|
386
|
+
include: ['included.json'],
|
|
387
|
+
});
|
|
388
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
389
|
+
});
|
|
390
|
+
it('throws ConfigError when a pattern is not an object', () => {
|
|
391
|
+
const configPath = writeConfig({
|
|
392
|
+
patterns: {
|
|
393
|
+
'bad-pattern': 'not-an-object',
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
397
|
+
});
|
|
398
|
+
it('treats missing patterns and rules in included files as implicitly empty', async () => {
|
|
399
|
+
const includedPath = join(tempDir, 'included.json');
|
|
400
|
+
writeFileSync(includedPath, JSON.stringify({}));
|
|
401
|
+
const configPath = writeConfig({
|
|
402
|
+
include: ['included.json'],
|
|
403
|
+
patterns: {
|
|
404
|
+
scope: { properties: { domain: { const: 'example.com' } }, required: ['domain'] },
|
|
405
|
+
permission: { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
406
|
+
},
|
|
407
|
+
rules: [{ scope: ['permission'] }],
|
|
408
|
+
});
|
|
409
|
+
const config = new Config(configPath, true);
|
|
410
|
+
const request = new Request('https://example.com');
|
|
411
|
+
expect(await config.check(request)).toBe(true);
|
|
412
|
+
});
|
|
413
|
+
it('treats missing config file as implicitly empty and rejects all requests', async () => {
|
|
414
|
+
const config = new Config('/nonexistent/path.json', true);
|
|
415
|
+
const request = new Request('https://example.com');
|
|
416
|
+
expect(await config.check(request)).toBe(false);
|
|
417
|
+
});
|
|
418
|
+
it('throws ConfigError for invalid JSON', () => {
|
|
419
|
+
const configPath = join(tempDir, 'bad.json');
|
|
420
|
+
writeFileSync(configPath, 'not json');
|
|
421
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
422
|
+
});
|
|
423
|
+
it('throws RequestPatternError for unknown pattern name in rule scope', () => {
|
|
424
|
+
const configPath = writeConfig({
|
|
425
|
+
patterns: {},
|
|
426
|
+
rules: [{ 'unknown-scope': ['also-unknown'] }],
|
|
427
|
+
});
|
|
428
|
+
expect(() => new Config(configPath, true)).toThrow(RequestPatternError);
|
|
429
|
+
});
|
|
430
|
+
it('throws RequestPatternError for unknown pattern name in permissions', () => {
|
|
431
|
+
const configPath = writeConfig({
|
|
432
|
+
patterns: {
|
|
433
|
+
'github-api': {
|
|
434
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
435
|
+
required: ['domain'],
|
|
436
|
+
},
|
|
437
|
+
},
|
|
438
|
+
rules: [{ 'github-api': ['nonexistent-permission'] }],
|
|
439
|
+
});
|
|
440
|
+
expect(() => new Config(configPath, true)).toThrow(RequestPatternError);
|
|
441
|
+
});
|
|
442
|
+
it('throws ConfigError for rule with multiple keys', () => {
|
|
443
|
+
const configPath = writeConfig({
|
|
444
|
+
patterns: {
|
|
445
|
+
a: { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
446
|
+
b: { properties: { method: { const: 'POST' } }, required: ['method'] },
|
|
447
|
+
},
|
|
448
|
+
rules: [{ a: ['b'], b: ['a'] }],
|
|
449
|
+
});
|
|
450
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
451
|
+
});
|
|
452
|
+
it('supports multiple permissions in a single rule', async () => {
|
|
453
|
+
const configPath = writeConfig({
|
|
454
|
+
patterns: {
|
|
455
|
+
'github-api': {
|
|
456
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
457
|
+
required: ['domain'],
|
|
458
|
+
},
|
|
459
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
460
|
+
'post-only': { properties: { method: { const: 'POST' } }, required: ['method'] },
|
|
461
|
+
},
|
|
462
|
+
rules: [{ 'github-api': ['get-only', 'post-only'] }],
|
|
463
|
+
});
|
|
464
|
+
const config = new Config(configPath, true);
|
|
465
|
+
const getRequest = new Request('https://api.github.com/repos');
|
|
466
|
+
expect(await config.check(getRequest)).toBe(true);
|
|
467
|
+
const postRequest = new Request('https://api.github.com/repos', { method: 'POST' });
|
|
468
|
+
expect(await config.check(postRequest)).toBe(true);
|
|
469
|
+
const deleteRequest = new Request('https://api.github.com/repos', { method: 'DELETE' });
|
|
470
|
+
expect(await config.check(deleteRequest)).toBe(false);
|
|
471
|
+
});
|
|
472
|
+
it('merges patterns and rules from included config files', async () => {
|
|
473
|
+
const includedPath = join(tempDir, 'included.json');
|
|
474
|
+
writeFileSync(includedPath, JSON.stringify({
|
|
475
|
+
patterns: {
|
|
476
|
+
'slack-api': { properties: { domain: { const: 'slack.com' } }, required: ['domain'] },
|
|
477
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
478
|
+
},
|
|
479
|
+
rules: [{ 'slack-api': ['get-only'] }],
|
|
480
|
+
}));
|
|
481
|
+
const configPath = writeConfig({
|
|
482
|
+
include: ['included.json'],
|
|
483
|
+
patterns: {
|
|
484
|
+
'github-api': {
|
|
485
|
+
properties: { domain: { const: 'api.github.com' } },
|
|
486
|
+
required: ['domain'],
|
|
487
|
+
},
|
|
488
|
+
},
|
|
489
|
+
rules: [{ 'github-api': ['get-only'] }],
|
|
490
|
+
});
|
|
491
|
+
const config = new Config(configPath, true);
|
|
492
|
+
// Included rule allows GET to slack
|
|
493
|
+
const slackGet = new Request('https://slack.com/api/conversations.list');
|
|
494
|
+
expect(await config.check(slackGet)).toBe(true);
|
|
495
|
+
// Parent rule allows GET to github using pattern from included config
|
|
496
|
+
const githubGet = new Request('https://api.github.com/repos');
|
|
497
|
+
expect(await config.check(githubGet)).toBe(true);
|
|
498
|
+
// POST to slack is rejected by the included rule
|
|
499
|
+
const slackPost = new Request('https://slack.com/api/chat.postMessage', { method: 'POST' });
|
|
500
|
+
expect(await config.check(slackPost)).toBe(false);
|
|
501
|
+
});
|
|
502
|
+
it('parent patterns override included patterns with the same name', async () => {
|
|
503
|
+
const includedPath = join(tempDir, 'included.json');
|
|
504
|
+
writeFileSync(includedPath, JSON.stringify({
|
|
505
|
+
patterns: {
|
|
506
|
+
scope: { properties: { domain: { const: 'included.com' } }, required: ['domain'] },
|
|
507
|
+
permission: { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
508
|
+
},
|
|
509
|
+
}));
|
|
510
|
+
const configPath = writeConfig({
|
|
511
|
+
include: ['included.json'],
|
|
512
|
+
patterns: {
|
|
513
|
+
scope: { properties: { domain: { const: 'parent.com' } }, required: ['domain'] },
|
|
514
|
+
},
|
|
515
|
+
rules: [{ scope: ['permission'] }],
|
|
516
|
+
});
|
|
517
|
+
const config = new Config(configPath, true);
|
|
518
|
+
// Parent overrides the scope pattern, so parent.com matches
|
|
519
|
+
const parentRequest = new Request('https://parent.com/test');
|
|
520
|
+
expect(await config.check(parentRequest)).toBe(true);
|
|
521
|
+
// included.com no longer matches scope because parent overrode it
|
|
522
|
+
const includedRequest = new Request('https://included.com/test');
|
|
523
|
+
expect(await config.check(includedRequest)).toBe(false);
|
|
524
|
+
});
|
|
525
|
+
it('included rules come before parent rules in evaluation order', async () => {
|
|
526
|
+
const includedPath = join(tempDir, 'included.json');
|
|
527
|
+
writeFileSync(includedPath, JSON.stringify({
|
|
528
|
+
patterns: {
|
|
529
|
+
'all-https': {
|
|
530
|
+
properties: { protocol: { const: 'https' } },
|
|
531
|
+
required: ['protocol'],
|
|
532
|
+
},
|
|
533
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
534
|
+
},
|
|
535
|
+
rules: [{ 'all-https': ['get-only'] }],
|
|
536
|
+
}));
|
|
537
|
+
const configPath = writeConfig({
|
|
538
|
+
include: ['included.json'],
|
|
539
|
+
patterns: {
|
|
540
|
+
'any-method': {},
|
|
541
|
+
},
|
|
542
|
+
// This rule would allow everything, but it's appended after included rules
|
|
543
|
+
rules: [{ 'all-https': ['any-method'] }],
|
|
544
|
+
});
|
|
545
|
+
const config = new Config(configPath, true);
|
|
546
|
+
// The included rule matches first and only allows GET
|
|
547
|
+
const postRequest = new Request('https://example.com', { method: 'POST' });
|
|
548
|
+
expect(await config.check(postRequest)).toBe(false);
|
|
549
|
+
});
|
|
550
|
+
it('resolves includes recursively', async () => {
|
|
551
|
+
const deepIncludedPath = join(tempDir, 'deep.json');
|
|
552
|
+
writeFileSync(deepIncludedPath, JSON.stringify({
|
|
553
|
+
patterns: {
|
|
554
|
+
'deep-pattern': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
555
|
+
},
|
|
556
|
+
}));
|
|
557
|
+
const middlePath = join(tempDir, 'middle.json');
|
|
558
|
+
writeFileSync(middlePath, JSON.stringify({
|
|
559
|
+
include: ['deep.json'],
|
|
560
|
+
patterns: {
|
|
561
|
+
'middle-scope': {
|
|
562
|
+
properties: { domain: { const: 'example.com' } },
|
|
563
|
+
required: ['domain'],
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
rules: [{ 'middle-scope': ['deep-pattern'] }],
|
|
567
|
+
}));
|
|
568
|
+
const configPath = writeConfig({
|
|
569
|
+
include: ['middle.json'],
|
|
570
|
+
});
|
|
571
|
+
const config = new Config(configPath, true);
|
|
572
|
+
const getRequest = new Request('https://example.com/test');
|
|
573
|
+
expect(await config.check(getRequest)).toBe(true);
|
|
574
|
+
const postRequest = new Request('https://example.com/test', { method: 'POST' });
|
|
575
|
+
expect(await config.check(postRequest)).toBe(false);
|
|
576
|
+
});
|
|
577
|
+
it('throws ConfigError on circular includes', () => {
|
|
578
|
+
const aPath = join(tempDir, 'a.json');
|
|
579
|
+
const bPath = join(tempDir, 'b.json');
|
|
580
|
+
writeFileSync(aPath, JSON.stringify({ include: ['b.json'] }));
|
|
581
|
+
writeFileSync(bPath, JSON.stringify({ include: ['a.json'] }));
|
|
582
|
+
expect(() => new Config(aPath, true)).toThrow(ConfigError);
|
|
583
|
+
});
|
|
584
|
+
it('throws ConfigError when a config includes itself', () => {
|
|
585
|
+
const configPath = writeConfig({ include: ['config.json'] });
|
|
586
|
+
expect(() => new Config(configPath, true)).toThrow(ConfigError);
|
|
587
|
+
});
|
|
588
|
+
it('resolves relative include paths from the directory of the including config', async () => {
|
|
589
|
+
const subDir = join(tempDir, 'sub');
|
|
590
|
+
mkdirSync(subDir, { recursive: true });
|
|
591
|
+
const subIncludedPath = join(subDir, 'sub-included.json');
|
|
592
|
+
writeFileSync(subIncludedPath, JSON.stringify({
|
|
593
|
+
patterns: {
|
|
594
|
+
'sub-permission': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
595
|
+
},
|
|
596
|
+
}));
|
|
597
|
+
const middlePath = join(subDir, 'middle.json');
|
|
598
|
+
writeFileSync(middlePath, JSON.stringify({
|
|
599
|
+
include: ['sub-included.json'],
|
|
600
|
+
patterns: {
|
|
601
|
+
'sub-scope': {
|
|
602
|
+
properties: { domain: { const: 'example.com' } },
|
|
603
|
+
required: ['domain'],
|
|
604
|
+
},
|
|
605
|
+
},
|
|
606
|
+
rules: [{ 'sub-scope': ['sub-permission'] }],
|
|
607
|
+
}));
|
|
608
|
+
const configPath = writeConfig({
|
|
609
|
+
include: ['sub/middle.json'],
|
|
610
|
+
});
|
|
611
|
+
const config = new Config(configPath, true);
|
|
612
|
+
const getRequest = new Request('https://example.com/test');
|
|
613
|
+
expect(await config.check(getRequest)).toBe(true);
|
|
614
|
+
});
|
|
615
|
+
it('merges patterns and rules from multiple includes in order', async () => {
|
|
616
|
+
const firstPath = join(tempDir, 'first.json');
|
|
617
|
+
writeFileSync(firstPath, JSON.stringify({
|
|
618
|
+
patterns: {
|
|
619
|
+
'first-scope': { properties: { domain: { const: 'first.com' } }, required: ['domain'] },
|
|
620
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
621
|
+
},
|
|
622
|
+
rules: [{ 'first-scope': ['get-only'] }],
|
|
623
|
+
}));
|
|
624
|
+
const secondPath = join(tempDir, 'second.json');
|
|
625
|
+
writeFileSync(secondPath, JSON.stringify({
|
|
626
|
+
patterns: {
|
|
627
|
+
'second-scope': {
|
|
628
|
+
properties: { domain: { const: 'second.com' } },
|
|
629
|
+
required: ['domain'],
|
|
630
|
+
},
|
|
631
|
+
'post-only': { properties: { method: { const: 'POST' } }, required: ['method'] },
|
|
632
|
+
},
|
|
633
|
+
rules: [{ 'second-scope': ['post-only'] }],
|
|
634
|
+
}));
|
|
635
|
+
const configPath = writeConfig({
|
|
636
|
+
include: ['first.json', 'second.json'],
|
|
637
|
+
});
|
|
638
|
+
const config = new Config(configPath, true);
|
|
639
|
+
const firstGet = new Request('https://first.com/test');
|
|
640
|
+
expect(await config.check(firstGet)).toBe(true);
|
|
641
|
+
const secondPost = new Request('https://second.com/test', { method: 'POST' });
|
|
642
|
+
expect(await config.check(secondPost)).toBe(true);
|
|
643
|
+
const firstPost = new Request('https://first.com/test', { method: 'POST' });
|
|
644
|
+
expect(await config.check(firstPost)).toBe(false);
|
|
645
|
+
});
|
|
646
|
+
});
|
|
647
|
+
describe('check (top-level function)', () => {
|
|
648
|
+
let tempDir;
|
|
649
|
+
beforeEach(() => {
|
|
650
|
+
tempDir = join(tmpdir(), `detent-test-${String(Date.now())}-${Math.random().toString(36).slice(2)}`);
|
|
651
|
+
mkdirSync(tempDir, { recursive: true });
|
|
652
|
+
});
|
|
653
|
+
afterEach(() => {
|
|
654
|
+
rmSync(tempDir, { recursive: true, force: true });
|
|
655
|
+
});
|
|
656
|
+
it('uses config from provided path', async () => {
|
|
657
|
+
const configPath = join(tempDir, 'config.json');
|
|
658
|
+
writeFileSync(configPath, JSON.stringify({
|
|
659
|
+
patterns: {
|
|
660
|
+
everything: {},
|
|
661
|
+
'get-only': { properties: { method: { const: 'GET' } }, required: ['method'] },
|
|
662
|
+
},
|
|
663
|
+
rules: [{ everything: ['get-only'] }],
|
|
664
|
+
}));
|
|
665
|
+
const request = new Request('https://example.com');
|
|
666
|
+
expect(await check(request, configPath)).toBe(true);
|
|
667
|
+
const postRequest = new Request('https://example.com', { method: 'POST' });
|
|
668
|
+
expect(await check(postRequest, configPath)).toBe(false);
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
//# sourceMappingURL=check.test.js.map
|