OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
PathSafety.cpp
Go to the documentation of this file.
1#include "PathSafety.h"
2
3#include <cstdint>
4
5namespace Utils
6{
7 namespace
8 {
9 constexpr std::size_t MAX_COMPONENT_LENGTH = 255;
10 constexpr std::size_t MAX_ENTRY_PATH_LENGTH = 1024;
11
12 bool is_control_character(unsigned char c)
13 {
14 return c < 0x20 || c == 0x7F;
15 }
16
17 bool is_windows_reserved_device_name(const std::string& component)
18 {
19 // Windows resolves "NUL.tgz" to the NUL device: the name up to the
20 // first dot is the device match. Reject the classic reserved set
21 // (case-insensitive, per component).
22 const auto stem = component.substr(0, component.find('.'));
23
24 std::string upper;
25 upper.reserve(stem.size());
26 for (const auto c : stem)
27 {
28 upper.push_back(c >= 'a' && c <= 'z' ? static_cast<char>(c - 'a' + 'A') : c);
29 }
30
31 if (upper == "CON" || upper == "PRN" || upper == "AUX" || upper == "NUL")
32 {
33 return true;
34 }
35 if ((upper.size() == 4) &&
36 (upper.compare(0, 3, "COM") == 0 || upper.compare(0, 3, "LPT") == 0) &&
37 (upper[3] >= '1' && upper[3] <= '9'))
38 {
39 return true;
40 }
41 return false;
42 }
43
44 // Full per-component policy applied identically by both validators:
45 // no empty, ".", "..", Windows name-normalization aliases (".. ",
46 // "file.", "file "), or reserved device names. Windows strips trailing
47 // dots/spaces before hitting the filesystem, so ".. " IS ".." there -
48 // a traversal that would pass a byte-equality check alone.
49 bool is_safe_path_component(const std::string& component)
50 {
51 if (component.empty())
52 {
53 return false;
54 }
55 if (component == "." || component == "..")
56 {
57 return false;
58 }
59 const auto last = component.back();
60 if (last == '.' || last == ' ')
61 {
62 return false;
63 }
64 if (is_windows_reserved_device_name(component))
65 {
66 return false;
67 }
68 return true;
69 }
70 }
71
72 bool is_safe_filename_component(const std::string& name)
73 {
74 if (name.empty() || name.size() > MAX_COMPONENT_LENGTH)
75 {
76 return false;
77 }
78
79 for (const auto c : name)
80 {
81 const auto uc = static_cast<unsigned char>(c);
82 if (is_control_character(uc))
83 {
84 return false;
85 }
86 switch (c)
87 {
88 case '/':
89 case '\\':
90 // separators turn one component into a path - traversal/splitting
91 return false;
92 case ':':
93 // drive letters ("C:x") and NTFS alternate data streams ("f.txt:stream")
94 return false;
95 default:
96 break;
97 }
98 }
99
100 return is_safe_path_component(name);
101 }
102
103 bool is_safe_relative_entry_path(const std::string& name)
104 {
105 if (name.empty() || name.size() > MAX_ENTRY_PATH_LENGTH)
106 {
107 return false;
108 }
109
110 // tolerate trailing separators: directory entries are legitimately
111 // stored as "step/layers/" by common tar writers - a trailing slash
112 // has no traversal meaning (leading separators are still rejected
113 // below, and interior empty components are rejected by the split)
114 auto trimmed = name;
115 while (!trimmed.empty() && (trimmed.back() == '/' || trimmed.back() == '\\'))
116 {
117 trimmed.pop_back();
118 }
119 if (trimmed.empty())
120 {
121 return false;
122 }
123
124 // rooted forms escape the base entirely when joined
125 if (trimmed.front() == '/' || trimmed.front() == '\\')
126 {
127 return false;
128 }
129
130 for (const auto c : trimmed)
131 {
132 const auto uc = static_cast<unsigned char>(c);
133 if (is_control_character(uc))
134 {
135 return false;
136 }
137 if (c == ':')
138 {
139 // drive forms and NTFS streams have no place in an archive entry
140 return false;
141 }
142 }
143
144 // apply the full per-component policy to every component (split on
145 // both separators; a '\' is treated as a separator everywhere so a
146 // Windows-shaped name cannot smuggle components past validation)
147 std::size_t begin = 0;
148 for (std::size_t i = 0; i <= trimmed.size(); ++i)
149 {
150 const bool atSeparator = (i == trimmed.size()) || (trimmed[i] == '/') || (trimmed[i] == '\\');
151 if (!atSeparator)
152 {
153 continue;
154 }
155
156 if (!is_safe_path_component(trimmed.substr(begin, i - begin)))
157 {
158 return false;
159 }
160 begin = i + 1;
161 }
162 return true;
163 }
164}
bool is_safe_filename_component(const std::string &name)
bool is_safe_relative_entry_path(const std::string &name)