diff options
author | 2021-05-10 20:05:53 -0700 | |
---|---|---|
committer | 2021-05-10 20:05:53 -0700 | |
commit | fc75a0dea67aa55fa972b6244358c58ac03bd2d7 (patch) | |
tree | d2a9fd4802e3f9a01aee1618da8d604653695c8d /src/resolver/resolve_path.zig | |
parent | 166c353ddbbd943d1bb49ad7e017a058b8f309ea (diff) | |
download | bun-fc75a0dea67aa55fa972b6244358c58ac03bd2d7.tar.gz bun-fc75a0dea67aa55fa972b6244358c58ac03bd2d7.tar.zst bun-fc75a0dea67aa55fa972b6244358c58ac03bd2d7.zip |
asdasdasdasd
Former-commit-id: 2b3c0584c623486d8ab5dc838bb7ba861b4395d7
Diffstat (limited to 'src/resolver/resolve_path.zig')
-rw-r--r-- | src/resolver/resolve_path.zig | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 81921e510..78f00cf96 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -4,19 +4,14 @@ const std = @import("std"); /// Resolves a unix-like path and removes all "." and ".." from it. Will not escape the root and can be used to sanitize inputs. -pub fn resolvePath(buffer: []u8, src_path: []const u8) error{BufferTooSmall}![]u8 { - if (buffer.len == 0) - return error.BufferTooSmall; - if (src_path.len == 0) { - buffer[0] = '/'; - return buffer[0..1]; - } - +pub fn resolvePath(buffer: []u8, src_path: []const u8) ?[]u8 { var end: usize = 0; - buffer[0] = '/'; + buffer[0] = '.'; var iter = std.mem.tokenize(src_path, "/"); while (iter.next()) |segment| { + if (end >= buffer.len) break; + if (std.mem.eql(u8, segment, ".")) { continue; } else if (std.mem.eql(u8, segment, "..")) { @@ -39,10 +34,16 @@ pub fn resolvePath(buffer: []u8, src_path: []const u8) error{BufferTooSmall}![]u } } - return if (end == 0) + const result = if (end == 0) buffer[0 .. end + 1] else buffer[0..end]; + + if (std.mem.eql(u8, result, src_path)) { + return null; + } + + return result; } fn testResolve(expected: []const u8, input: []const u8) !void { |