-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpath_validator.hpp
More file actions
168 lines (144 loc) · 5.84 KB
/
Copy pathpath_validator.hpp
File metadata and controls
168 lines (144 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#include "utf8_path.hpp"
#include <string>
#include <vector>
#include <filesystem>
#include <algorithm>
namespace acecode {
class PathValidator {
public:
PathValidator(const std::string& working_dir, bool dangerous_mode)
: dangerous_mode_(dangerous_mode)
{
try {
working_dir_ = normalize(path_to_utf8(std::filesystem::weakly_canonical(path_from_utf8(working_dir))));
} catch (...) {
working_dir_ = normalize(working_dir);
}
}
// Returns empty string if path is safe, otherwise a rejection reason
std::string validate(const std::string& path) const {
if (dangerous_mode_) return "";
std::string resolved;
try {
resolved = normalize(path_to_utf8(std::filesystem::weakly_canonical(resolve_input_path(path))));
} catch (...) {
resolved = normalize(path_to_utf8(resolve_input_path(path)));
}
// CWD boundary check
if (!is_inside_working_dir(resolved)) {
return "Path outside working directory: " + path +
" (resolved: " + resolved + ", cwd: " + working_dir_ + ")";
}
return "";
}
// Check if a path points to a dangerous/sensitive file
bool is_dangerous_path(const std::string& path) const {
if (dangerous_mode_) return false;
std::string resolved;
try {
resolved = normalize(path_to_utf8(std::filesystem::weakly_canonical(resolve_input_path(path))));
} catch (...) {
resolved = normalize(path_to_utf8(resolve_input_path(path)));
}
// Check filename against dangerous files list
std::string filename = get_filename(resolved);
std::string filename_lower = to_lower(filename);
for (const auto& df : dangerous_files()) {
if (df[0] == '*') {
// Wildcard extension match: *.pem, *.key
std::string ext = df.substr(1); // ".pem", ".key"
if (ends_with_ci(filename_lower, ext)) return true;
} else {
if (filename_lower == df) return true;
}
}
// Check path segments against dangerous directories
for (const auto& dd : dangerous_directories()) {
std::string dd_segment = "/" + dd + "/";
std::string resolved_lower = to_lower(resolved);
if (resolved_lower.find(dd_segment) != std::string::npos) return true;
// Also check if path ends with the dangerous dir
if (ends_with_ci(resolved_lower, "/" + dd)) return true;
}
return false;
}
private:
static std::string normalize(const std::string& path) {
std::string result = path;
for (auto& c : result) {
if (c == '\\') c = '/';
}
// Remove trailing slash (but keep root "/")
while (result.size() > 1 && result.back() == '/') {
result.pop_back();
}
return result;
}
static std::string to_lower(const std::string& s) {
std::string result = s;
std::transform(result.begin(), result.end(), result.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return result;
}
static std::string get_filename(const std::string& path) {
auto pos = path.rfind('/');
if (pos != std::string::npos) return path.substr(pos + 1);
return path;
}
std::filesystem::path resolve_input_path(const std::string& path) const {
std::filesystem::path p = path_from_utf8(path);
if (p.is_relative() && !working_dir_.empty()) {
return path_from_utf8(working_dir_) / p;
}
return p;
}
bool is_inside_working_dir(const std::string& resolved) const {
if (!starts_with_ci(resolved, working_dir_)) return false;
if (resolved.size() == working_dir_.size()) return true;
char next = resolved[working_dir_.size()];
return next == '/' || next == '\\';
}
static bool ends_with_ci(const std::string& str, const std::string& suffix) {
if (suffix.size() > str.size()) return false;
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(),
[](char a, char b) {
return std::tolower(static_cast<unsigned char>(a)) ==
std::tolower(static_cast<unsigned char>(b));
});
}
// Case-insensitive prefix check (for Windows path comparison)
static bool starts_with_ci(const std::string& str, const std::string& prefix) {
if (prefix.size() > str.size()) return false;
for (size_t i = 0; i < prefix.size(); ++i) {
char a = static_cast<char>(std::tolower(static_cast<unsigned char>(str[i])));
char b = static_cast<char>(std::tolower(static_cast<unsigned char>(prefix[i])));
if (a != b) return false;
}
// After matching the prefix, the next char must be / or end of string
if (str.size() > prefix.size() && str[prefix.size()] != '/') return false;
return true;
}
static const std::vector<std::string>& dangerous_files() {
static const std::vector<std::string> files = {
".env", ".env.local", ".env.production",
".gitconfig", ".bashrc", ".bash_profile", ".zshrc",
".npmrc", ".yarnrc",
"id_rsa", "id_ed25519", "id_ecdsa", "id_dsa",
"*.pem", "*.key", "*.p12", "*.pfx",
"authorized_keys", "known_hosts",
".netrc", ".pgpass",
};
return files;
}
static const std::vector<std::string>& dangerous_directories() {
static const std::vector<std::string> dirs = {
".git", ".ssh", ".gnupg", ".vscode",
".aws", ".azure", ".kube",
};
return dirs;
}
std::string working_dir_;
bool dangerous_mode_;
};
} // namespace acecode