From 770100b688c8812d62cff11e23902d397febc539 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 4 May 2023 22:52:16 -0700 Subject: [PATCH] fileutil: start working on symlink checking --- fileutil/fileutil.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index b667696..cab5fda 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -3,6 +3,8 @@ package fileutil import ( "os" + "path/filepath" + "strings" "golang.org/x/sys/unix" ) @@ -45,3 +47,10 @@ const ( func Access(path string, mode int) error { return unix.Access(path, uint32(mode)) } + +// ValidateSymlink checks to make sure a symlink exists in some top-level +// directory. +func ValidateSymlink(symlink, topLevel string) bool { + target, err := filepath.EvalSymlinks(symlink) + return strings.HasPrefix(target, topLevel) +}