-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Expand file tree
/
Copy pathenv-sanitizer.test.ts
More file actions
231 lines (199 loc) · 7.88 KB
/
Copy pathenv-sanitizer.test.ts
File metadata and controls
231 lines (199 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'bun:test';
import { sanitizeEnv } from '../../src/supervisor/env-sanitizer.js';
describe('sanitizeEnv', () => {
it('strips variables with CLAUDECODE_ prefix', () => {
const result = sanitizeEnv({
CLAUDECODE_FOO: 'bar',
CLAUDECODE_SOMETHING: 'value',
PATH: '/usr/bin'
});
expect(result.CLAUDECODE_FOO).toBeUndefined();
expect(result.CLAUDECODE_SOMETHING).toBeUndefined();
expect(result.PATH).toBe('/usr/bin');
});
it('strips variables with CLAUDE_CODE_ prefix but preserves allowed ones', () => {
const result = sanitizeEnv({
CLAUDE_CODE_BAR: 'baz',
CLAUDE_CODE_OAUTH_TOKEN: 'token',
HOME: '/home/user'
});
expect(result.CLAUDE_CODE_BAR).toBeUndefined();
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('token');
expect(result.HOME).toBe('/home/user');
});
it('preserves CLAUDE_CODE_SKIP_BEDROCK_AUTH', () => {
const result = sanitizeEnv({
CLAUDE_CODE_USE_BEDROCK: '1',
CLAUDE_CODE_SKIP_BEDROCK_AUTH: '1',
PATH: '/usr/bin'
});
expect(result.CLAUDE_CODE_USE_BEDROCK).toBe('1');
expect(result.CLAUDE_CODE_SKIP_BEDROCK_AUTH).toBe('1');
expect(result.PATH).toBe('/usr/bin');
});
it('preserves CLAUDE_CODE_SKIP_VERTEX_AUTH', () => {
const result = sanitizeEnv({
CLAUDE_CODE_USE_VERTEX: '1',
CLAUDE_CODE_SKIP_VERTEX_AUTH: '1',
PATH: '/usr/bin'
});
expect(result.CLAUDE_CODE_USE_VERTEX).toBe('1');
expect(result.CLAUDE_CODE_SKIP_VERTEX_AUTH).toBe('1');
expect(result.PATH).toBe('/usr/bin');
});
it('keeps Bedrock and Vertex skip-auth flags in shipped runtime bundles', () => {
const workerBundle = readFileSync(
new URL('../../plugin/scripts/worker-service.cjs', import.meta.url),
'utf-8'
);
const serverBetaBundle = readFileSync(
new URL('../../plugin/scripts/server-beta-service.cjs', import.meta.url),
'utf-8'
);
const mcpServerBundle = readFileSync(
new URL('../../plugin/scripts/mcp-server.cjs', import.meta.url),
'utf-8'
);
expect(workerBundle).toContain('CLAUDE_CODE_SKIP_BEDROCK_AUTH');
expect(workerBundle).toContain('CLAUDE_CODE_SKIP_VERTEX_AUTH');
expect(serverBetaBundle).toContain('CLAUDE_CODE_SKIP_BEDROCK_AUTH');
expect(serverBetaBundle).toContain('CLAUDE_CODE_SKIP_VERTEX_AUTH');
expect(mcpServerBundle).toContain('CLAUDE_CODE_SKIP_BEDROCK_AUTH');
expect(mcpServerBundle).toContain('CLAUDE_CODE_SKIP_VERTEX_AUTH');
});
it('strips exact-match variables (CLAUDECODE, CLAUDE_CODE_SESSION, CLAUDE_CODE_ENTRYPOINT, MCP_SESSION_ID)', () => {
const result = sanitizeEnv({
CLAUDECODE: '1',
CLAUDE_CODE_SESSION: 'session-123',
CLAUDE_CODE_ENTRYPOINT: 'hook',
MCP_SESSION_ID: 'mcp-abc',
NODE_PATH: '/usr/local/lib'
});
expect(result.CLAUDECODE).toBeUndefined();
expect(result.CLAUDE_CODE_SESSION).toBeUndefined();
expect(result.CLAUDE_CODE_ENTRYPOINT).toBeUndefined();
expect(result.MCP_SESSION_ID).toBeUndefined();
expect(result.NODE_PATH).toBe('/usr/local/lib');
});
it('preserves allowed variables like PATH, HOME, NODE_PATH', () => {
const result = sanitizeEnv({
PATH: '/usr/bin:/usr/local/bin',
HOME: '/home/user',
NODE_PATH: '/usr/local/lib/node_modules',
SHELL: '/bin/zsh',
USER: 'developer',
LANG: 'en_US.UTF-8'
});
expect(result.PATH).toBe('/usr/bin:/usr/local/bin');
expect(result.HOME).toBe('/home/user');
expect(result.NODE_PATH).toBe('/usr/local/lib/node_modules');
expect(result.SHELL).toBe('/bin/zsh');
expect(result.USER).toBe('developer');
expect(result.LANG).toBe('en_US.UTF-8');
});
it('returns a new object and does not mutate the original', () => {
const original: NodeJS.ProcessEnv = {
PATH: '/usr/bin',
CLAUDECODE_FOO: 'bar',
KEEP: 'yes'
};
const originalCopy = { ...original };
const result = sanitizeEnv(original);
expect(result).not.toBe(original);
expect(original).toEqual(originalCopy);
expect(result.CLAUDECODE_FOO).toBeUndefined();
expect(result.PATH).toBe('/usr/bin');
});
it('handles empty env gracefully', () => {
const result = sanitizeEnv({});
expect(result).toEqual({});
});
it('skips entries with undefined values', () => {
const env: NodeJS.ProcessEnv = {
DEFINED: 'value',
UNDEFINED_KEY: undefined
};
const result = sanitizeEnv(env);
expect(result.DEFINED).toBe('value');
expect('UNDEFINED_KEY' in result).toBe(false);
});
it('combines prefix and exact match removal in a single pass', () => {
const result = sanitizeEnv({
PATH: '/usr/bin',
CLAUDECODE: '1',
CLAUDECODE_FOO: 'bar',
CLAUDE_CODE_BAR: 'baz',
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token',
CLAUDE_CODE_SESSION: 'session',
CLAUDE_CODE_ENTRYPOINT: 'entry',
MCP_SESSION_ID: 'mcp',
KEEP_ME: 'yes'
});
expect(result.PATH).toBe('/usr/bin');
expect(result.KEEP_ME).toBe('yes');
expect(result.CLAUDECODE).toBeUndefined();
expect(result.CLAUDECODE_FOO).toBeUndefined();
expect(result.CLAUDE_CODE_BAR).toBeUndefined();
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('oauth-token');
expect(result.CLAUDE_CODE_SESSION).toBeUndefined();
expect(result.CLAUDE_CODE_ENTRYPOINT).toBeUndefined();
expect(result.MCP_SESSION_ID).toBeUndefined();
});
it('preserves CLAUDE_CODE_GIT_BASH_PATH through sanitization', () => {
const result = sanitizeEnv({
CLAUDE_CODE_GIT_BASH_PATH: 'C:\\Program Files\\Git\\bin\\bash.exe',
PATH: '/usr/bin',
HOME: '/home/user'
});
expect(result.CLAUDE_CODE_GIT_BASH_PATH).toBe('C:\\Program Files\\Git\\bin\\bash.exe');
expect(result.PATH).toBe('/usr/bin');
expect(result.HOME).toBe('/home/user');
});
it('strips proxy env vars (uppercase and lowercase) so the worker subprocess is not routed through the user proxy', () => {
const result = sanitizeEnv({
HTTP_PROXY: 'http://bad-proxy:1234',
HTTPS_PROXY: 'http://bad-proxy:1234',
ALL_PROXY: 'socks5://bad-proxy:1080',
NO_PROXY: 'localhost,127.0.0.1',
http_proxy: 'http://bad-proxy:1234',
https_proxy: 'http://bad-proxy:1234',
all_proxy: 'socks5://bad-proxy:1080',
no_proxy: 'localhost,127.0.0.1',
npm_config_proxy: 'http://bad-proxy:1234',
npm_config_https_proxy: 'http://bad-proxy:1234',
PATH: '/usr/bin'
});
expect(result.HTTP_PROXY).toBeUndefined();
expect(result.HTTPS_PROXY).toBeUndefined();
expect(result.ALL_PROXY).toBeUndefined();
expect(result.NO_PROXY).toBeUndefined();
expect(result.http_proxy).toBeUndefined();
expect(result.https_proxy).toBeUndefined();
expect(result.all_proxy).toBeUndefined();
expect(result.no_proxy).toBeUndefined();
expect(result.npm_config_proxy).toBeUndefined();
expect(result.npm_config_https_proxy).toBeUndefined();
expect(result.PATH).toBe('/usr/bin');
});
it('selectively preserves only allowed CLAUDE_CODE_* vars while stripping others', () => {
const result = sanitizeEnv({
CLAUDE_CODE_OAUTH_TOKEN: 'my-oauth-token',
CLAUDE_CODE_GIT_BASH_PATH: '/usr/bin/bash',
CLAUDE_CODE_USE_BEDROCK: '1',
CLAUDE_CODE_SKIP_BEDROCK_AUTH: '1',
CLAUDE_CODE_SKIP_VERTEX_AUTH: '0',
CLAUDE_CODE_RANDOM_OTHER: 'should-be-stripped',
CLAUDE_CODE_INTERNAL_FLAG: 'should-be-stripped',
PATH: '/usr/bin'
});
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('my-oauth-token');
expect(result.CLAUDE_CODE_GIT_BASH_PATH).toBe('/usr/bin/bash');
expect(result.CLAUDE_CODE_USE_BEDROCK).toBe('1');
expect(result.CLAUDE_CODE_SKIP_BEDROCK_AUTH).toBe('1');
expect(result.CLAUDE_CODE_SKIP_VERTEX_AUTH).toBe('0');
expect(result.CLAUDE_CODE_RANDOM_OTHER).toBeUndefined();
expect(result.CLAUDE_CODE_INTERNAL_FLAG).toBeUndefined();
expect(result.PATH).toBe('/usr/bin');
});
});