devanvil 0.0.0-stage → 2.1.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/LICENSE +21 -0
- package/README.ar.md +113 -0
- package/README.md +272 -2
- package/SECURITY.md +49 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +664 -0
- package/dist/core/contract-drift.d.ts +49 -0
- package/dist/core/contract-drift.js +312 -0
- package/dist/core/converters.d.ts +18 -0
- package/dist/core/converters.js +989 -0
- package/dist/core/cron.d.ts +33 -0
- package/dist/core/cron.js +309 -0
- package/dist/core/crypto.d.ts +59 -0
- package/dist/core/crypto.js +260 -0
- package/dist/core/curl.d.ts +17 -0
- package/dist/core/curl.js +174 -0
- package/dist/core/diff.d.ts +36 -0
- package/dist/core/diff.js +148 -0
- package/dist/core/encoding.d.ts +3 -0
- package/dist/core/encoding.js +143 -0
- package/dist/core/importers.d.ts +65 -0
- package/dist/core/importers.js +361 -0
- package/dist/core/jwt.d.ts +39 -0
- package/dist/core/jwt.js +269 -0
- package/dist/core/llm-tokens.d.ts +38 -0
- package/dist/core/llm-tokens.js +125 -0
- package/dist/core/log-analyzer.d.ts +30 -0
- package/dist/core/log-analyzer.js +155 -0
- package/dist/core/network.d.ts +72 -0
- package/dist/core/network.js +185 -0
- package/dist/core/oauth-protobuf.d.ts +74 -0
- package/dist/core/oauth-protobuf.js +396 -0
- package/dist/core/openapi.d.ts +27 -0
- package/dist/core/openapi.js +257 -0
- package/dist/core/pipeline.d.ts +28 -0
- package/dist/core/pipeline.js +154 -0
- package/dist/core/regex.d.ts +50 -0
- package/dist/core/regex.js +360 -0
- package/dist/core/runner.d.ts +53 -0
- package/dist/core/runner.js +250 -0
- package/dist/core/secrets.d.ts +87 -0
- package/dist/core/secrets.js +603 -0
- package/dist/core/security.d.ts +34 -0
- package/dist/core/security.js +301 -0
- package/dist/core/sql-explain.d.ts +50 -0
- package/dist/core/sql-explain.js +259 -0
- package/dist/core/tls.d.ts +44 -0
- package/dist/core/tls.js +169 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +31 -0
- package/dist/server/app.d.ts +31 -0
- package/dist/server/app.js +359 -0
- package/dist/server/http-proxy.d.ts +55 -0
- package/dist/server/http-proxy.js +373 -0
- package/dist/server/http-utils.d.ts +28 -0
- package/dist/server/http-utils.js +118 -0
- package/dist/server/load-tester.d.ts +53 -0
- package/dist/server/load-tester.js +137 -0
- package/dist/server/mcp.d.ts +35 -0
- package/dist/server/mcp.js +470 -0
- package/dist/server/mock-store.d.ts +38 -0
- package/dist/server/mock-store.js +238 -0
- package/dist/server/plugin-loader.d.ts +61 -0
- package/dist/server/plugin-loader.js +305 -0
- package/dist/server/recorder-store.d.ts +35 -0
- package/dist/server/recorder-store.js +147 -0
- package/dist/server/routes/network-routes.d.ts +3 -0
- package/dist/server/routes/network-routes.js +441 -0
- package/dist/server/routes/tools-routes.d.ts +3 -0
- package/dist/server/routes/tools-routes.js +299 -0
- package/dist/server/routes/workspace-routes.d.ts +3 -0
- package/dist/server/routes/workspace-routes.js +262 -0
- package/dist/server/webhook-store.d.ts +29 -0
- package/dist/server/webhook-store.js +100 -0
- package/dist/server/workspace-store.d.ts +91 -0
- package/dist/server/workspace-store.js +448 -0
- package/dist/server/ws-probe.d.ts +34 -0
- package/dist/server/ws-probe.js +291 -0
- package/dist/ui/app.js +290 -0
- package/dist/ui/favicon.svg +14 -0
- package/dist/ui/index.html +83 -0
- package/dist/ui/logo.png +0 -0
- package/dist/ui/styles.css +694 -0
- package/dist/ui/tools-data.js +719 -0
- package/dist/ui/tools-network.js +769 -0
- package/dist/ui/tools-ops.js +299 -0
- package/dist/ui/tools-pro.js +500 -0
- package/dist/ui/tools-security.js +397 -0
- package/dist/ui/tools-v12.js +765 -0
- package/dist/ui/tools-v13.js +321 -0
- package/dist/ui/tools-v20.js +563 -0
- package/dist/ui/tools-v21.js +411 -0
- package/package.json +75 -4
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { generateUuidV7 } from '../core/crypto.js';
|
|
2
|
+
import { assertSafeChannelName } from '../core/security.js';
|
|
3
|
+
const MAX_EVENTS_PER_CHANNEL = 100;
|
|
4
|
+
const MAX_CHANNELS = 50;
|
|
5
|
+
const MAX_SSE_CLIENTS = 50;
|
|
6
|
+
export class WebhookStore {
|
|
7
|
+
channels = new Map();
|
|
8
|
+
listeners = new Map();
|
|
9
|
+
capture(input) {
|
|
10
|
+
const channel = assertSafeChannelName(input.channel);
|
|
11
|
+
if (!this.channels.has(channel) && this.channels.size >= MAX_CHANNELS) {
|
|
12
|
+
// Evict oldest channel to stay bounded
|
|
13
|
+
const oldestKey = this.channels.keys().next().value;
|
|
14
|
+
if (oldestKey)
|
|
15
|
+
this.channels.delete(oldestKey);
|
|
16
|
+
}
|
|
17
|
+
const event = {
|
|
18
|
+
...input,
|
|
19
|
+
channel,
|
|
20
|
+
id: generateUuidV7(),
|
|
21
|
+
receivedAt: new Date().toISOString(),
|
|
22
|
+
};
|
|
23
|
+
const list = this.channels.get(channel) ?? [];
|
|
24
|
+
list.unshift(event);
|
|
25
|
+
if (list.length > MAX_EVENTS_PER_CHANNEL) {
|
|
26
|
+
list.length = MAX_EVENTS_PER_CHANNEL;
|
|
27
|
+
}
|
|
28
|
+
this.channels.set(channel, list);
|
|
29
|
+
this.broadcast(channel, 'webhook', event);
|
|
30
|
+
return event;
|
|
31
|
+
}
|
|
32
|
+
list(rawChannel) {
|
|
33
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
34
|
+
return [...(this.channels.get(channel) ?? [])];
|
|
35
|
+
}
|
|
36
|
+
listChannels() {
|
|
37
|
+
return Array.from(this.channels.entries()).map(([channel, events]) => ({
|
|
38
|
+
channel,
|
|
39
|
+
count: events.length,
|
|
40
|
+
...(events[0] ? { lastReceivedAt: events[0].receivedAt } : {}),
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
clear(rawChannel) {
|
|
44
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
45
|
+
this.channels.set(channel, []);
|
|
46
|
+
this.broadcast(channel, 'cleared', { channel });
|
|
47
|
+
}
|
|
48
|
+
totalListeners() {
|
|
49
|
+
let total = 0;
|
|
50
|
+
for (const set of this.listeners.values())
|
|
51
|
+
total += set.size;
|
|
52
|
+
return total;
|
|
53
|
+
}
|
|
54
|
+
subscribe(rawChannel, res) {
|
|
55
|
+
const channel = assertSafeChannelName(rawChannel);
|
|
56
|
+
if (this.totalListeners() >= MAX_SSE_CLIENTS) {
|
|
57
|
+
throw new Error(`Maximum concurrent SSE listeners (${MAX_SSE_CLIENTS}) reached`);
|
|
58
|
+
}
|
|
59
|
+
let set = this.listeners.get(channel);
|
|
60
|
+
if (!set) {
|
|
61
|
+
set = new Set();
|
|
62
|
+
this.listeners.set(channel, set);
|
|
63
|
+
}
|
|
64
|
+
set.add(res);
|
|
65
|
+
const heartbeat = setInterval(() => {
|
|
66
|
+
if (!res.writableEnded) {
|
|
67
|
+
try {
|
|
68
|
+
res.write(': heartbeat\n\n');
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
set?.delete(res);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}, 20_000);
|
|
75
|
+
heartbeat.unref();
|
|
76
|
+
return () => {
|
|
77
|
+
clearInterval(heartbeat);
|
|
78
|
+
set?.delete(res);
|
|
79
|
+
if (set && set.size === 0) {
|
|
80
|
+
this.listeners.delete(channel);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
broadcast(channel, eventName, payload) {
|
|
85
|
+
const set = this.listeners.get(channel);
|
|
86
|
+
if (!set || set.size === 0)
|
|
87
|
+
return;
|
|
88
|
+
const frame = `event: ${eventName}\ndata: ${JSON.stringify(payload)}\n\n`;
|
|
89
|
+
for (const res of set) {
|
|
90
|
+
if (!res.writableEnded) {
|
|
91
|
+
try {
|
|
92
|
+
res.write(frame);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
set.delete(res);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { SavedApiRequest } from '../core/importers.js';
|
|
2
|
+
import type { MockRoute, MockRouteInput } from './mock-store.js';
|
|
3
|
+
export interface WorkspaceSecretWarning {
|
|
4
|
+
scope: 'environment' | 'request';
|
|
5
|
+
location: string;
|
|
6
|
+
ruleId: string;
|
|
7
|
+
ruleName: string;
|
|
8
|
+
severity: 'critical' | 'high' | 'medium';
|
|
9
|
+
masked: string;
|
|
10
|
+
recommendation: string;
|
|
11
|
+
}
|
|
12
|
+
export interface EncryptedWorkspaceBundle {
|
|
13
|
+
format: 'devanvil-encrypted-workspace';
|
|
14
|
+
schemaVersion: 1;
|
|
15
|
+
cipher: 'AES-256-GCM';
|
|
16
|
+
kdf: 'scrypt';
|
|
17
|
+
createdAt: string;
|
|
18
|
+
envelope: string;
|
|
19
|
+
}
|
|
20
|
+
export interface WorkspaceEnvironment {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
variables: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
export interface WorkspaceCollection {
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
requests: SavedApiRequest[];
|
|
29
|
+
createdAt: string;
|
|
30
|
+
}
|
|
31
|
+
export interface DevAnvilWorkspaceData {
|
|
32
|
+
schemaVersion: 1;
|
|
33
|
+
name: string;
|
|
34
|
+
activeEnvironmentId: string;
|
|
35
|
+
environments: WorkspaceEnvironment[];
|
|
36
|
+
collections: WorkspaceCollection[];
|
|
37
|
+
mockRoutes: MockRouteInput[];
|
|
38
|
+
updatedAt: string;
|
|
39
|
+
}
|
|
40
|
+
export declare class WorkspaceStore {
|
|
41
|
+
private readonly filePath?;
|
|
42
|
+
private readonly localSecretsPath?;
|
|
43
|
+
private localEnvOverrides;
|
|
44
|
+
private data;
|
|
45
|
+
constructor(filePath?: string);
|
|
46
|
+
private createDefaultWorkspace;
|
|
47
|
+
isPersistent(): boolean;
|
|
48
|
+
getFilePath(): string | undefined;
|
|
49
|
+
getSnapshot(currentMocks?: MockRoute[]): DevAnvilWorkspaceData;
|
|
50
|
+
getActiveEnvironment(): WorkspaceEnvironment | undefined;
|
|
51
|
+
getLocalOverridesPath(): string | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Loads optional gitignored companion file (e.g. `devanvil.local.json`) containing
|
|
54
|
+
* per-environment secret overrides `{ "environments": { "Local Dev": { "API_TOKEN": "..." } } }`
|
|
55
|
+
* or flat `{ "API_TOKEN": "..." }`. Values are NEVER written back to `devanvil.json`.
|
|
56
|
+
*/
|
|
57
|
+
reloadLocalOverrides(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Audits environments and saved requests inside the workspace for accidentally committed
|
|
60
|
+
* plaintext secrets (API keys, private keys, DB passwords) and suggests `{{env.VAR}}` instead.
|
|
61
|
+
*/
|
|
62
|
+
auditWorkspaceSecrets(): WorkspaceSecretWarning[];
|
|
63
|
+
/**
|
|
64
|
+
* Exports the full workspace (environments, collections, mock routes) encrypted with
|
|
65
|
+
* authenticated AES-256-GCM + scrypt KDF (`.anvil` bundle).
|
|
66
|
+
*/
|
|
67
|
+
exportEncryptedBundle(passphrase: string, currentMocks?: MockRoute[]): EncryptedWorkspaceBundle;
|
|
68
|
+
/**
|
|
69
|
+
* Decrypts and imports an authenticated `.anvil` workspace bundle.
|
|
70
|
+
*/
|
|
71
|
+
importEncryptedBundle(rawBundle: string, passphrase: string): DevAnvilWorkspaceData;
|
|
72
|
+
setActiveEnvironment(id: string): WorkspaceEnvironment;
|
|
73
|
+
upsertEnvironment(input: {
|
|
74
|
+
id?: string;
|
|
75
|
+
name: string;
|
|
76
|
+
variables: Record<string, string>;
|
|
77
|
+
}): WorkspaceEnvironment;
|
|
78
|
+
deleteEnvironment(id: string): boolean;
|
|
79
|
+
addCollection(name: string, requests: SavedApiRequest[]): WorkspaceCollection;
|
|
80
|
+
saveRequestToCollection(collectionId: string | undefined, request: Omit<SavedApiRequest, 'id'> & {
|
|
81
|
+
id?: string;
|
|
82
|
+
}): SavedApiRequest;
|
|
83
|
+
updateRequestInCollection(collectionId: string, requestId: string, patch: Partial<Omit<SavedApiRequest, 'id'>>): SavedApiRequest;
|
|
84
|
+
deleteRequestFromCollection(collectionId: string, requestId: string): boolean;
|
|
85
|
+
deleteCollection(id: string): boolean;
|
|
86
|
+
importWorkspaceJson(rawJson: string): DevAnvilWorkspaceData;
|
|
87
|
+
syncMocksAndPersist(mocks: MockRoute[]): void;
|
|
88
|
+
private touchAndPersist;
|
|
89
|
+
private loadFromDisk;
|
|
90
|
+
private persistToDisk;
|
|
91
|
+
}
|
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import * as crypto from 'node:crypto';
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { decryptAesGcm, encryptAesGcm } from '../core/crypto.js';
|
|
5
|
+
import { scanSecrets } from '../core/secrets.js';
|
|
6
|
+
import { isDangerousKey, safeJsonParse } from '../core/security.js';
|
|
7
|
+
const MAX_COLLECTIONS = 50;
|
|
8
|
+
const MAX_REQUESTS_PER_COLLECTION = 200;
|
|
9
|
+
const MAX_ENVIRONMENTS = 20;
|
|
10
|
+
function sanitizeVarMap(raw) {
|
|
11
|
+
const out = Object.create(null);
|
|
12
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
13
|
+
return out;
|
|
14
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
15
|
+
const cleanKey = k.trim();
|
|
16
|
+
if (!cleanKey || isDangerousKey(cleanKey))
|
|
17
|
+
continue;
|
|
18
|
+
out[cleanKey] = typeof v === 'string' ? v : String(v ?? '');
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
export class WorkspaceStore {
|
|
23
|
+
filePath;
|
|
24
|
+
localSecretsPath;
|
|
25
|
+
localEnvOverrides = Object.create(null);
|
|
26
|
+
data;
|
|
27
|
+
constructor(filePath) {
|
|
28
|
+
this.filePath = filePath ? path.resolve(filePath) : undefined;
|
|
29
|
+
if (this.filePath) {
|
|
30
|
+
const ext = path.extname(this.filePath);
|
|
31
|
+
const base = ext ? this.filePath.slice(0, -ext.length) : this.filePath;
|
|
32
|
+
this.localSecretsPath = `${base}.local${ext || '.json'}`;
|
|
33
|
+
}
|
|
34
|
+
this.data = this.createDefaultWorkspace();
|
|
35
|
+
if (this.filePath && fs.existsSync(this.filePath)) {
|
|
36
|
+
this.loadFromDisk(this.filePath);
|
|
37
|
+
}
|
|
38
|
+
this.reloadLocalOverrides();
|
|
39
|
+
}
|
|
40
|
+
createDefaultWorkspace() {
|
|
41
|
+
return {
|
|
42
|
+
schemaVersion: 1,
|
|
43
|
+
name: 'Default DevAnvil Workspace',
|
|
44
|
+
activeEnvironmentId: 'env_local',
|
|
45
|
+
environments: [
|
|
46
|
+
{
|
|
47
|
+
id: 'env_local',
|
|
48
|
+
name: 'Local Dev',
|
|
49
|
+
variables: {
|
|
50
|
+
BASE_URL: 'http://127.0.0.1:4321',
|
|
51
|
+
API_TOKEN: 'demo-local-token',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'env_staging',
|
|
56
|
+
name: 'Staging',
|
|
57
|
+
variables: {
|
|
58
|
+
BASE_URL: 'https://staging.example.com',
|
|
59
|
+
API_TOKEN: 'replace-me',
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
collections: [
|
|
64
|
+
{
|
|
65
|
+
id: 'col_starter',
|
|
66
|
+
name: 'DevAnvil Starter Requests',
|
|
67
|
+
createdAt: new Date().toISOString(),
|
|
68
|
+
requests: [
|
|
69
|
+
{
|
|
70
|
+
id: 'req_health',
|
|
71
|
+
name: 'Local Mock Health Check',
|
|
72
|
+
method: 'GET',
|
|
73
|
+
url: '{{BASE_URL}}/mock/health',
|
|
74
|
+
headers: { Accept: 'application/json' },
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: 'req_user_mock',
|
|
78
|
+
name: 'Dynamic Mock User (:id)',
|
|
79
|
+
method: 'GET',
|
|
80
|
+
url: '{{BASE_URL}}/mock/users/42?role=admin',
|
|
81
|
+
headers: { Accept: 'application/json', Authorization: 'Bearer {{API_TOKEN}}' },
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
mockRoutes: [],
|
|
87
|
+
updatedAt: new Date().toISOString(),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
isPersistent() {
|
|
91
|
+
return Boolean(this.filePath);
|
|
92
|
+
}
|
|
93
|
+
getFilePath() {
|
|
94
|
+
return this.filePath;
|
|
95
|
+
}
|
|
96
|
+
getSnapshot(currentMocks) {
|
|
97
|
+
return {
|
|
98
|
+
...this.data,
|
|
99
|
+
mockRoutes: currentMocks
|
|
100
|
+
? currentMocks.map((r) => ({
|
|
101
|
+
method: r.method,
|
|
102
|
+
path: r.path,
|
|
103
|
+
status: r.status,
|
|
104
|
+
headers: r.headers,
|
|
105
|
+
body: r.body,
|
|
106
|
+
delayMs: r.delayMs,
|
|
107
|
+
enabled: r.enabled,
|
|
108
|
+
}))
|
|
109
|
+
: this.data.mockRoutes,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
getActiveEnvironment() {
|
|
113
|
+
const base = this.data.environments.find((e) => e.id === this.data.activeEnvironmentId) ??
|
|
114
|
+
this.data.environments[0];
|
|
115
|
+
if (!base)
|
|
116
|
+
return undefined;
|
|
117
|
+
const overrides = this.localEnvOverrides[base.id] ?? this.localEnvOverrides[base.name];
|
|
118
|
+
if (!overrides)
|
|
119
|
+
return base;
|
|
120
|
+
return {
|
|
121
|
+
...base,
|
|
122
|
+
variables: { ...base.variables, ...overrides },
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
getLocalOverridesPath() {
|
|
126
|
+
return this.localSecretsPath && fs.existsSync(this.localSecretsPath) ? this.localSecretsPath : undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Loads optional gitignored companion file (e.g. `devanvil.local.json`) containing
|
|
130
|
+
* per-environment secret overrides `{ "environments": { "Local Dev": { "API_TOKEN": "..." } } }`
|
|
131
|
+
* or flat `{ "API_TOKEN": "..." }`. Values are NEVER written back to `devanvil.json`.
|
|
132
|
+
*/
|
|
133
|
+
reloadLocalOverrides() {
|
|
134
|
+
this.localEnvOverrides = Object.create(null);
|
|
135
|
+
if (!this.localSecretsPath || !fs.existsSync(this.localSecretsPath))
|
|
136
|
+
return;
|
|
137
|
+
try {
|
|
138
|
+
const parsed = safeJsonParse(fs.readFileSync(this.localSecretsPath, 'utf8'));
|
|
139
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
140
|
+
return;
|
|
141
|
+
if (parsed.environments && typeof parsed.environments === 'object' && !Array.isArray(parsed.environments)) {
|
|
142
|
+
for (const [envKey, vars] of Object.entries(parsed.environments)) {
|
|
143
|
+
if (!envKey || isDangerousKey(envKey))
|
|
144
|
+
continue;
|
|
145
|
+
this.localEnvOverrides[envKey] = sanitizeVarMap(vars);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
// Flat override applies to every environment
|
|
150
|
+
const flat = sanitizeVarMap(parsed);
|
|
151
|
+
for (const env of this.data.environments) {
|
|
152
|
+
this.localEnvOverrides[env.id] = flat;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
// Ignore malformed local override file gracefully
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Audits environments and saved requests inside the workspace for accidentally committed
|
|
162
|
+
* plaintext secrets (API keys, private keys, DB passwords) and suggests `{{env.VAR}}` instead.
|
|
163
|
+
*/
|
|
164
|
+
auditWorkspaceSecrets() {
|
|
165
|
+
const warnings = [];
|
|
166
|
+
for (const env of this.data.environments) {
|
|
167
|
+
for (const [key, val] of Object.entries(env.variables)) {
|
|
168
|
+
if (!val || /^\{\{\s*env\.[A-Za-z0-9_]+\s*\}\}$/.test(val.trim()))
|
|
169
|
+
continue;
|
|
170
|
+
const scan = scanSecrets(`${key}=${val}`, { checkHighEntropy: true });
|
|
171
|
+
for (const f of scan.findings) {
|
|
172
|
+
warnings.push({
|
|
173
|
+
scope: 'environment',
|
|
174
|
+
location: `${env.name}.${key}`,
|
|
175
|
+
ruleId: f.ruleId,
|
|
176
|
+
ruleName: f.name,
|
|
177
|
+
severity: f.severity,
|
|
178
|
+
masked: f.masked,
|
|
179
|
+
recommendation: `Replace with "{{env.${key}}}" or store in gitignored devanvil.local.json`,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const col of this.data.collections) {
|
|
185
|
+
for (const req of col.requests) {
|
|
186
|
+
const blob = [
|
|
187
|
+
req.url,
|
|
188
|
+
...Object.entries(req.headers ?? {}).map(([k, v]) => `${k}: ${v}`),
|
|
189
|
+
req.body ?? '',
|
|
190
|
+
].join('\n');
|
|
191
|
+
const scan = scanSecrets(blob, { checkHighEntropy: false });
|
|
192
|
+
for (const f of scan.findings) {
|
|
193
|
+
warnings.push({
|
|
194
|
+
scope: 'request',
|
|
195
|
+
location: `${col.name} -> ${req.name}`,
|
|
196
|
+
ruleId: f.ruleId,
|
|
197
|
+
ruleName: f.name,
|
|
198
|
+
severity: f.severity,
|
|
199
|
+
masked: f.masked,
|
|
200
|
+
recommendation: 'Use {{env.SECRET_NAME}} or an environment variable instead of hardcoding credentials',
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return warnings;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Exports the full workspace (environments, collections, mock routes) encrypted with
|
|
209
|
+
* authenticated AES-256-GCM + scrypt KDF (`.anvil` bundle).
|
|
210
|
+
*/
|
|
211
|
+
exportEncryptedBundle(passphrase, currentMocks) {
|
|
212
|
+
if (!passphrase || passphrase.length < 6) {
|
|
213
|
+
throw new Error('Passphrase must be at least 6 characters to encrypt a workspace bundle');
|
|
214
|
+
}
|
|
215
|
+
const snapshot = this.getSnapshot(currentMocks);
|
|
216
|
+
const envelope = encryptAesGcm(JSON.stringify(snapshot), passphrase);
|
|
217
|
+
return {
|
|
218
|
+
format: 'devanvil-encrypted-workspace',
|
|
219
|
+
schemaVersion: 1,
|
|
220
|
+
cipher: 'AES-256-GCM',
|
|
221
|
+
kdf: 'scrypt',
|
|
222
|
+
createdAt: new Date().toISOString(),
|
|
223
|
+
envelope,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Decrypts and imports an authenticated `.anvil` workspace bundle.
|
|
228
|
+
*/
|
|
229
|
+
importEncryptedBundle(rawBundle, passphrase) {
|
|
230
|
+
const parsed = safeJsonParse(rawBundle);
|
|
231
|
+
if (!parsed ||
|
|
232
|
+
typeof parsed !== 'object' ||
|
|
233
|
+
parsed.format !== 'devanvil-encrypted-workspace' ||
|
|
234
|
+
typeof parsed.envelope !== 'string') {
|
|
235
|
+
throw new Error('Invalid encrypted DevAnvil workspace (.anvil) bundle');
|
|
236
|
+
}
|
|
237
|
+
const plaintext = decryptAesGcm(parsed.envelope, passphrase);
|
|
238
|
+
return this.importWorkspaceJson(plaintext);
|
|
239
|
+
}
|
|
240
|
+
setActiveEnvironment(id) {
|
|
241
|
+
const found = this.data.environments.find((e) => e.id === id);
|
|
242
|
+
if (!found)
|
|
243
|
+
throw new Error(`Environment "${id}" not found`);
|
|
244
|
+
this.data.activeEnvironmentId = found.id;
|
|
245
|
+
this.touchAndPersist();
|
|
246
|
+
return found;
|
|
247
|
+
}
|
|
248
|
+
upsertEnvironment(input) {
|
|
249
|
+
const name = input.name.trim();
|
|
250
|
+
if (!name)
|
|
251
|
+
throw new Error('Environment name is required');
|
|
252
|
+
const vars = sanitizeVarMap(input.variables);
|
|
253
|
+
const existing = input.id ? this.data.environments.find((e) => e.id === input.id) : undefined;
|
|
254
|
+
if (existing) {
|
|
255
|
+
existing.name = name;
|
|
256
|
+
existing.variables = vars;
|
|
257
|
+
this.touchAndPersist();
|
|
258
|
+
return existing;
|
|
259
|
+
}
|
|
260
|
+
if (this.data.environments.length >= MAX_ENVIRONMENTS) {
|
|
261
|
+
throw new Error(`Maximum of ${MAX_ENVIRONMENTS} environments reached`);
|
|
262
|
+
}
|
|
263
|
+
const created = {
|
|
264
|
+
id: `env_${crypto.randomBytes(4).toString('hex')}`,
|
|
265
|
+
name,
|
|
266
|
+
variables: vars,
|
|
267
|
+
};
|
|
268
|
+
this.data.environments.push(created);
|
|
269
|
+
this.touchAndPersist();
|
|
270
|
+
return created;
|
|
271
|
+
}
|
|
272
|
+
deleteEnvironment(id) {
|
|
273
|
+
if (this.data.environments.length <= 1) {
|
|
274
|
+
throw new Error('Cannot delete the last remaining environment');
|
|
275
|
+
}
|
|
276
|
+
const idx = this.data.environments.findIndex((e) => e.id === id);
|
|
277
|
+
if (idx === -1)
|
|
278
|
+
return false;
|
|
279
|
+
this.data.environments.splice(idx, 1);
|
|
280
|
+
if (this.data.activeEnvironmentId === id) {
|
|
281
|
+
this.data.activeEnvironmentId = this.data.environments[0].id;
|
|
282
|
+
}
|
|
283
|
+
this.touchAndPersist();
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
addCollection(name, requests) {
|
|
287
|
+
if (this.data.collections.length >= MAX_COLLECTIONS) {
|
|
288
|
+
throw new Error(`Maximum of ${MAX_COLLECTIONS} collections reached`);
|
|
289
|
+
}
|
|
290
|
+
const cleanName = name.trim() || 'Untitled Collection';
|
|
291
|
+
const created = {
|
|
292
|
+
id: `col_${crypto.randomBytes(4).toString('hex')}`,
|
|
293
|
+
name: cleanName,
|
|
294
|
+
requests: requests.slice(0, MAX_REQUESTS_PER_COLLECTION),
|
|
295
|
+
createdAt: new Date().toISOString(),
|
|
296
|
+
};
|
|
297
|
+
this.data.collections.unshift(created);
|
|
298
|
+
this.touchAndPersist();
|
|
299
|
+
return created;
|
|
300
|
+
}
|
|
301
|
+
saveRequestToCollection(collectionId, request) {
|
|
302
|
+
let col = collectionId
|
|
303
|
+
? this.data.collections.find((c) => c.id === collectionId)
|
|
304
|
+
: this.data.collections[0];
|
|
305
|
+
if (!col) {
|
|
306
|
+
col = this.addCollection('Saved Requests', []);
|
|
307
|
+
}
|
|
308
|
+
if (col.requests.length >= MAX_REQUESTS_PER_COLLECTION) {
|
|
309
|
+
throw new Error(`Maximum of ${MAX_REQUESTS_PER_COLLECTION} requests per collection reached`);
|
|
310
|
+
}
|
|
311
|
+
const saved = {
|
|
312
|
+
id: request.id || `req_${crypto.randomBytes(4).toString('hex')}`,
|
|
313
|
+
name: request.name.trim() || `${request.method} ${request.url}`,
|
|
314
|
+
folder: request.folder,
|
|
315
|
+
method: request.method.toUpperCase(),
|
|
316
|
+
url: request.url,
|
|
317
|
+
headers: sanitizeVarMap(request.headers),
|
|
318
|
+
body: request.body,
|
|
319
|
+
notes: request.notes,
|
|
320
|
+
...(Array.isArray(request.assertions) && request.assertions.length ? { assertions: request.assertions } : {}),
|
|
321
|
+
...(Array.isArray(request.extract) && request.extract.length ? { extract: request.extract } : {}),
|
|
322
|
+
};
|
|
323
|
+
col.requests.unshift(saved);
|
|
324
|
+
this.touchAndPersist();
|
|
325
|
+
return saved;
|
|
326
|
+
}
|
|
327
|
+
updateRequestInCollection(collectionId, requestId, patch) {
|
|
328
|
+
const col = this.data.collections.find((c) => c.id === collectionId);
|
|
329
|
+
if (!col)
|
|
330
|
+
throw new Error(`Collection "${collectionId}" not found`);
|
|
331
|
+
const existing = col.requests.find((r) => r.id === requestId);
|
|
332
|
+
if (!existing)
|
|
333
|
+
throw new Error(`Request "${requestId}" not found in collection "${collectionId}"`);
|
|
334
|
+
if (typeof patch.name === 'string' && patch.name.trim())
|
|
335
|
+
existing.name = patch.name.trim();
|
|
336
|
+
if (typeof patch.method === 'string' && patch.method.trim())
|
|
337
|
+
existing.method = patch.method.trim().toUpperCase();
|
|
338
|
+
if (typeof patch.url === 'string' && patch.url.trim())
|
|
339
|
+
existing.url = patch.url.trim();
|
|
340
|
+
if (patch.headers && typeof patch.headers === 'object')
|
|
341
|
+
existing.headers = sanitizeVarMap(patch.headers);
|
|
342
|
+
if (typeof patch.body === 'string')
|
|
343
|
+
existing.body = patch.body;
|
|
344
|
+
if (typeof patch.notes === 'string')
|
|
345
|
+
existing.notes = patch.notes;
|
|
346
|
+
if (Array.isArray(patch.assertions)) {
|
|
347
|
+
existing.assertions = patch.assertions.length ? patch.assertions : undefined;
|
|
348
|
+
}
|
|
349
|
+
if (Array.isArray(patch.extract)) {
|
|
350
|
+
existing.extract = patch.extract.length ? patch.extract : undefined;
|
|
351
|
+
}
|
|
352
|
+
this.touchAndPersist();
|
|
353
|
+
return existing;
|
|
354
|
+
}
|
|
355
|
+
deleteRequestFromCollection(collectionId, requestId) {
|
|
356
|
+
const col = this.data.collections.find((c) => c.id === collectionId);
|
|
357
|
+
if (!col)
|
|
358
|
+
return false;
|
|
359
|
+
const idx = col.requests.findIndex((r) => r.id === requestId);
|
|
360
|
+
if (idx === -1)
|
|
361
|
+
return false;
|
|
362
|
+
col.requests.splice(idx, 1);
|
|
363
|
+
this.touchAndPersist();
|
|
364
|
+
return true;
|
|
365
|
+
}
|
|
366
|
+
deleteCollection(id) {
|
|
367
|
+
const idx = this.data.collections.findIndex((c) => c.id === id);
|
|
368
|
+
if (idx === -1)
|
|
369
|
+
return false;
|
|
370
|
+
this.data.collections.splice(idx, 1);
|
|
371
|
+
this.touchAndPersist();
|
|
372
|
+
return true;
|
|
373
|
+
}
|
|
374
|
+
importWorkspaceJson(rawJson) {
|
|
375
|
+
const parsed = safeJsonParse(rawJson);
|
|
376
|
+
if (!parsed || typeof parsed !== 'object') {
|
|
377
|
+
throw new Error('Invalid DevAnvil workspace JSON');
|
|
378
|
+
}
|
|
379
|
+
const envs = Array.isArray(parsed.environments)
|
|
380
|
+
? parsed.environments
|
|
381
|
+
.slice(0, MAX_ENVIRONMENTS)
|
|
382
|
+
.map((e, i) => ({
|
|
383
|
+
id: typeof e.id === 'string' && e.id ? e.id : `env_${i}`,
|
|
384
|
+
name: typeof e.name === 'string' && e.name ? e.name : `Env ${i + 1}`,
|
|
385
|
+
variables: sanitizeVarMap(e.variables),
|
|
386
|
+
}))
|
|
387
|
+
: this.data.environments;
|
|
388
|
+
const cols = Array.isArray(parsed.collections)
|
|
389
|
+
? parsed.collections
|
|
390
|
+
.slice(0, MAX_COLLECTIONS)
|
|
391
|
+
.map((c, i) => ({
|
|
392
|
+
id: typeof c.id === 'string' && c.id ? c.id : `col_${i}`,
|
|
393
|
+
name: typeof c.name === 'string' && c.name ? c.name : `Collection ${i + 1}`,
|
|
394
|
+
createdAt: typeof c.createdAt === 'string' ? c.createdAt : new Date().toISOString(),
|
|
395
|
+
requests: Array.isArray(c.requests)
|
|
396
|
+
? c.requests.slice(0, MAX_REQUESTS_PER_COLLECTION)
|
|
397
|
+
: [],
|
|
398
|
+
}))
|
|
399
|
+
: this.data.collections;
|
|
400
|
+
const mockRoutes = Array.isArray(parsed.mockRoutes)
|
|
401
|
+
? parsed.mockRoutes.slice(0, 100)
|
|
402
|
+
: [];
|
|
403
|
+
this.data = {
|
|
404
|
+
schemaVersion: 1,
|
|
405
|
+
name: typeof parsed.name === 'string' && parsed.name.trim() ? parsed.name.trim() : this.data.name,
|
|
406
|
+
activeEnvironmentId: typeof parsed.activeEnvironmentId === 'string' && envs.some((e) => e.id === parsed.activeEnvironmentId)
|
|
407
|
+
? parsed.activeEnvironmentId
|
|
408
|
+
: (envs[0]?.id ?? 'env_local'),
|
|
409
|
+
environments: envs.length > 0 ? envs : this.data.environments,
|
|
410
|
+
collections: cols,
|
|
411
|
+
mockRoutes,
|
|
412
|
+
updatedAt: new Date().toISOString(),
|
|
413
|
+
};
|
|
414
|
+
this.persistToDisk();
|
|
415
|
+
return this.data;
|
|
416
|
+
}
|
|
417
|
+
syncMocksAndPersist(mocks) {
|
|
418
|
+
this.data.mockRoutes = mocks.map((r) => ({
|
|
419
|
+
method: r.method,
|
|
420
|
+
path: r.path,
|
|
421
|
+
status: r.status,
|
|
422
|
+
headers: r.headers,
|
|
423
|
+
body: r.body,
|
|
424
|
+
delayMs: r.delayMs,
|
|
425
|
+
enabled: r.enabled,
|
|
426
|
+
}));
|
|
427
|
+
this.touchAndPersist();
|
|
428
|
+
}
|
|
429
|
+
touchAndPersist() {
|
|
430
|
+
this.data.updatedAt = new Date().toISOString();
|
|
431
|
+
this.persistToDisk();
|
|
432
|
+
}
|
|
433
|
+
loadFromDisk(filePath) {
|
|
434
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
435
|
+
this.importWorkspaceJson(raw);
|
|
436
|
+
}
|
|
437
|
+
persistToDisk() {
|
|
438
|
+
if (!this.filePath)
|
|
439
|
+
return;
|
|
440
|
+
const dir = path.dirname(this.filePath);
|
|
441
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
442
|
+
// Atomic write-temp-then-rename with unpredictable suffix & restricted owner-only permissions
|
|
443
|
+
const suffix = crypto.randomBytes(6).toString('hex');
|
|
444
|
+
const tmpPath = `${this.filePath}.${process.pid}.${suffix}.tmp`;
|
|
445
|
+
fs.writeFileSync(tmpPath, `${JSON.stringify(this.data, null, 2)}\n`, { encoding: 'utf8', mode: 0o600 });
|
|
446
|
+
fs.renameSync(tmpPath, this.filePath);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface WsProbeMessage {
|
|
2
|
+
direction: 'sent' | 'received';
|
|
3
|
+
opcode: 'text' | 'binary' | 'pong' | 'close';
|
|
4
|
+
data: string;
|
|
5
|
+
deltaMs: number;
|
|
6
|
+
}
|
|
7
|
+
export interface WsProbeResult {
|
|
8
|
+
url: string;
|
|
9
|
+
connected: boolean;
|
|
10
|
+
connectMs: number;
|
|
11
|
+
totalMs: number;
|
|
12
|
+
messages: WsProbeMessage[];
|
|
13
|
+
closeCode?: number;
|
|
14
|
+
stoppedReason: 'completed' | 'closed' | 'max_messages' | 'timeout';
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Zero-dependency RFC 6455 WebSocket client tester with full SSRF & DNS-pinning defense.
|
|
18
|
+
* Connects to `ws://` or `wss://`, sends optional test payloads, captures inbound frames
|
|
19
|
+
* with millisecond timing, automatically answers server Ping frames, and closes cleanly.
|
|
20
|
+
*/
|
|
21
|
+
export declare function executeWebSocketProbe(input: {
|
|
22
|
+
url: string;
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
messages?: string[];
|
|
25
|
+
maxMessages?: number;
|
|
26
|
+
maxDurationMs?: number;
|
|
27
|
+
}, options?: {
|
|
28
|
+
allowPrivate?: boolean;
|
|
29
|
+
}): Promise<WsProbeResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Standard minimal GraphQL introspection query used by GraphQL Studio to discover
|
|
32
|
+
* available Query, Mutation, and Subscription fields on any GraphQL endpoint.
|
|
33
|
+
*/
|
|
34
|
+
export declare const GRAPHQL_INTROSPECTION_QUERY = "query DevAnvilIntrospection {\n __schema {\n queryType { name }\n mutationType { name }\n subscriptionType { name }\n types {\n kind\n name\n fields(includeDeprecated: false) {\n name\n args { name type { name kind ofType { name kind } } }\n type { name kind ofType { name kind } }\n }\n }\n }\n}";
|