Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created June 27, 2019 15:05
Show Gist options
  • Save ccapndave/61efbe7213d426d96d8f2e9da850dec4 to your computer and use it in GitHub Desktop.
Save ccapndave/61efbe7213d426d96d8f2e9da850dec4 to your computer and use it in GitHub Desktop.
function monkeyPatchXMLHttpRequest() {
const { xhook } = require("xhook");
xhook.before(function (request) {
if (request.headers[ "X-Passphrase" ]) {
request.passphrase = request.headers[ "X-Passphrase" ];
delete request.headers[ "X-Passphrase" ];
}
});
xhook.after(async function (request, response, callback) {
if (request.passphrase) {
try {
const plaintext = await Fernet.decrypt(request.passphrase, atob(response.text));
response.text = response.data = plaintext;
} catch (e) {
response.text = e.message;
response.status = 400;
response.statusText = e.message;
}
}
callback();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment