Skip to content
Open
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,13 @@ console.log(output);
| <pre>npm install --global httpsnippet</pre> | <pre>yarn global add httpsnippet</pre> |

```text
httpsnippet [harFilePath]
httpsnippet <harFilePaths..>

the default command

Positionals:
harFilePaths HAR input files

Options:
--help Show help [boolean]
--version Show version number [boolean]
Expand All @@ -102,6 +105,12 @@ Examples:
httpsnippet my_har.json --target rust --client actix --output my_src_directory
```

To convert multiple files, pass their paths or let your shell expand a glob:

```shell
httpsnippet ./requests/*.json --target shell --client curl --output ./snippets
```

### Example

The input to HTTPSnippet is any valid [HAR Request Object](http://www.softwareishard.com/blog/har-12-spec/#request), or full [HAR](http://www.softwareishard.com/blog/har-12-spec/#log) log format.
Expand Down
58 changes: 58 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { mkdtempSync, readFileSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import path from 'path';

import { go } from './cli';

const fixtureDir = path.join(__dirname, 'fixtures', 'requests');
const shortHar = path.join(fixtureDir, 'short.json');
const httpsHar = path.join(fixtureDir, 'https.json');

const runCli = (...args: string[]) => {
const originalArgv = process.argv;
process.argv = [process.execPath, 'httpsnippet', ...args];
try {
return go();
} finally {
process.argv = originalArgv;
}
};

describe('command line', () => {
it('writes a snippet for each input file to a new output directory', () => {
const tempDir = mkdtempSync(path.join(tmpdir(), 'httpsnippet-'));
const output = path.join(tempDir, 'snippets');
jest.spyOn(console, 'log').mockImplementation(() => undefined);
jest.spyOn(console, 'error').mockImplementation(() => undefined);
jest.spyOn(process, 'exit').mockImplementation(code => {
throw new Error(`CLI exited with status ${code}`);
});

try {
runCli(shortHar, httpsHar, '--target', 'shell', '--client', 'curl', '--output', output);

expect(readFileSync(path.join(output, 'short.sh'), 'utf8')).toContain(
'http://mockbin.com/har',
);
expect(readFileSync(path.join(output, 'https.sh'), 'utf8')).toContain(
'https://mockbin.com/har',
);
} finally {
jest.restoreAllMocks();
rmSync(tempDir, { recursive: true, force: true });
}
});

it('prints a single input snippet to stdout when no output directory is given', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);

try {
runCli(shortHar, '--target', 'shell');

expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith(expect.stringContaining('http://mockbin.com/har'));
} finally {
log.mockRestore();
}
});
});
60 changes: 34 additions & 26 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import { readFileSync, writeFileSync } from 'fs';
import { mkdirSync, readFileSync, writeFileSync } from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
Expand All @@ -19,18 +19,22 @@ interface CliOptions {
target: TargetId;
client?: ClientId;
output?: string;
harFilePath: string;
harFilePaths: string[];
options?: any;
}

export const go = () =>
yargs(hideBin(process.argv))
.version(packageJson.version)
.command<CliOptions>(
'$0 [harFilePath]',
'$0 <harFilePaths..>',
'the default command',
builder => {
builder
.positional('harFilePaths', {
type: 'string',
describe: 'HAR input files',
})
.option('target', {
alias: 't',
type: 'string',
Expand Down Expand Up @@ -59,42 +63,46 @@ export const go = () =>
.showHelpOnFail(true)
.help();
},
({ target: targetId, client, output, options, harFilePath }) => {
const har = JSON.parse(readFileSync(harFilePath).toString()) as HarRequest;
const httpsnippet = new HTTPSnippet(har);

({ target: targetId, client, output, options, harFilePaths }) => {
try {
if (options) {
options = JSON.parse(options);
}
} catch (error) {
if (error instanceof Error) {
bad(`${cyan.bold(harFilePath)} failed to read JSON: ${red(error.message)}`);
bad(`failed to parse --options as JSON: ${red(error.message)}`);
}
return;
}

const result = httpsnippet.convert(targetId, client, options);

if (!result) {
throw new Error('something went wrong');
if (output) {
mkdirSync(output, { recursive: true });
}

if (!output) {
console.log(result);
return;
}
for (const harFilePath of harFilePaths) {
const har = JSON.parse(readFileSync(harFilePath, 'utf8')) as HarRequest;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

const result = new HTTPSnippet(har).convert(targetId, client, options);

const file = path.parse(harFilePath).name;
const writeFilePath = path.join(output, `${file}${extname(targetId)}`);
writeFileSync(writeFilePath, String(result));
const target = targets[targetId];
const clientId = target.clientsById[client || target.info.default].info.key;
good(
`converted ${cyan.bold(harFilePath)} with ${yellow(targetId)}[${yellow(
clientId,
)}] at ${cyan(writeFilePath)}\n\n${result}`,
);
if (!result) {
throw new Error('something went wrong');
}

if (!output) {
console.log(result);
continue;
}

const file = path.parse(harFilePath).name;
const writeFilePath = path.join(output, `${file}${extname(targetId)}`);
writeFileSync(writeFilePath, String(result));
const target = targets[targetId];
const clientId = target.clientsById[client || target.info.default].info.key;
good(
`converted ${cyan.bold(harFilePath)} with ${yellow(targetId)}[${yellow(
clientId,
)}] at ${cyan(writeFilePath)}\n\n${result}`,
);
}
},
)
.example('$0 my_har.json', '--target rust --client actix --output my_src_directory').argv;