Created
December 10, 2018 14:05
-
-
Save monochromer/65e1f4dcbe0eb39b7175da6f26b3be92 to your computer and use it in GitHub Desktop.
protect node.js app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://blog.logrocket.com/how-to-protect-your-node-js-applications-from-malicious-dependencies-5f2e60ea08f9 | |
const fs = require('fs') | |
const path = require('path') | |
const wrap = (module, name, wrapper) => { | |
const original = module[name] | |
module[name] = wrapper(original) | |
} | |
wrap(fs, 'readFileSync', (readFileSync) => (...args) => { | |
const [filepath] = args | |
const fullpath = path.resolve(filepath) | |
if (fullpath.startsWith('/system/')) { | |
throw new Error('You do not have permissions to access this file') | |
} | |
return readFileSync(...args) | |
}) | |
// Prevent further changes | |
Object.freeze(fs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment