Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/daemon/__tests__/session-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,25 @@ test('writeSessionLog preserves interaction series flags for click/press/swipe',
assert.match(script, /swipe 10 20 30 40 --count 3 --pause-ms 12 --pattern ping-pong/);
assert.match(script, /fill @e5 "search" --delay-ms 40/);
});

test('writeSessionLog escapes device labels with quotes and backslashes', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-session-log-device-label-'));
const store = new SessionStore(root);
const session = makeSession('default');
session.device.name = 'QA "Lab" \\ Shelf';
store.recordAction(session, {
command: 'open',
positionals: ['Settings'],
flags: { platform: 'ios', saveScript: true },
result: {},
});

store.writeSessionLog(session);
const scriptFile = fs.readdirSync(root).find((file) => file.endsWith('.ad'));
assert.ok(scriptFile);
const script = fs.readFileSync(path.join(root, scriptFile!), 'utf8');
assert.match(
script,
/context platform=ios device="QA \\"Lab\\" \\\\ Shelf" kind=simulator theme=unknown/,
);
});
15 changes: 15 additions & 0 deletions src/daemon/handlers/__tests__/session-replay-script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ test('type replay script preserves literal delay flag tokens', () => {
assert.equal(parsed[0]?.flags.delayMs, undefined);
});

test('writeReplayScript escapes device labels with quotes and backslashes', () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-replay-script-device-label-'));
const replayPath = path.join(root, 'flow.ad');
const session = makeSession();
session.device.name = 'Pixel "QA" \\ Lab';

writeReplayScript(replayPath, [], session);
const script = fs.readFileSync(replayPath, 'utf8');

assert.match(
script,
/context platform=android device="Pixel \\"QA\\" \\\\ Lab" kind=emulator theme=unknown/,
);
});

test('readReplayScriptMetadata extracts platform from context header', () => {
const metadata = readReplayScriptMetadata(
'# comment\n\ncontext platform=android device="Pixel 9 Pro"\nopen "Demo"\n',
Expand Down
11 changes: 8 additions & 3 deletions src/daemon/handlers/session-replay-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ import {
appendScriptSeriesFlags,
formatScriptArgQuoteIfNeeded,
formatScriptArg,
formatScriptStringLiteral,
isClickLikeCommand,
parseReplaySeriesFlags,
parseReplayRuntimeFlags,
} from '../script-utils.ts';

type ReplayScriptPlatform = Exclude<PlatformSelector, 'apple'>;

const REPLAY_METADATA_PLATFORMS = new Set<ReplayScriptPlatform>(['ios', 'android', 'macos', 'linux']);
const REPLAY_METADATA_PLATFORMS = new Set<ReplayScriptPlatform>([
'ios',
'android',
'macos',
'linux',
]);

export type ReplayScriptMetadata = {
platform?: ReplayScriptPlatform;
Expand Down Expand Up @@ -310,11 +316,10 @@ export function writeReplayScript(
// Session can be missing if the replay session is closed/deleted between execution and update write.
// In that case we still persist healed actions and omit only the context header.
if (session) {
const deviceLabel = session.device.name.replace(/"/g, '\\"');
const kind = session.device.kind ? ` kind=${session.device.kind}` : '';
const target = session.device.target ? ` target=${session.device.target}` : '';
lines.push(
`context platform=${session.device.platform}${target} device="${deviceLabel}"${kind} theme=unknown`,
`context platform=${session.device.platform}${target} device=${formatScriptStringLiteral(session.device.name)}${kind} theme=unknown`,
);
}
for (const action of actions) {
Expand Down
4 changes: 4 additions & 0 deletions src/daemon/script-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function formatScriptArg(value: string): string {
return JSON.stringify(trimmed);
}

export function formatScriptStringLiteral(value: string): string {
return JSON.stringify(value);
}

// Preserve readable CLI-ish script output for ordinary tokens while still quoting whitespace.
export function formatScriptArgQuoteIfNeeded(value: string): string {
const trimmed = value.trim();
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
appendScriptSeriesFlags,
formatScriptArgQuoteIfNeeded,
formatScriptArg,
formatScriptStringLiteral,
isClickLikeCommand,
} from './script-utils.ts';
import { emitDiagnostic } from '../utils/diagnostics.ts';
Expand Down Expand Up @@ -285,11 +286,10 @@ function sanitizeFlags(flags: CommandFlags | undefined): SessionAction['flags']

function formatScript(session: SessionState, actions: SessionAction[]): string {
const lines: string[] = [];
const deviceLabel = session.device.name.replace(/"/g, '\\"');
const kind = session.device.kind ? ` kind=${session.device.kind}` : '';
const theme = 'unknown';
lines.push(
`context platform=${session.device.platform} device="${deviceLabel}"${kind} theme=${theme}`,
`context platform=${session.device.platform} device=${formatScriptStringLiteral(session.device.name)}${kind} theme=${theme}`,
);
for (const action of actions) {
if (action.flags?.noRecord) continue;
Expand Down
11 changes: 10 additions & 1 deletion src/utils/__tests__/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { runCmd } from '../exec.ts';
import { runCmd, whichCmd } from '../exec.ts';

test('runCmd enforces timeoutMs and rejects with COMMAND_FAILED', async () => {
await assert.rejects(
Expand All @@ -16,3 +16,12 @@ test('runCmd enforces timeoutMs and rejects with COMMAND_FAILED', async () => {
},
);
});

test('whichCmd resolves absolute executable paths without invoking a shell', async () => {
assert.equal(await whichCmd(process.execPath), true);
});

test('whichCmd rejects suspicious command strings', async () => {
assert.equal(await whichCmd('node; rm -rf /'), false);
assert.equal(await whichCmd('./node'), false);
});
Loading
Loading