-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathlog_capture_escape.test.ts
More file actions
193 lines (168 loc) · 6.7 KB
/
log_capture_escape.test.ts
File metadata and controls
193 lines (168 loc) · 6.7 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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { activeLogSessions, startLogCapture } from '../log_capture.ts';
import type { CommandExecutor } from '../CommandExecutor.ts';
import type { FileSystemExecutor } from '../FileSystemExecutor.ts';
import { Writable } from 'stream';
type CallHistoryEntry = {
command: string[];
logPrefix?: string;
useShell?: boolean;
opts?: { env?: Record<string, string>; cwd?: string };
detached?: boolean;
};
function createMockExecutorWithCalls(callHistory: CallHistoryEntry[]): CommandExecutor {
const mockProcess = {
pid: 12345,
stdout: null,
stderr: null,
killed: false,
exitCode: null,
on: () => mockProcess,
};
return async (command, logPrefix, useShell, opts, detached) => {
callHistory.push({ command, logPrefix, useShell, opts, detached });
return { success: true, output: '', process: mockProcess as any };
};
}
type InMemoryFileRecord = { content: string; mtimeMs: number };
function createInMemoryFileSystemExecutor(): FileSystemExecutor {
const files = new Map<string, InMemoryFileRecord>();
const tempDir = '/virtual/tmp';
return {
mkdir: async () => {},
readFile: async (path) => {
const record = files.get(path);
if (!record) throw new Error(`Missing file: ${path}`);
return record.content;
},
writeFile: async (path, content) => {
files.set(path, { content, mtimeMs: Date.now() });
},
createWriteStream: (path) => {
const chunks: Buffer[] = [];
const stream = new Writable({
write(chunk, _encoding, callback) {
chunks.push(Buffer.from(chunk));
callback();
},
final(callback) {
const existing = files.get(path)?.content ?? '';
files.set(path, {
content: existing + Buffer.concat(chunks).toString('utf8'),
mtimeMs: Date.now(),
});
callback();
},
});
return stream as unknown as ReturnType<FileSystemExecutor['createWriteStream']>;
},
cp: async () => {},
readdir: async (dir) => {
const prefix = `${dir}/`;
return Array.from(files.keys())
.filter((fp) => fp.startsWith(prefix))
.map((fp) => fp.slice(prefix.length));
},
stat: async (path) => {
const record = files.get(path);
if (!record) throw new Error(`Missing file: ${path}`);
return { isDirectory: () => false, mtimeMs: record.mtimeMs };
},
rm: async (path) => {
files.delete(path);
},
existsSync: (path) => files.has(path),
mkdtemp: async (prefix) => `${tempDir}/${prefix}mock-temp`,
tmpdir: () => tempDir,
};
}
beforeEach(() => {
activeLogSessions.clear();
});
afterEach(() => {
activeLogSessions.clear();
});
describe('NSPredicate injection protection (escapePredicateString)', () => {
it('escapes double quotes in bundleId so they cannot break NSPredicate', async () => {
const callHistory: CallHistoryEntry[] = [];
const executor = createMockExecutorWithCalls(callHistory);
const fileSystem = createInMemoryFileSystemExecutor();
// Malicious bundleId containing a double-quote to break out of predicate
const maliciousBundleId = 'io.evil" OR 1==1 OR subsystem == "x';
await startLogCapture(
{ simulatorUuid: 'sim-uuid', bundleId: maliciousBundleId, subsystemFilter: 'app' },
executor,
fileSystem,
);
expect(callHistory).toHaveLength(1);
const predicateIndex = callHistory[0].command.indexOf('--predicate');
expect(predicateIndex).toBeGreaterThan(-1);
const predicate = callHistory[0].command[predicateIndex + 1];
// The quotes should be escaped so the predicate is:
// subsystem == "io.evil\" OR 1==1 OR subsystem == \"x"
// NOT broken out to: subsystem == "io.evil" OR 1==1 OR ...
expect(predicate).toBe('subsystem == "io.evil\\" OR 1==1 OR subsystem == \\"x"');
// Verify the predicate does NOT contain a non-escaped split
expect(predicate.startsWith('subsystem == "')).toBe(true);
expect(predicate.endsWith('"')).toBe(true);
});
it('escapes backslashes in bundleId', async () => {
const callHistory: CallHistoryEntry[] = [];
const executor = createMockExecutorWithCalls(callHistory);
const fileSystem = createInMemoryFileSystemExecutor();
await startLogCapture(
{ simulatorUuid: 'sim-uuid', bundleId: 'io.test\\evil', subsystemFilter: 'app' },
executor,
fileSystem,
);
const predicateIndex = callHistory[0].command.indexOf('--predicate');
const predicate = callHistory[0].command[predicateIndex + 1];
expect(predicate).toBe('subsystem == "io.test\\\\evil"');
});
it('escapes double quotes in custom subsystem filter array', async () => {
const callHistory: CallHistoryEntry[] = [];
const executor = createMockExecutorWithCalls(callHistory);
const fileSystem = createInMemoryFileSystemExecutor();
await startLogCapture(
{
simulatorUuid: 'sim-uuid',
bundleId: 'io.safe.app',
subsystemFilter: ['com.evil" OR 1==1 OR subsystem == "x'],
},
executor,
fileSystem,
);
const predicateIndex = callHistory[0].command.indexOf('--predicate');
const predicate = callHistory[0].command[predicateIndex + 1];
// Both the safe bundleId and the malicious subsystem should be present
// The malicious one must have escaped quotes
expect(predicate).toContain('subsystem == "io.safe.app"');
expect(predicate).toContain('subsystem == "com.evil\\" OR 1==1 OR subsystem == \\"x"');
});
it('escapes quotes in "swiftui" mode', async () => {
const callHistory: CallHistoryEntry[] = [];
const executor = createMockExecutorWithCalls(callHistory);
const fileSystem = createInMemoryFileSystemExecutor();
await startLogCapture(
{ simulatorUuid: 'sim-uuid', bundleId: 'io.evil"app', subsystemFilter: 'swiftui' },
executor,
fileSystem,
);
const predicateIndex = callHistory[0].command.indexOf('--predicate');
const predicate = callHistory[0].command[predicateIndex + 1];
expect(predicate).toBe('subsystem == "io.evil\\"app" OR subsystem == "com.apple.SwiftUI"');
});
it('normal bundleId without special chars passes through unchanged', async () => {
const callHistory: CallHistoryEntry[] = [];
const executor = createMockExecutorWithCalls(callHistory);
const fileSystem = createInMemoryFileSystemExecutor();
await startLogCapture(
{ simulatorUuid: 'sim-uuid', bundleId: 'io.sentry.app', subsystemFilter: 'app' },
executor,
fileSystem,
);
const predicateIndex = callHistory[0].command.indexOf('--predicate');
const predicate = callHistory[0].command[predicateIndex + 1];
expect(predicate).toBe('subsystem == "io.sentry.app"');
});
});