Skip to content

Instantly share code, notes, and snippets.

@monochromer
Created December 10, 2018 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromer/65e1f4dcbe0eb39b7175da6f26b3be92 to your computer and use it in GitHub Desktop.
Save monochromer/65e1f4dcbe0eb39b7175da6f26b3be92 to your computer and use it in GitHub Desktop.
protect node.js app
// 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