Line data Source code
1 : #include "PathSafety.h"
2 :
3 : #include <cstdint>
4 :
5 : namespace 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 4549024 : bool is_control_character(unsigned char c)
13 : {
14 4549024 : return c < 0x20 || c == 0x7F;
15 : }
16 :
17 405298 : 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 405298 : const auto stem = component.substr(0, component.find('.'));
23 :
24 405298 : std::string upper;
25 405298 : upper.reserve(stem.size());
26 4283970 : for (const auto c : stem)
27 : {
28 3878672 : upper.push_back(c >= 'a' && c <= 'z' ? static_cast<char>(c - 'a' + 'A') : c);
29 : }
30 :
31 405298 : if (upper == "CON" || upper == "PRN" || upper == "AUX" || upper == "NUL")
32 : {
33 3 : return true;
34 : }
35 405295 : if ((upper.size() == 4) &&
36 405297 : (upper.compare(0, 3, "COM") == 0 || upper.compare(0, 3, "LPT") == 0) &&
37 2 : (upper[3] >= '1' && upper[3] <= '9'))
38 : {
39 2 : return true;
40 : }
41 405293 : return false;
42 405298 : }
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 405315 : bool is_safe_path_component(const std::string& component)
50 : {
51 405315 : if (component.empty())
52 : {
53 1 : return false;
54 : }
55 405314 : if (component == "." || component == "..")
56 : {
57 8 : return false;
58 : }
59 405306 : const auto last = component.back();
60 405306 : if (last == '.' || last == ' ')
61 : {
62 8 : return false;
63 : }
64 405298 : if (is_windows_reserved_device_name(component))
65 : {
66 5 : return false;
67 : }
68 405293 : return true;
69 : }
70 : }
71 :
72 26 : bool is_safe_filename_component(const std::string& name)
73 : {
74 26 : if (name.empty() || name.size() > MAX_COMPONENT_LENGTH)
75 : {
76 2 : return false;
77 : }
78 :
79 150 : for (const auto c : name)
80 : {
81 135 : const auto uc = static_cast<unsigned char>(c);
82 135 : if (is_control_character(uc))
83 : {
84 9 : return false;
85 : }
86 132 : switch (c)
87 : {
88 4 : case '/':
89 : case '\\':
90 : // separators turn one component into a path - traversal/splitting
91 4 : return false;
92 2 : case ':':
93 : // drive letters ("C:x") and NTFS alternate data streams ("f.txt:stream")
94 2 : return false;
95 126 : default:
96 126 : break;
97 : }
98 : }
99 :
100 15 : return is_safe_path_component(name);
101 : }
102 :
103 86068 : bool is_safe_relative_entry_path(const std::string& name)
104 : {
105 86068 : if (name.empty() || name.size() > MAX_ENTRY_PATH_LENGTH)
106 : {
107 1 : 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 86067 : auto trimmed = name;
115 103358 : while (!trimmed.empty() && (trimmed.back() == '/' || trimmed.back() == '\\'))
116 : {
117 17291 : trimmed.pop_back();
118 : }
119 86067 : if (trimmed.empty())
120 : {
121 0 : return false;
122 : }
123 :
124 : // rooted forms escape the base entirely when joined
125 86067 : if (trimmed.front() == '/' || trimmed.front() == '\\')
126 : {
127 4 : return false;
128 : }
129 :
130 4634950 : for (const auto c : trimmed)
131 : {
132 4548889 : const auto uc = static_cast<unsigned char>(c);
133 4548889 : if (is_control_character(uc))
134 : {
135 2 : return false;
136 : }
137 4548887 : if (c == ':')
138 : {
139 : // drive forms and NTFS streams have no place in an archive entry
140 0 : 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 86061 : std::size_t begin = 0;
148 4720914 : for (std::size_t i = 0; i <= trimmed.size(); ++i)
149 : {
150 4634866 : const bool atSeparator = (i == trimmed.size()) || (trimmed[i] == '/') || (trimmed[i] == '\\');
151 4634866 : if (!atSeparator)
152 : {
153 4229566 : continue;
154 : }
155 :
156 405300 : if (!is_safe_path_component(trimmed.substr(begin, i - begin)))
157 : {
158 13 : return false;
159 : }
160 405287 : begin = i + 1;
161 : }
162 86048 : return true;
163 86067 : }
164 : }
|