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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brightdata/cli",
"version": "0.3.5",
"version": "0.3.6",
"description": "Command-line interface for Bright Data. Scrape, search, extract structured data, and automate browsers directly from your terminal.",
"main": "dist/index.js",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/commands/discover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ vi.mock('../../utils/output', ()=>({
print_table: mocks.print_table,
fail: mocks.fail,
dim: mocks.dim,
is_tty: true,
is_tty: ()=>true,
}));

vi.mock('../../utils/polling', ()=>({
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/commands/scraper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ vi.mock('../../utils/output', ()=>({
fail: mocks.fail,
success: mocks.success,
dim: mocks.dim,
is_tty: false,
is_tty: ()=>false,
}));

vi.mock('../../utils/polling', ()=>({
Expand Down
154 changes: 153 additions & 1 deletion src/__tests__/utils/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import {serialize, format_from_ext, print} from '../../utils/output';
import {serialize, format_from_ext, print, print_table} from '../../utils/output';

describe('utils/output.serialize csv', ()=>{
it('serializes array of flat objects as RFC 4180 CSV with header row', ()=>{
Expand Down Expand Up @@ -153,4 +153,156 @@ describe('utils/output.print writes correct format from extension', ()=>{
const content = fs.readFileSync(out, 'utf8');
expect(JSON.parse(content)).toEqual([{a: 1}]);
});
it('-o file preserves terminal escape sequences', ()=>{
const out = make_tmp('.txt');
const content = 'hello\x1b[31mRED\x1b[0m';
print(content, {output: out});
expect(fs.readFileSync(out, 'utf8')).toBe(content);
});
});

describe('utils/output.print terminal sanitization', ()=>{
let stdout_write: ReturnType<typeof vi.spyOn>;
const original_is_tty = Object.getOwnPropertyDescriptor(
process.stdout,
'isTTY',
);
const set_tty = (value: boolean)=>{
Object.defineProperty(process.stdout, 'isTTY', {
configurable: true,
value,
});
};
beforeEach(()=>{
stdout_write = vi.spyOn(process.stdout, 'write')
.mockImplementation(()=>true);
});
afterEach(()=>{
vi.restoreAllMocks();
if (original_is_tty)
{
Object.defineProperty(
process.stdout,
'isTTY',
original_is_tty,
);
}
else
delete (process.stdout as {isTTY?: boolean}).isTTY;
});
it('sanitizes terminal escape sequences on TTY stdout', ()=>{
set_tty(true);
const malicious = 'hello'
+ '\x1b[2J'
+ '\x1b[31mRED\x1b[0m'
+ '\x1b]0;Title-pwn\x07'
+ 'world';

print(malicious);
const output = stdout_write.mock.calls
.map((call: unknown[])=>String(call[0]))
.join('');
expect(output).toBe('helloREDworld\n');
expect(output).not.toContain('\x1b');
expect(output).not.toContain('\x07');
});
it('keeps normal TTY stdout content unchanged', ()=>{
set_tty(true);
print('hello world');
const output = stdout_write.mock.calls
.map((call: unknown[])=>String(call[0]))
.join('');
expect(output).toBe('hello world\n');
});
it('preserves row stdout for non-TTY stdout', ()=>{
set_tty(false);
const content = 'hello\x1b[31mRED\x1b[0m';
print(content);
const output = stdout_write.mock.calls
.map((call: unknown[])=>String(call[0]))
.join('');
expect(output).toBe(content + '\n');
});
it('preserves structured output for non-TTY stdout', ()=>{
set_tty(false);
const data = [{
value: 'hello\x1b[31mRED\x1b[0m',
}];
print(data, {format: 'json'});
const output = stdout_write.mock.calls
.map((call: unknown[])=>String(call[0]))
.join('');
expect(output).toBe(JSON.stringify(data) + '\n');
});
it('removes standalone terminal control characters on TTY', ()=>{
set_tty(true);
print('a\x07b\bcd\ref');
const output = stdout_write.mock.calls
.map((call: unknown[])=>String(call[0]))
.join('');
expect(output).toBe('abcd\nef\n');
expect(output).not.toContain('\x07');
expect(output).not.toContain('\b');
expect(output).not.toContain('\r');
});
});

describe('utils/output.print_table terminal sanitization', ()=>{
const original_is_tty = Object.getOwnPropertyDescriptor(
process.stdout,
'isTTY',
);
const set_tty = (value: boolean)=>{
Object.defineProperty(process.stdout, 'isTTY', {
configurable: true,
value,
});
};
afterEach(()=>{
vi.restoreAllMocks();
if (original_is_tty)
{
Object.defineProperty(
process.stdout,
'isTTY',
original_is_tty,
);
}
else
delete (process.stdout as {isTTY?: boolean}).isTTY;
});
it('sanitizes malicious values passed through print_table', ()=>{
set_tty(true);
const log = vi.spyOn(console, 'log')
.mockImplementation(()=>{});
print_table(
[{
title: 'hello\x1b[31mRED\x1b[0m',
url: 'before\x1b[2Jafter',
}],
['title', 'url'],
);
const output = log.mock.calls
.map((call: unknown[])=>call.map(String).join(' '))
.join('\n');
expect(output).toContain('helloRED');
expect(output).toContain('beforeafter');
expect(output).not.toContain('\x1b[31m');
expect(output).not.toContain('\x1b[2J');
});
it('flattens table cells for non-TTY output', ()=>{
set_tty(false);
const log = vi.spyOn(console, 'log')
.mockImplementation(()=>{});
print_table(
[{title: 'line1\nline2\tvalue'}],
['title'],
);
const output = log.mock.calls
.map((call: unknown[])=>call.map(String).join(' '))
.join('\n');
expect(output).toContain('line1 line2 value');
expect(output).not.toContain('line1\nline2');
expect(output).not.toContain('\t');
});
});
2 changes: 1 addition & 1 deletion src/commands/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ const handle_discover = async(query: string, opts: Discover_opts)=>{
}
const print_opts = {json: opts.json, pretty: opts.pretty,
output: opts.output};
if (opts.json || opts.pretty || opts.output || !is_tty)
if (opts.json || opts.pretty || opts.output || !is_tty())
{
print(response, print_opts);
return;
Expand Down
14 changes: 7 additions & 7 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
} from '../utils/output';
import {start as start_spinner} from '../utils/spinner';

const white = (text: string)=>is_tty ? `\x1b[37m${text}\x1b[0m` : text;
const blue = (text: string)=>is_tty ? `\x1b[34m${text}\x1b[0m` : text;
const white = (text: string)=>is_tty() ? `\x1b[37m${text}\x1b[0m` : text;
const blue = (text: string)=>is_tty() ? `\x1b[34m${text}\x1b[0m` : text;
const BANNER_SPLIT_COL = 62;

type Init_opts = {
Expand Down Expand Up @@ -95,7 +95,7 @@ const prompt_zone = async(
zone_names: string[],
suggested: string|undefined
): Promise<string|undefined>=>{
if (!is_tty)
if (!is_tty())
return suggested;
if (!zone_names.length)
{
Expand Down Expand Up @@ -129,7 +129,7 @@ const prompt_zone = async(

const prompt_default_format = async(current: string|undefined):
Promise<string>=>{
if (!is_tty)
if (!is_tty())
return current ?? 'markdown';
const selected = await select({
message: 'Choose default output format',
Expand Down Expand Up @@ -161,7 +161,7 @@ const resolve_initial_api_key = (flag_key: string|undefined):
const prompt_api_key = async(
initial: string|undefined
): Promise<string|undefined>=>{
if (!is_tty)
if (!is_tty())
return initial;
if (initial)
{
Expand Down Expand Up @@ -236,7 +236,7 @@ const show_quick_start = (
};

const maybe_show_install_hint = async()=>{
if (!is_tty)
if (!is_tty())
return;
const show = await confirm({
message: 'Show global install command?',
Expand Down Expand Up @@ -287,7 +287,7 @@ const handle_init = async(opts: Init_opts)=>{
);
unlocker_zone = pick_best_zone(zone_names, unlocker_zone);
serp_zone = pick_best_zone(zone_names, serp_zone ?? unlocker_zone);
if (is_tty)
if (is_tty())
{
unlocker_zone = await prompt_zone(
'Select default Web Unlocker zone',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const build_heal_envelope = (params: {
const wants_machine_output = (
opts: {json?: boolean; pretty?: boolean; output?: string}
): boolean=>
!!(opts.json || opts.pretty || opts.output) || !is_tty;
!!(opts.json || opts.pretty || opts.output) || !is_tty();

const emit_create_output = (
envelope: Create_envelope,
Expand Down
62 changes: 45 additions & 17 deletions src/utils/output.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import fs from 'fs';
import path from 'path';
import { stripVTControlCharacters } from 'util';

const is_tty = process.stdout.isTTY === true;
const terminal_safe = (val: unknown): string=>
stripVTControlCharacters(String(val))
.replace(/\r\n?/g, '\n')
.replace(/[\x00-\x08\x0B-\x1F\x7F-\x9F]/g, '');

const is_tty = ()=>process.stdout.isTTY === true;

const ansi = (code: string, text: string)=>
is_tty ? `\x1b[${code}m${text}\x1b[0m` : text;
is_tty() ? `\x1b[${code}m${text}\x1b[0m` : text;
Comment on lines 12 to +13

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ansi() decides on colors via process.stdout.isTTY, but success/warn/info/fail write to stderr it might cause 2 bugs:

  • cmd | jq - stdout is a pipe, stderr is still a TTY -> diagnostics lose color.
  • cmd 2>error.log - stdout is a TTY, stderr is a file -> raw \x1b[33m writes in the log file.

Suggested fix:

const is_tty = ()=>process.stdout.isTTY === true;
const is_tty_err = ()=>process.stderr.isTTY === true;

const ansi = (code: string, text: string, tty = is_tty())=>
    tty ? `\x1b[${code}m${text}\x1b[0m` : text;

const green = (s: string, tty?: boolean)=>ansi('32', s, tty ?? is_tty());
const red = (s: string, tty?: boolean)=>ansi('31', s, tty ?? is_tty());
const yellow = (s: string, tty?: boolean)=>ansi('33', s, tty ?? is_tty());
const dim = (s: string, tty?: boolean)=>ansi('2', s, tty ?? is_tty());

const warn = (msg: string)=>
    console.error(yellow(`⚠ ${terminal_safe(msg)}`, is_tty_err()));
// same for success / info / fail

We can skip it for now, it's not a blocker. Maybe it's a case for another request


const green = (s: string)=>ansi('32', s);
const red = (s: string)=>ansi('31', s);
const yellow = (s: string)=>ansi('33', s);
const dim = (s: string)=>ansi('2', s);

const success = (msg: string)=>console.error(green(`✓ ${msg}`));
const warn = (msg: string)=>console.error(yellow(`⚠ ${msg}`));
const info = (msg: string)=>console.error(dim(msg));
const fail = (msg: string)=>{ console.error(red(`✗ ${msg}`));
process.exit(1); };
const success = (msg: string)=>
console.error(green(`✓ ${terminal_safe(msg)}`));
const warn = (msg: string)=>
console.error(yellow(`⚠ ${terminal_safe(msg)}`));
const info = (msg: string)=>
console.error(dim(terminal_safe(msg)));
const fail = (msg: string)=>{
console.error(red(`✗ ${terminal_safe(msg)}`));
process.exit(1);
};

type Output_format = 'markdown'|'json'|'pretty'|'html'|'csv'|'raw';

Expand Down Expand Up @@ -181,26 +192,43 @@ const print = (data: unknown, opts: Print_opts = {})=>{
info(`Output written to ${opts.output}`);
return;
}
if (!is_tty && fmt == 'raw')
if (!is_tty() && fmt == 'raw')
fmt = typeof data == 'string' ? 'raw' : 'json';
process.stdout.write(serialize(data, fmt)+'\n');
const content = serialize(data, fmt);
process.stdout.write(
(is_tty() ? terminal_safe(content) : content) + '\n'
);
};

const print_table = (rows: Record<string, unknown>[], cols: string[])=>{
if (!rows.length)
return;
const widths = cols.map(c=>
Math.max(c.length, ...rows.map(r=>String(r[c] ?? '').length))
const tty = is_tty();
const safe_value = (value: unknown): string=>{
const text = String(value ?? '');
const safe_text = tty ? terminal_safe(text) : text;
return safe_text.replace(/[\n\t]/g, ' ');
};
const safe_cols = cols.map(safe_value);
const safe_rows = rows.map(r=>
cols.map(c=>safe_value(r[c])));
const widths = safe_cols.map((c, i)=>
Math.max(
c.length,
...safe_rows.map(row=>row[i].length),
)
);
const divider = widths.map(w=>'-'.repeat(w)).join('-+-');
const header = cols.map((c, i)=>c.padEnd(widths[i])).join(' | ');
const header = safe_cols.map((c, i)=>
c.padEnd(widths[i])).join(' | ');
console.log(dim(header));
console.log(dim(divider));
for (let i=0; i<rows.length; i++)
console.log(dim(divider));
for (const row of safe_rows)
{
const row = cols.map((c, j)=>String(rows[i][c] ?? '').
padEnd(widths[j]));
console.log(row.join(' | '));
console.log(
row.map((cell, i)=>
cell.padEnd(widths[i])).join(' | ')
);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Spinner = {
};

const start = (msg: string): Spinner=>{
if (!is_tty)
if (!is_tty())
{
process.stderr.write(msg+'\n');
return {stop: ()=>{}};
Expand Down