dd-native-iast-rewriter-js
Nodejs native AST rewriter heavily based on Speedy Web Compiler o SWC compiler used to instrument Javascript source files.
Workflow
- Parse Javascript code to obtain the AST
- Replace certain AST expressions -> Currently it is focused in certain operators like
+
and +=
, template literals and some String methods - Generate the new Javascript code as from the modified AST -> In addition to the Javascript code, the corresponding source map is returned chaining it with the original source map if necessary.
Usage
const Rewriter = require('@datadog/native-iast-rewriter')
const rewriter = new Rewriter(rewriterConfig)
const result = rewriter.rewrite(code, filename)
Configuration options
RewriterConfig {
chainSourceMap?: boolean
comments?: boolean
localVarPrefix?: string
csiMethods?: Array<CsiMethod>
literals?: boolean
}
CsiMethod {
src: string
dst?: string
operator?: boolean
}
Example
const Rewriter = require('@datadog/native-iast-rewriter')
const rewriterConfig = {
csiMethods: [{ src: 'substring' }],
localVarPrefix: 'test',
}
const rewriter = new Rewriter(rewriterConfig)
const code = `function sub(a) {
return a.substring(1)
}`
const result = rewriter.rewrite(code, filename)
console.log(result.content)
Local setup
To set up the project locally, you should install cargo
and wasm-pack
:
$ curl https://sh.rustup.rs -sSf | sh
$ cargo install wasm-pack
and project dependencies:
$ npm install
Build
Build the project with
$ npm run build
It will compile WASM binaries by default
and then it will be possible to run the tests with
$ npm t