From bb5246c65db66b11287393b4250adda4c6c7b079 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 3 Oct 2023 22:13:36 -0700 Subject: [PATCH] apprt/embedded: validate directory for wd --- src/apprt/embedded.zig | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index a4cf66074..bffc8a95b 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -240,9 +240,35 @@ pub const Surface = struct { defer config.deinit(); // If we have a working directory from the options then we set it. - // TODO: validate the working directory const wd = std.mem.sliceTo(opts.working_directory, 0); - if (wd.len > 0) config.@"working-directory" = wd; + if (wd.len > 0) wd: { + var dir = std.fs.openDirAbsolute(wd, .{}) catch |err| { + log.warn( + "error opening requested working directory dir={s} err={}", + .{ wd, err }, + ); + break :wd; + }; + defer dir.close(); + + const stat = dir.stat() catch |err| { + log.warn( + "failed to stat requested working directory dir={s} err={}", + .{ wd, err }, + ); + break :wd; + }; + + if (stat.kind != .directory) { + log.warn( + "requested working directory is not a directory dir={s}", + .{wd}, + ); + break :wd; + } + + config.@"working-directory" = wd; + } // Initialize our surface right away. We're given a view that is // ready to use.