tiny.sandbox.cwd
Defined in tiny.sandbox.
API (2)
Actions
Public operations.
Source
Source: lib/sandbox/src/cwd.zig
zig
const std = @import("std");const sys = @import("sys");const resolve = @import("resolve.zig");pub fn validate(path: []const u8) !void { if (path.len == 0) return error.InvalidCwd; if (std.fs.path.isAbsolute(path)) return error.AbsoluteCwd; var iterator = std.fs.path.componentIterator(path); while (iterator.next()) |component| { if (std.mem.eql(u8, component.name, "..")) return error.CwdEscapesLayer; }}pub fn ensureInside(root: sys.fs.Dir, child: sys.fs.Dir) !void { var root_buffer: [std.fs.max_path_bytes]u8 = undefined; const root_path = try resolve.dirPath(root, &root_buffer); var child_buffer: [std.fs.max_path_bytes]u8 = undefined; const child_path = try resolve.dirPath(child, &child_buffer); if (!contains(root_path, child_path)) return error.CwdEscapesLayer;}fn contains(root: []const u8, child: []const u8) bool { if (std.mem.eql(u8, root, child)) return true; if (root.len == 1 and root[0] == std.fs.path.sep) return std.fs.path.isAbsolute(child); if (!std.mem.startsWith(u8, child, root)) return false; if (child.len <= root.len) return false; return child[root.len] == std.fs.path.sep;}test "cwd rejects absolute and parent paths" { try std.testing.expectError(error.AbsoluteCwd, validate("/tmp")); try std.testing.expectError(error.CwdEscapesLayer, validate("../tmp")); try std.testing.expectError(error.CwdEscapesLayer, validate("sub/../tmp")); try validate("."); try validate("sub");}Source: lib/sandbox/src/root.zig:35
zig
pub const cwd = @import("cwd.zig");Audit
| Definitions | 3 |
|---|---|
| Public names | 3 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |