-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
permission: handle case-sensitive file systems #47269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
RafaelGSS
wants to merge
1
commit into
nodejs:main
from
RafaelGSS:permissin/fix-case-insensitive-systems
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
#include <limits.h> | ||
#include <stdlib.h> | ||
#include <algorithm> | ||
#include <cctype> | ||
#include <filesystem> | ||
#include <string> | ||
#include <vector> | ||
|
@@ -154,6 +155,20 @@ bool FSPermission::is_granted(PermissionScope perm, | |
} | ||
} | ||
|
||
std::string FSPermission::RadixTree::NormalizePathIfCaseInsensitive( | ||
const std::string& path) { | ||
if (!case_sensitive_) { | ||
std::string transformed_path = path; | ||
std::transform( | ||
transformed_path.begin(), | ||
transformed_path.end(), | ||
transformed_path.begin(), | ||
[](unsigned char c) -> unsigned char { return std::tolower(c); }); | ||
return transformed_path; | ||
} | ||
return path; | ||
} | ||
|
||
FSPermission::RadixTree::RadixTree() : root_node_(new Node("")) {} | ||
|
||
FSPermission::RadixTree::~RadixTree() { | ||
|
@@ -168,7 +183,15 @@ bool FSPermission::RadixTree::Lookup(const std::string_view& s, | |
} | ||
|
||
unsigned int parent_node_prefix_len = current_node->prefix.length(); | ||
const std::string path(s); | ||
std::string path(s); | ||
if (!case_sensitive_) { | ||
std::transform( | ||
path.begin(), | ||
path.end(), | ||
path.begin(), | ||
[](unsigned char c) -> unsigned char { return std::tolower(c); }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @bnoordhuis, I am not sure if this is related to the concern in #47105 (comment). |
||
} | ||
|
||
auto path_len = path.length(); | ||
|
||
while (true) { | ||
|
@@ -190,10 +213,11 @@ bool FSPermission::RadixTree::Lookup(const std::string_view& s, | |
} | ||
} | ||
|
||
void FSPermission::RadixTree::Insert(const std::string& path) { | ||
void FSPermission::RadixTree::Insert(const std::string& res) { | ||
FSPermission::RadixTree::Node* current_node = root_node_; | ||
|
||
unsigned int parent_node_prefix_len = current_node->prefix.length(); | ||
const std::string path = NormalizePathIfCaseInsensitive(res); | ||
int path_len = path.length(); | ||
|
||
for (int i = 1; i <= path_len; ++i) { | ||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Flags: --experimental-permission --allow-fs-read=* --no-permission-case-sensitive | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
common.skipIfWorker(); | ||
|
||
const assert = require('node:assert'); | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const protectedFile = fixtures.path('permission', 'deny', 'protected-file.md'); | ||
const protectedFileCapsLetters = fixtures.path('permission', 'deny', 'PrOtEcTeD-File.MD'); | ||
|
||
{ | ||
assert.ok(process.permission.deny('fs.read', [protectedFile])); | ||
} | ||
|
||
{ | ||
// Guarantee the initial protection | ||
assert.throws(() => { | ||
fs.readFile(protectedFile, () => {}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFile), | ||
})); | ||
} | ||
|
||
{ | ||
// uppercase/capitalize files | ||
assert.throws(() => { | ||
fs.readFile(protectedFile.toUpperCase(), (err) => { | ||
assert.ifError(err); | ||
}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFile.toUpperCase()), | ||
})); | ||
|
||
assert.throws(() => { | ||
fs.readFile(protectedFileCapsLetters, (err) => { | ||
assert.ifError(err); | ||
}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFileCapsLetters), | ||
})); | ||
} |
61 changes: 61 additions & 0 deletions
61
test/parallel/test-permission-fs-case-sensitive-default.js
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Flags: --experimental-permission --allow-fs-read=* | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
common.skipIfWorker(); | ||
|
||
const assert = require('node:assert'); | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
const protectedFile = fixtures.path('permission', 'deny', 'protected-file.md'); | ||
const protectedFileCapsLetters = fixtures.path('permission', 'deny', 'PrOtEcTeD-File.MD'); | ||
|
||
{ | ||
assert.ok(process.permission.deny('fs.read', [protectedFile])); | ||
} | ||
|
||
{ | ||
// Guarantee the initial protection | ||
assert.throws(() => { | ||
fs.readFile(protectedFile, () => {}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFile), | ||
})); | ||
} | ||
|
||
// case-sensitive = true | ||
if (common.isLinux) { | ||
// doesNotThrow | ||
fs.readFile(protectedFile.toUpperCase(), (err) => { | ||
assert.ifError(err); | ||
}); | ||
|
||
// doesNotThrow | ||
fs.readFile(protectedFileCapsLetters, (err) => { | ||
assert.ifError(err); | ||
}); | ||
} else { | ||
assert.throws(() => { | ||
fs.readFile(protectedFile.toUpperCase(), (err) => { | ||
assert.ifError(err); | ||
}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFile.toUpperCase()), | ||
})); | ||
|
||
assert.throws(() => { | ||
fs.readFile(protectedFileCapsLetters, (err) => { | ||
assert.ifError(err); | ||
}); | ||
}, common.expectsError({ | ||
code: 'ERR_ACCESS_DENIED', | ||
permission: 'FileSystemRead', | ||
resource: path.toNamespacedPath(protectedFileCapsLetters), | ||
})); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.