Skip to content

Commit b65c49a

Browse files
Merge pull request #4 from Pro-Fa/fix/ai-findings
Potential fix for code scanning alert no. 4: Uncontrolled data used in path expression
2 parents 4333949 + d0af391 commit b65c49a

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

samples/language-service-sample/serve-sample.cjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ const server = http.createServer((req, res) => {
4040
urlPath = 'samples/language-service-sample' + urlPath;
4141
}
4242

43-
const filePath = path.join(root, urlPath);
43+
const relativePath = urlPath.replace(/^\/+/, '');
44+
const filePath = path.resolve(root, relativePath);
45+
const relativeToRoot = path.relative(root, filePath);
46+
if (relativeToRoot.startsWith('..') || path.isAbsolute(relativeToRoot)) {
47+
return send(res, 403, 'Forbidden');
48+
}
4449

45-
// OBVIOUSLY THIS IS NOT SECURE! DO NOT USE IN UNSAFE ENVIRONMENTS!
4650
fs.stat(filePath, (err, stat) => {
4751
if (err) {
4852
return send(res, 404, 'Not found');

0 commit comments

Comments
 (0)