Is there a way to limit the access on certain nodejs module? #56227
Unanswered
hansyulian
asked this question in
General
Replies: 1 comment 1 reply
-
You can create a custom wrapper around the fs module to restrict access to specific directories const fs = require('fs');
const path = require('path');
const BASE_DIR = path.resolve('D:/Sources'); // Allowed base directory
function safeJoin(base, target) {
const targetPath = path.resolve(base, target);
if (!targetPath.startsWith(base)) {
throw new Error('Access denied: Outside allowed directory');
}
return targetPath;
}
// fs.readFile wrapper
function readFileSafe(filePath, options, callback) {
const safePath = safeJoin(BASE_DIR, filePath);
return fs.readFile(safePath, options, callback);
}
readFileSafe('example.txt', 'utf8', (err, data) => {
if (err) console.error(err);
else console.log(data);
}); This is the easiest way i know about and did some digging also got to know about the library |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Recently i found a post in linkedin regarding code challenge scam: https://www.linkedin.com/posts/franco-aguilera-2583685a_the-code-challenge-scam-they-tried-to-hack-activity-7270114822950703107-K3DW?utm_source=share&utm_medium=member_desktop
So i am wondering whether we can have some kind of limited behaviour of the fs by default? for example:
or something similar
Maybe we need to take a look also on some other module such as http request module that maybe need to be limited as well
Beta Was this translation helpful? Give feedback.
All reactions