-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathexpo-upload-sourcemaps.test.ts
More file actions
313 lines (263 loc) · 10.3 KB
/
expo-upload-sourcemaps.test.ts
File metadata and controls
313 lines (263 loc) · 10.3 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { spawnSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
const SCRIPTS_DIR = path.resolve(__dirname, '../../scripts');
const EXPO_UPLOAD_SCRIPT = path.join(SCRIPTS_DIR, 'expo-upload-sourcemaps.js');
describe('expo-upload-sourcemaps.js', () => {
let tempDir: string;
let outputDir: string;
let mockSentryCliScript: string;
beforeEach(() => {
// Create temporary directories for test artifacts
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'expo-upload-test-'));
outputDir = path.join(tempDir, 'dist');
fs.mkdirSync(outputDir, { recursive: true });
// Create a mock sentry-cli script
mockSentryCliScript = path.join(tempDir, 'mock-sentry-cli');
const mockCliContent = `#!/usr/bin/env node
const args = process.argv.slice(2);
const output = process.env.MOCK_CLI_OUTPUT || 'Mock upload successful';
// Echo the arguments to verify they're passed correctly
console.log('Arguments received:', JSON.stringify(args));
console.log(output);
const exitCode = parseInt(process.env.MOCK_CLI_EXIT_CODE || '0');
process.exit(exitCode);
`;
fs.writeFileSync(mockSentryCliScript, mockCliContent);
fs.chmodSync(mockSentryCliScript, '755');
});
afterEach(() => {
// Clean up temp directory
if (tempDir && fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
const createAssets = (filenames: string[]) => {
filenames.forEach(filename => {
const filePath = path.join(outputDir, filename);
if (filename.endsWith('.map')) {
// Create a valid sourcemap with debug_id
fs.writeFileSync(
filePath,
JSON.stringify({
version: 3,
sources: ['index.js'],
names: [],
mappings: 'AAAA',
debugId: 'test-debug-id-123',
}),
);
} else {
// Create a simple JS file
fs.writeFileSync(filePath, '// Mock bundle file');
}
});
};
const runScript = (env: Record<string, string> = {}): { stdout: string; stderr: string; exitCode: number } => {
const defaultEnv = {
SENTRY_ORG: 'test-org',
SENTRY_PROJECT: 'test-project',
SENTRY_URL: 'https://sentry.io/',
SENTRY_AUTH_TOKEN: 'test-token',
SENTRY_CLI_EXECUTABLE: mockSentryCliScript,
// Skip expo config loading
EXPO_PUBLIC_SKIP_CONFIG: 'true',
};
try {
const result = spawnSync(process.execPath, [EXPO_UPLOAD_SCRIPT, outputDir], {
env: { ...process.env, ...defaultEnv, ...env },
encoding: 'utf8',
});
return {
stdout: result.stdout || '',
stderr: result.stderr || '',
exitCode: result.status || 0,
};
} catch (error: any) {
return {
stdout: error.stdout?.toString() || '',
stderr: error.stderr?.toString() || '',
exitCode: error.status || 1,
};
}
};
describe('basic functionality', () => {
it('successfully uploads normal bundles and sourcemaps', () => {
createAssets(['bundle.js', 'bundle.js.map']);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
MOCK_CLI_OUTPUT: 'Upload successful',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Upload successful');
expect(result.stdout).toContain('Uploaded bundles and sourcemaps to Sentry successfully');
});
it('handles multiple bundles', () => {
createAssets(['bundle1.js', 'bundle1.js.map', 'bundle2.js', 'bundle2.js.map']);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Uploaded bundles and sourcemaps to Sentry successfully');
});
it('skips bundles without sourcemaps', () => {
createAssets(['bundle.js']); // No .map file
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Sourcemap for');
expect(result.stdout).toContain('not found, skipping');
});
it('exits with error when sentry-cli fails', () => {
createAssets(['bundle.js', 'bundle.js.map']);
const result = runScript({
MOCK_CLI_EXIT_CODE: '1',
MOCK_CLI_OUTPUT: 'Upload failed: Network error',
});
expect(result.exitCode).toBe(1);
expect(result.stdout).toContain('Upload failed: Network error');
});
});
describe('security: command injection prevention', () => {
it('safely handles filenames with spaces (simulating special characters)', () => {
// Use spaces to simulate special character handling
// (semicolons, pipes can't be created on most filesystems)
const fileWithSpaces = 'bundle with spaces.js';
const mapWithSpaces = 'bundle with spaces.js.map';
createAssets([fileWithSpaces, mapWithSpaces]);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
// Should complete successfully with proper argument passing
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Arguments received:');
const argsMatch = result.stdout.match(/Arguments received: (.+)/);
if (argsMatch) {
const args = JSON.parse(argsMatch[1]);
// Verify filename with spaces is passed as single argument
const fileArg = args.find((arg: string) => arg.includes('bundle with spaces'));
expect(fileArg).toBeDefined();
expect(fileArg).toContain('bundle with spaces.js');
}
});
it('safely handles filenames with shell metacharacters via spawnSync', () => {
// spawnSync doesn't use a shell, so no special character escaping is needed
// Test with parentheses which are valid in filenames but special to shell
const fileWithParens = 'bundle(test).js';
const mapWithParens = 'bundle(test).js.map';
createAssets([fileWithParens, mapWithParens]);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('Arguments received:');
// Verify parentheses are preserved
const argsMatch = result.stdout.match(/Arguments received: (.+)/);
if (argsMatch) {
const args = JSON.parse(argsMatch[1]);
const fileArg = args.find((arg: string) => arg.includes('(test)'));
expect(fileArg).toBeDefined();
}
});
it('demonstrates shell safety: no command interpretation possible', () => {
// This test verifies that spawnSync passes arguments directly
// without shell interpretation, making command injection impossible
const normalFile = 'bundle-safe.js';
const normalMap = 'bundle-safe.js.map';
createAssets([normalFile, normalMap]);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
// Verify arguments are passed as an array
const argsMatch = result.stdout.match(/Arguments received: (.+)/);
if (argsMatch) {
const args = JSON.parse(argsMatch[1]);
// First arg should be the subcommand
expect(args[0]).toBe('sourcemaps');
expect(args[1]).toBe('upload');
// Remaining args should be file paths
expect(args.length).toBeGreaterThanOrEqual(2);
}
});
});
describe('Hermes support', () => {
it('passes --debug-id-reference flag for Hermes bundles', () => {
// For Hermes, we need the .hbc and .hbc.map files
createAssets(['bundle.hbc', 'bundle.hbc.map']);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
// Check that the flag is in the arguments
const argsMatch = result.stdout.match(/Arguments received: (.+)/);
if (argsMatch) {
const args = JSON.parse(argsMatch[1]);
expect(args).toContain('--debug-id-reference');
}
});
it('does not pass --debug-id-reference flag for non-Hermes bundles', () => {
createAssets(['bundle.js', 'bundle.js.map']);
const result = runScript({
MOCK_CLI_EXIT_CODE: '0',
});
expect(result.exitCode).toBe(0);
const argsMatch = result.stdout.match(/Arguments received: (.+)/);
if (argsMatch) {
const args = JSON.parse(argsMatch[1]);
expect(args).not.toContain('--debug-id-reference');
}
});
});
describe('environment variables', () => {
it('requires SENTRY_AUTH_TOKEN', () => {
createAssets(['bundle.js', 'bundle.js.map']);
const result = spawnSync(process.execPath, [EXPO_UPLOAD_SCRIPT, outputDir], {
env: {
...process.env,
SENTRY_ORG: 'test-org',
SENTRY_PROJECT: 'test-project',
SENTRY_URL: 'https://sentry.io/',
SENTRY_CLI_EXECUTABLE: mockSentryCliScript,
// Explicitly unset SENTRY_AUTH_TOKEN
SENTRY_AUTH_TOKEN: undefined,
},
encoding: 'utf8',
});
expect(result.status).toBe(1);
expect(result.stdout || result.stderr).toContain('SENTRY_AUTH_TOKEN environment variable must be set');
});
it('requires output directory argument', () => {
const result = spawnSync(process.execPath, [EXPO_UPLOAD_SCRIPT], {
env: {
...process.env,
SENTRY_ORG: 'test-org',
SENTRY_PROJECT: 'test-project',
SENTRY_URL: 'https://sentry.io/',
SENTRY_AUTH_TOKEN: 'test-token',
SENTRY_CLI_EXECUTABLE: mockSentryCliScript,
},
encoding: 'utf8',
timeout: 5000, // Add timeout in case expo config hangs
});
expect(result.status).toBe(1);
const output = result.stdout + result.stderr;
expect(output).toContain('Provide the directory with your bundles and sourcemaps');
});
});
describe('sourcemap processing', () => {
it('converts debugId to debug_id in sourcemaps', () => {
createAssets(['bundle.js', 'bundle.js.map']);
runScript({
MOCK_CLI_EXIT_CODE: '0',
});
// Read the sourcemap file to verify it was updated
const sourceMapPath = path.join(outputDir, 'bundle.js.map');
const sourceMapContent = JSON.parse(fs.readFileSync(sourceMapPath, 'utf8'));
expect(sourceMapContent.debug_id).toBe('test-debug-id-123');
expect(sourceMapContent.debugId).toBe('test-debug-id-123');
});
});
});