-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.zig
More file actions
212 lines (172 loc) · 6.21 KB
/
Copy pathpath.zig
File metadata and controls
212 lines (172 loc) · 6.21 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Valence Shell - Path Operations
//!
//! Path handling matching the Coq path model.
//!
//! In Coq: Path = list PathComponent
//! Here: We use []const u8 slices with / separators
//!
//! Security: This module handles path normalization to prevent
//! directory traversal attacks.
const std = @import("std");
const Allocator = std.mem.Allocator;
/// Path component (single directory/file name)
pub const PathComponent = []const u8;
/// Full path as list of components (matches Coq model)
pub const Path = struct {
components: []PathComponent,
allocator: Allocator,
const Self = @This();
/// Parse a string path into components
pub fn fromString(allocator: Allocator, path_str: []const u8) !Self {
var components = std.ArrayList(PathComponent).init(allocator);
var iter = std.mem.splitScalar(u8, path_str, '/');
while (iter.next()) |component| {
if (component.len > 0) {
try components.append(component);
}
}
return .{
.components = try components.toOwnedSlice(),
.allocator = allocator,
};
}
/// Convert back to string
pub fn toString(self: *const Self, allocator: Allocator) ![]const u8 {
if (self.components.len == 0) {
return try allocator.dupe(u8, "/");
}
var result = std.ArrayList(u8).init(allocator);
for (self.components) |comp| {
try result.append('/');
try result.appendSlice(comp);
}
return try result.toOwnedSlice();
}
/// Get parent path (matches Coq parent_path function)
pub fn parent(self: *const Self) ?Self {
if (self.components.len <= 1) {
return null;
}
return .{
.components = self.components[0 .. self.components.len - 1],
.allocator = self.allocator,
};
}
/// Get the final component (filename/dirname)
pub fn basename(self: *const Self) ?PathComponent {
if (self.components.len == 0) {
return null;
}
return self.components[self.components.len - 1];
}
/// Check if this is the root path
pub fn isRoot(self: *const Self) bool {
return self.components.len == 0;
}
/// Check if path contains dangerous components
pub fn containsTraversal(self: *const Self) bool {
for (self.components) |comp| {
if (std.mem.eql(u8, comp, "..") or std.mem.eql(u8, comp, ".")) {
return true;
}
}
return false;
}
pub fn deinit(self: *Self) void {
self.allocator.free(self.components);
}
};
/// Normalize a path string (resolve . and .., remove double slashes)
pub fn normalizePath(allocator: Allocator, path_str: []const u8) ![]const u8 {
var components = std.ArrayList([]const u8).init(allocator);
defer components.deinit();
var iter = std.mem.splitScalar(u8, path_str, '/');
while (iter.next()) |comp| {
if (comp.len == 0 or std.mem.eql(u8, comp, ".")) {
// Skip empty and current-dir references
continue;
} else if (std.mem.eql(u8, comp, "..")) {
// Go up one level if possible
if (components.items.len > 0) {
_ = components.pop();
}
} else {
try components.append(comp);
}
}
// Rebuild path
if (components.items.len == 0) {
return try allocator.dupe(u8, "/");
}
var result = std.ArrayList(u8).init(allocator);
for (components.items) |comp| {
try result.append('/');
try result.appendSlice(comp);
}
return try result.toOwnedSlice();
}
/// Join two paths safely
pub fn joinPaths(allocator: Allocator, base: []const u8, relative: []const u8) ![]const u8 {
// If relative is absolute, use it directly
if (relative.len > 0 and relative[0] == '/') {
return try normalizePath(allocator, relative);
}
const combined = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ base, relative });
defer allocator.free(combined);
return try normalizePath(allocator, combined);
}
/// Check if a path is within a given root (for sandboxing)
pub fn isWithinRoot(path: []const u8, root: []const u8) bool {
// Both paths should be normalized first
if (path.len < root.len) {
return false;
}
// Check prefix match
if (!std.mem.startsWith(u8, path, root)) {
return false;
}
// Make sure we're not matching partial component names
// e.g., /tmp/test should not match /tmp/testing
if (path.len > root.len) {
const next_char = path[root.len];
if (next_char != '/') {
return false;
}
}
return true;
}
// =========================================================
// Tests
// =========================================================
test "path_from_string" {
const allocator = std.testing.allocator;
var path = try Path.fromString(allocator, "/home/user/test");
defer path.deinit();
try std.testing.expect(path.components.len == 3);
try std.testing.expectEqualStrings("home", path.components[0]);
try std.testing.expectEqualStrings("user", path.components[1]);
try std.testing.expectEqualStrings("test", path.components[2]);
}
test "path_parent" {
const allocator = std.testing.allocator;
var path = try Path.fromString(allocator, "/home/user/test");
defer path.deinit();
const parent_opt = path.parent();
try std.testing.expect(parent_opt != null);
const par = parent_opt.?;
try std.testing.expect(par.components.len == 2);
}
test "normalize_path" {
const allocator = std.testing.allocator;
const normalized = try normalizePath(allocator, "/home/user/../test/./file");
defer allocator.free(normalized);
try std.testing.expectEqualStrings("/home/test/file", normalized);
}
test "is_within_root" {
try std.testing.expect(isWithinRoot("/tmp/vsh/test", "/tmp/vsh"));
try std.testing.expect(isWithinRoot("/tmp/vsh", "/tmp/vsh"));
try std.testing.expect(!isWithinRoot("/tmp/vsh_escape", "/tmp/vsh"));
try std.testing.expect(!isWithinRoot("/etc/passwd", "/tmp/vsh"));
}