Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
112 changes: 110 additions & 2 deletions src/commands/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,38 @@ function percentArg(value: unknown, flag: string): number {
return n;
}

/** The longest a tap may wait for its target, as the API allows. */
const MAX_TAP_TIMEOUT_SECONDS = 60;

/** Seconds from a flag value, from 0 to `max`, or a usage error. Undefined when the flag was not given. */
function secondsArg(value: unknown, flag: string, max: number): number | undefined {
if (value === undefined) return undefined;
const n = typeof value === 'number' ? value : Number(value);
if (typeof value === 'boolean' || value === '' || !Number.isFinite(n) || n < 0 || n > max) {
throw new CliError(`--${flag} must be a number of seconds from 0 to ${max}.`, 1);
}
return n;
}

/** Decimal degrees from a flag value, within `limit` either way, or a usage error. */
function degreesArg(value: unknown, flag: string, limit: number): number {
const n = typeof value === 'number' ? value : Number(value);
if (
value === undefined ||
typeof value === 'boolean' ||
(typeof value === 'string' && value.trim() === '') ||
!Number.isFinite(n) ||
n < -limit ||
n > limit
) {
throw new CliError(`--${flag} must be a number from -${limit} to ${limit} (decimal degrees).`, 1);
}
return n;
}

/** A link with a scheme and nothing a shell or a URL would split on. A session id has no colon. */
const LINK = /^[A-Za-z][A-Za-z0-9+.-]*:\S+$/;

/** "x,y" (percentages) from a flag value, or a usage error. */
function pointArg(value: unknown, flag: string): DevicePoint {
const parts = typeof value === 'string' ? value.split(',') : [];
Expand Down Expand Up @@ -782,6 +814,10 @@ const tapSub = defineCommand({
rid: { type: 'string', description: 'The element\'s resource id (rid in `dcd device inspect`)' },
x: { type: 'string', description: 'With --y: a point, as percentages of the screen (0-100)' },
y: { type: 'string', description: 'See --x' },
timeout: {
type: 'string',
description: `With --text or --rid: seconds to wait for it to appear before failing (0-${MAX_TAP_TIMEOUT_SECONDS}, default 17; 0 looks once)`,
},
maestro: maestroArg,
},
async run({ args }) {
Expand All @@ -795,11 +831,16 @@ const tapSub = defineCommand({
if (targets !== 1) {
throw new CliError('tap takes exactly one target: --text, --rid, or --x and --y.', 1);
}
const timeoutSeconds = secondsArg(args.timeout, 'timeout', MAX_TAP_TIMEOUT_SECONDS);
if (timeoutSeconds !== undefined && args.text === undefined && args.rid === undefined) {
throw new CliError('tap --timeout goes with --text or --rid: a point has nothing to wait for.', 1);
}
const wait = timeoutSeconds === undefined ? {} : { timeoutSeconds };
const body: DeviceActionRequest =
args.text !== undefined
? { type: 'tap', text: String(args.text) }
? { type: 'tap', text: String(args.text), ...wait }
: args.rid !== undefined
? { type: 'tap', id: String(args.rid) }
? { type: 'tap', id: String(args.rid), ...wait }
: { type: 'tap', x: percentArg(args.x, 'x'), y: percentArg(args.y, 'y') };
const what =
args.text !== undefined
Expand Down Expand Up @@ -914,6 +955,71 @@ const pressKeySub = defineCommand({
},
});

const openLinkSub = defineCommand({
meta: { name: 'open-link', description: 'Open a link on the device: a deep link into an app, or a web link' },
args: {
...commonArgs,
id: idArg,
// Not `required`, as with `type --text`: a missing link fails inside the envelope.
url: {
type: 'positional',
required: false,
description: 'The link with its scheme, e.g. myapp://path or https://example.com',
},
sensitive: {
type: 'boolean',
default: false,
description: 'The link is a secret (it carries a token): stored encrypted, never shown',
Comment thread
finalerock44 marked this conversation as resolved.
Resolved
},
},
async run({ args }) {
const json = Boolean(args.json);
await runDeviceCommand(json, async () => {
let id = args.id as string | undefined;
let url = args.url as string | undefined;
// `dcd device open-link myapp://path`: the one positional is the link, not a session.
if (id && !url && id.includes(':')) [id, url] = [undefined, id];
if (!url || !LINK.test(url)) {
throw new CliError(
'open-link takes a link with its scheme and no spaces, e.g. myapp://path or https://example.com.',
1,
);
}
const shown = args.sensitive ? 'a link' : url;
return performAction(
args,
id,
{ type: 'open_link', url, ...(args.sensitive ? { sensitive: true } : {}) },
{ done: `Opened ${shown}`, failed: `Opening ${shown} failed` },
);
});
},
});

const setLocationSub = defineCommand({
meta: { name: 'set-location', description: "Set the device's GPS position, in decimal degrees" },
args: {
...commonArgs,
id: idArg,
latitude: { type: 'string', description: 'Decimal degrees, north positive (-90 to 90)' },
longitude: { type: 'string', description: 'Decimal degrees, east positive (-180 to 180)' },
},
async run({ args }) {
const json = Boolean(args.json);
await runDeviceCommand(json, async () => {
// South and west are negative. citty keeps a value that is a number, minus sign and all.
const latitude = degreesArg(args.latitude, 'latitude', 90);
const longitude = degreesArg(args.longitude, 'longitude', 180);
return performAction(
args,
args.id as string | undefined,
{ type: 'set_location', latitude, longitude },
{ done: `Location set to ${latitude}, ${longitude}`, failed: 'Setting the location failed' },
);
});
},
});

const modelsSub = defineCommand({
meta: { name: 'models', description: 'The devices a session can ask for' },
args: {
Expand Down Expand Up @@ -1553,6 +1659,8 @@ export const deviceCommand = defineCommand({
type: typeSub,
swipe: swipeSub,
'press-key': pressKeySub,
'open-link': openLinkSub,
'set-location': setLocationSub,
models: modelsSub,
inspect: inspectSub,
screenshot: screenshotSub,
Expand Down
2 changes: 1 addition & 1 deletion src/gateways/device-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const DeviceGateway = {
return request(baseUrl, auth, 'POST', `${sessionPath(id)}/request-human`, { reason });
},

/** One typed action: tap, type, swipe or press_key. Synchronous: answers with the outcome. */
/** One typed action: tap, type, swipe, press_key, open_link or set_location. Synchronous: answers with the outcome. */
action(
baseUrl: string,
auth: AuthContext,
Expand Down
41 changes: 37 additions & 4 deletions src/services/telemetry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,22 +345,55 @@ const SENSITIVE_FLAG_NAMES = new Set([
'--env',
]);

function scrubArgv(args: string[]): string[] {
const REDACTED = '<redacted>';

/**
* The argv a command was run with, as telemetry may keep it: without the
* values of the flags above, and without what two `dcd device` subcommands
* are given that no flag name marks as a secret.
*
* - `device open-link` takes its link as a positional. A sign-in link is a
* credential, and `--sensitive` promises it is never shown, so everything
* after `open-link` that is not a flag is taken for the link, and the value
* of any flag but the API URL goes too. That costs the session id, and
* keeps a link too malformed to send from being kept either.
* - `device type` takes `--text`, which is a password or a code as often as
* not. `device tap` has a `--text` as well, an element's label, and keeps it.
*
* The subcommand is found by its name anywhere after `device`, not as the
* first positional: a flag's value in between must not hide it.
*/
export function scrubArgv(args: string[]): string[] {
const device = args.indexOf('device');
const subcommand = (name: string) => (device === -1 ? -1 : args.indexOf(name, device + 1));
const openLink = subcommand('open-link');
const type = subcommand('type');
const keeps = (flag: string) => API_URL_FLAGS.includes(flag);

const scrubbed: string[] = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
const isFlag = arg.startsWith('-');
const eqIndex = arg.indexOf('=');
const flagName = eqIndex === -1 ? arg : arg.slice(0, eqIndex);
if (arg.startsWith('-') && SENSITIVE_FLAG_NAMES.has(flagName)) {
const afterOpenLink = openLink !== -1 && i > openLink;
const secretFlag =
SENSITIVE_FLAG_NAMES.has(flagName) ||
(type !== -1 && i > type && flagName === '--text') ||
// `--anything=value` after open-link: no flag of the command needs its value kept.
(afterOpenLink && eqIndex !== -1 && !keeps(flagName));
if (isFlag && secretFlag) {
if (eqIndex === -1) {
scrubbed.push(arg);
if (i + 1 < args.length) {
scrubbed.push('<redacted>');
scrubbed.push(REDACTED);
i++;
}
} else {
scrubbed.push(`${flagName}=<redacted>`);
scrubbed.push(`${flagName}=${REDACTED}`);
}
} else if (afterOpenLink && !isFlag) {
scrubbed.push(keeps(args[i - 1]) ? arg : REDACTED);
} else {
scrubbed.push(arg);
}
Expand Down
15 changes: 13 additions & 2 deletions src/types/domain/device-session.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,16 @@ export interface DevicePoint {
* no longer sends it.
*/
export type DeviceActionRequest =
| { id?: string; mode?: 'maestro'; text?: string; type: 'tap'; x?: number; y?: number }
| {
id?: string;
mode?: 'maestro';
text?: string;
/** With `text` or `id`: seconds to wait for it to appear (0 to 60; 0 looks once). */
timeoutSeconds?: number;
type: 'tap';
x?: number;
y?: number;
}
| { key: string; mode?: 'maestro'; type: 'press_key' }
| { mode?: 'maestro'; sensitive?: boolean; text: string; type: 'type' }
| {
Expand All @@ -139,7 +148,9 @@ export type DeviceActionRequest =
mode?: 'maestro';
to?: DevicePoint;
type: 'swipe';
};
}
| { sensitive?: boolean; type: 'open_link'; url: string }
| { latitude: number; longitude: number; type: 'set_location' };

/** The keys press_key takes, as the API names them. */
export const DEVICE_KEYS = [
Expand Down
Loading
Loading