Repository navigation
bound nand buffers and guest paths #408
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace RuntimeNandGuestPath { | ||
|
|
||
| // A guest path component may only ever name an entry directly inside the directory it is | ||
| // appended to. ':' is refused on every host so a profile behaves the same everywhere: on | ||
| // Windows it forms a drive prefix ("C:"), which std::filesystem appends by *replacing* the | ||
| // path built so far, and an NTFS stream name ("file:stream"). | ||
| inline bool IsSafeComponent(std::string_view component) { | ||
| if (component.empty() || component == "." || component == "..") { | ||
| return false; | ||
| } | ||
| if (component.find_first_of(":/\\") != std::string_view::npos || | ||
| component.find('\0') != std::string_view::npos) { | ||
| return false; | ||
| } | ||
| // Belt and braces for any other host spelling of a root. | ||
| const std::filesystem::path part{std::string(component)}; | ||
| return !part.has_root_name() && !part.has_root_directory(); | ||
| } | ||
|
|
||
| // Appends a '/'-separated guest path below `root`, one component at a time. Empty | ||
| // components (leading, trailing or doubled slashes) are skipped; nullopt when any | ||
| // component could leave `root`. | ||
| inline std::optional<std::filesystem::path> AppendBelowRoot(const std::filesystem::path& root, | ||
| std::string_view guestPath) { | ||
| std::filesystem::path hostPath = root; | ||
| size_t cursor = 0; | ||
| while (cursor < guestPath.size()) { | ||
| const size_t slash = guestPath.find('/', cursor); | ||
| const size_t end = slash == std::string_view::npos ? guestPath.size() : slash; | ||
| if (end != cursor) { | ||
| const std::string_view component = guestPath.substr(cursor, end - cursor); | ||
| if (!IsSafeComponent(component)) { | ||
| return std::nullopt; | ||
| } | ||
| hostPath /= std::filesystem::path{std::string(component)}; | ||
| } | ||
| cursor = end + 1; | ||
| } | ||
| return hostPath; | ||
| } | ||
|
|
||
| } // namespace RuntimeNandGuestPath |
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| #include "nand_guest_path.h" | ||
|
|
||
| #include <iostream> | ||
| #include <stdexcept> | ||
|
|
||
| namespace fs = std::filesystem; | ||
| using RuntimeNandGuestPath::AppendBelowRoot; | ||
| using RuntimeNandGuestPath::IsSafeComponent; | ||
|
|
||
| static void Require(bool condition, const char* message) { | ||
| if (!condition) throw std::runtime_error(message); | ||
| } | ||
|
|
||
| // Lexical containment: every accepted path must still start with the root's components. | ||
| static bool IsBelow(const fs::path& root, const fs::path& path) { | ||
| auto rootIt = root.begin(); | ||
| auto pathIt = path.begin(); | ||
| for (; rootIt != root.end(); ++rootIt, ++pathIt) { | ||
| if (pathIt == path.end() || *pathIt != *rootIt) return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| int main() { | ||
| try { | ||
| #ifdef _WIN32 | ||
| const fs::path root = "C:/Users/player/NAND"; | ||
| #else | ||
| const fs::path root = "/home/player/NAND"; | ||
| #endif | ||
| const auto save = AppendBelowRoot(root, "/title/00010004/524d4350/data/rksys.dat"); | ||
| Require(save.has_value(), "Ordinary save path must translate"); | ||
| Require(*save == root / "title" / "00010004" / "524d4350" / "data" / "rksys.dat", | ||
| "Ordinary save path must keep its components"); | ||
| Require(IsBelow(root, *save), "Ordinary save path must stay below the root"); | ||
|
|
||
| const auto slashes = AppendBelowRoot(root, "//shared2///wc24/"); | ||
| Require(slashes && *slashes == root / "shared2" / "wc24", | ||
| "Empty components must be skipped"); | ||
| const auto rootOnly = AppendBelowRoot(root, "/"); | ||
| Require(rootOnly && *rootOnly == root, "The guest root maps to the host root"); | ||
| const auto empty = AppendBelowRoot(root, ""); | ||
| Require(empty && *empty == root, "An empty relative path maps to the base directory"); | ||
|
|
||
| // A drive prefix replaces the path built so far on Windows. | ||
| Require(!AppendBelowRoot(root, "/title/C:/Windows/system.ini"), "Same-drive prefix must be refused"); | ||
| Require(!AppendBelowRoot(root, "/D:/autorun.inf"), "Other-drive prefix must be refused"); | ||
| Require(!AppendBelowRoot(root, "C:/Users/player/startup.bat"), | ||
| "Drive-absolute relative path must be refused"); | ||
| Require(!AppendBelowRoot(root, "/title/C:"), "Bare drive component must be refused"); | ||
| Require(!AppendBelowRoot(root, "/title/save.dat:stream"), "Stream name must be refused"); | ||
|
|
||
| Require(!AppendBelowRoot(root, "/title/../../escape"), "Unresolved parent component must be refused"); | ||
| Require(!AppendBelowRoot(root, "/title/./data"), "Unresolved current component must be refused"); | ||
| Require(!AppendBelowRoot(root, R"(/title\..\escape)"), "Backslash separator must be refused"); | ||
|
|
||
| Require(!IsSafeComponent(""), "Empty component is not a name"); | ||
| Require(!IsSafeComponent(std::string_view("a\0b", 3)), "Embedded NUL must be refused"); | ||
| Require(IsSafeComponent("rksys.dat"), "Plain file name is safe"); | ||
| Require(IsSafeComponent("..data"), "Leading dots alone are an ordinary name"); | ||
| Require(IsSafeComponent("wc24recv.mbx"), "WC24 mailbox name is safe"); | ||
| } catch (const std::exception& error) { | ||
| std::cerr << "FAILED: " << error.what() << std::endl; | ||
| return 1; | ||
| } | ||
| std::cout << "nand_guest_path_tests passed" << std::endl; | ||
| return 0; | ||
| } |
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.