Skip to content

Commit

Permalink
Small changes to proxy server code.
Browse files Browse the repository at this point in the history
1. allow for AST handler module in esnstrument_cli.js

2. directly pass arguments from proxy.py into esnstrument_cli.js.  This
allows for passing additional arguments besides the analyses.  Now we must
require passing '--analysis' for each analysis (README has been updated).
We still turn relative paths into absolute paths, as before.
  • Loading branch information
msridhar committed May 22, 2015
1 parent 7a64c0c commit b96266f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ commands:

mkdir tmp
cd tmp
mitmdump -q --anticache -s "../scripts/proxy.py ../src/js/sample_analyses/ChainedAnalyses.js ../src/js/runtime/analysisCallbackTemplate.js"
mitmdump -q --anticache -s "../scripts/proxy.py --analysis ../src/js/sample_analyses/ChainedAnalyses.js --analysis ../src/js/runtime/analysisCallbackTemplate.js"

In your browser, the http and https proxy should be set to 127.0.0.1:8080. Now if you load a website in your browser, all JavaScript files associated with
the website will get instrumented on-the-fly.
Expand All @@ -95,7 +95,7 @@ On a mac, proxy can be set and launched automatically by giving the following co

mkdir tmp
cd tmp
../scripts/mitmproxywrapper.py -t -q --anticache -s "../scripts/proxy.py ../src/js/sample_analyses/ChainedAnalyses.js ../src/js/runtime/analysisCallbackTemplate.js"
../scripts/mitmproxywrapper.py -t -q --anticache -s "../scripts/proxy.py --analysis ../src/js/sample_analyses/ChainedAnalyses.js --analysis ../src/js/runtime/analysisCallbackTemplate.js"

The proxy can be disabled by re-executing the last command. The last command enables proxy and starts the mitmproxy if the proxy is not currently enabled.
If the proxy is currently enabled, the command disables the proxy.
Expand Down
13 changes: 6 additions & 7 deletions scripts/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
sys.path.insert(0, JALANGI_HOME+'/scripts')
import sj

analysis = ' --analysis '.join([' ']+ANALYSES)
extraArgs = ' --analysis '.join([' ']+ANALYSES)

def processFile (content, ext):
try:
Expand All @@ -24,7 +24,7 @@ def processFile (content, ext):
print "Storing and instrumenting "+fileName+"."+ext
with open(fileName+"."+ext, "w") as text_file:
text_file.write(content)
sj.execute(sj.INSTRUMENTATION_SCRIPT+' --inlineIID --inlineSource '+analysis+' '+fileName+'.'+ext)
sj.execute(sj.INSTRUMENTATION_SCRIPT+' --inlineIID --inlineSource '+extraArgs+' '+fileName+'.'+ext)
with open (fileName+"_jalangi_."+ext, "r") as text_file:
data = text_file.read()
return data
Expand All @@ -34,12 +34,11 @@ def processFile (content, ext):
return content

def start(context, argv):
global ANALYSES
global analysis
global extraArgs
if len(argv) > 1:
ANALYSES = [os.path.abspath(os.path.join(WORKING_DIR,x)) for x in argv[1:]]
analysis = ' --analysis '.join([' ']+ANALYSES)
print analysis
def mapper(p): return p if p.startswith('--') else os.path.abspath(os.path.join(WORKING_DIR,p))
extraArgs = ' '.join(map(mapper, [x for x in argv[1:]]))
print extraArgs

def response(context, flow):
flow.response.decode()
Expand Down
64 changes: 37 additions & 27 deletions src/js/commands/esnstrument_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,36 @@ if (typeof J$ === 'undefined') {



function rewriteInlineScript(src, metadata) {
//var instname = instUtil.createFilenameForScript(metadata.url);
//var origname = createOrigScriptFilename(instname);

var origname = md5(src)+".js";
var instname = makeInstCodeFileName(origname);

try {
var instCodeAndData = instrumentCode(
{
code: src,
isEval: false,
origCodeFileName: sanitizePath(origname),
instCodeFileName: sanitizePath(instname),
inlineSourceMap: inlineIID,
inlineSource: inlineSource,
url: url
});

} catch (e) {
console.log(src);
throw e;
function rewriteInlineScript(astHandler) {
return function (src, metadata) {
//var instname = instUtil.createFilenameForScript(metadata.url);
//var origname = createOrigScriptFilename(instname);

var origname = md5(src)+".js";
var instname = makeInstCodeFileName(origname), instCodeAndData;

try {
instCodeAndData = instrumentCode(
{
code: src,
isEval: false,
origCodeFileName: sanitizePath(origname),
instCodeFileName: sanitizePath(instname),
inlineSourceMap: inlineIID,
inlineSource: inlineSource,
url: url
});

} catch (e) {
console.log(src);
throw e;
}
instUtil.applyASTHandler(instCodeAndData, astHandler, sandbox);
fs.writeFileSync(path.join(outDir, origname), src, "utf8");
fs.writeFileSync(makeSMapFileName(path.join(outDir, instname)), instCodeAndData.sourceMapString, "utf8");
fs.writeFileSync(path.join(outDir, instname), instCodeAndData.code, "utf8");
return instCodeAndData.code;
}
fs.writeFileSync(path.join(outDir, origname), src, "utf8");
fs.writeFileSync(makeSMapFileName(path.join(outDir, instname)), instCodeAndData.sourceMapString, "utf8");
fs.writeFileSync(path.join(outDir, instname), instCodeAndData.code, "utf8");
return instCodeAndData.code;
}

function getJalangiRoot() {
Expand Down Expand Up @@ -158,6 +161,7 @@ if (typeof J$ === 'undefined') {
parser.addArgument(['--inlineSource'], {help: "Inline original source as string in J$.iids.code in the instrumented file", action: 'storeTrue'});
parser.addArgument(['--initParam'], { help: "initialization parameter for analysis, specified as key:value", action:'append'});
parser.addArgument(['--noResultsGUI'], { help: "disable insertion of results GUI code in HTML", action:'storeTrue'});
parser.addArgument(['--astHandlerModule'], {help: "Path to a node module that exports a function to be used for additional AST handling after instrumentation"});
parser.addArgument(['--outDir'], {
help: "Directory containing scripts inlined in html",
defaultValue: process.cwd()
Expand All @@ -182,6 +186,10 @@ if (typeof J$ === 'undefined') {
});
var args = parser.parseArgs();

var astHandler = null;
if (args.astHandlerModule) {
astHandler = require(args.astHandlerModule);
}
var initParams = args.initParam;
inlineIID = args.inlineIID;
inlineSource = args.inlineSource;
Expand All @@ -205,6 +213,7 @@ if (typeof J$ === 'undefined') {
var origCode = fs.readFileSync(fileName, "utf8");
var instCodeAndData, instCode;

var inlineRewriter = rewriteInlineScript(astHandler);
if (fileName.endsWith(".js")) {
instCodeAndData = instrumentCode(
{
Expand All @@ -216,11 +225,12 @@ if (typeof J$ === 'undefined') {
inlineSource: inlineSource,
url: url
});
instUtil.applyASTHandler(instCodeAndData, astHandler, sandbox);
fs.writeFileSync(makeSMapFileName(instFileName), instCodeAndData.sourceMapString, "utf8");
fs.writeFileSync(instFileName, instCodeAndData.code, "utf8");
} else {
var jalangiRoot = getJalangiRoot();
instCode = proxy.rewriteHTML(origCode, "http://foo.com", rewriteInlineScript, "");
instCode = proxy.rewriteHTML(origCode, "http://foo.com", inlineRewriter, "");

var headerStr = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
headerStr += instUtil.getInlinedScripts(analyses, initParams, extraAppScripts, EXTRA_SCRIPTS_DIR, jalangiRoot);
Expand Down
13 changes: 2 additions & 11 deletions src/js/commands/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,6 @@ if (typeof J$ === 'undefined') {

var astHandler = options.astHandler;

function applyASTHandler(instResult) {
if (astHandler) {
var info = astHandler(instResult.instAST);
if (info) {
instResult.code = sandbox.Constants.JALANGI_VAR + ".ast_info = " + JSON.stringify(info) + ";\n" + instResult.code;
}
}
return instResult.code;
}

/**
* extra scripts to inject into the application and instrument
Expand Down Expand Up @@ -150,7 +141,7 @@ if (typeof J$ === 'undefined') {
};

var instResult = sandbox.instrumentCode(options);
var instrumentedCode = applyASTHandler(instResult);
var instrumentedCode = instUtil.applyASTHandler(instResult, astHandler, sandbox);
fs.writeFileSync(path.join(copyDir, instname).replace(/.js$/, "_jalangi_.json"), instResult.sourceMapString, "utf8");
fs.writeFileSync(path.join(copyDir, origname), src);
fs.writeFileSync(path.join(copyDir, instname), instrumentedCode);
Expand Down Expand Up @@ -310,7 +301,7 @@ if (typeof J$ === 'undefined') {
}
}
if (instResult) {
var instrumentedCode = applyASTHandler(instResult);
var instrumentedCode = instUtil.applyASTHandler(instResult, astHandler, sandbox);
fs.writeFileSync(this.instScriptName.replace(/.js$/, "_jalangi_.json"), instResult.sourceMapString, "utf8");
this.push(instrumentedCode);
}
Expand Down
13 changes: 3 additions & 10 deletions src/js/commands/jalangi.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/*global J$ */

var argparse = require('argparse');
var instUtil = require('../instrument/instUtil');
var parser = new argparse.ArgumentParser({
addHelp: true,
description: "Command-line utility to perform Jalangi2's instrumentation and analysis"
Expand All @@ -41,15 +42,7 @@ if (args.astHandlerModule) {
astHandler = require(args.astHandlerModule);
}

function applyASTHandler(instResult) {
if (astHandler) {
var info = astHandler(instResult.instAST);
if (info) {
instResult.code = J$.Constants.JALANGI_VAR + ".ast_info = " + JSON.stringify(info) + ";\n" + instResult.code;
}
}
return instResult.code;
}


if (args.script_and_args.length === 0) {
console.error("must provide script to record");
Expand Down Expand Up @@ -109,7 +102,7 @@ Module._extensions['.js'] = function (module, filename) {
inlineSourceMap: !!args.inlineIID,
inlineSource: !!args.inlineSource
});
applyASTHandler(instCodeAndData);
instUtil.applyASTHandler(instCodeAndData, astHandler, J$);
fs.writeFileSync(makeSMapFileName(instFilename), instCodeAndData.sourceMapString, "utf8");
fs.writeFileSync(instFilename, instCodeAndData.code, "utf8");
module._compile(instCodeAndData.code, filename);
Expand Down
10 changes: 10 additions & 0 deletions src/js/instrument/instUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ function genInitParamsCode(initParams) {
return "<script>J$.initParams = " + JSON.stringify(initParamsObj) + ";</script>";
}

function applyASTHandler(instResult, astHandler, sandbox) {
if (astHandler) {
var info = astHandler(instResult.instAST);
if (info) {
instResult.code = sandbox.Constants.JALANGI_VAR + ".ast_info = " + JSON.stringify(info) + ";\n" + instResult.code;
}
}
return instResult.code;
}

function headerCodeInit(root) {
headerSources.forEach(function (src) {
Expand Down Expand Up @@ -176,6 +185,7 @@ exports.setHeaders = setHeaders;
exports.getHeaderCode = getHeaderCode;
exports.getHeaderCodeAsScriptTags = getHeaderCodeAsScriptTags;
exports.genInitParamsCode = genInitParamsCode;
exports.applyASTHandler = applyASTHandler;
exports.isInlineScript = isInlineScript;
exports.headerSources = headerSources;
exports.createFilenameForScript = createFilenameForScript;
Expand Down

0 comments on commit b96266f

Please sign in to comment.