Debian Codegraph @

common/hexec/nodesymlinks.go

raw
1 // Copyright 2026 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hexec
15
16 import (
17 "fmt"
18 "io/fs"
19 "os"
20 "path/filepath"
21 "slices"
22 "strings"
23 )
24
25 // Node's permission model checks the lexical path only and follows symlinks
26 // even when they point outside the allowed set (documented as a known
27 // limitation). checkNodeSymlinks walks the allowed roots and fails on any
28 // symlink whose target resolves outside them. Each root is walked once per
29 // Exec; new symlinks created later are not seen.
30 func (e *Exec) checkNodeSymlinks(kind string, roots []string) error {
31 if slices.Contains(roots, "*") {
32 return nil
33 }
34
35 var real []string
36 for _, r := range roots {
37 if p, err := filepath.EvalSymlinks(r); err == nil {
38 real = append(real, p)
39 }
40 }
41
42 for _, root := range topLevelDirs(real) {
43 if _, err := e.nodeSymlinkChecks.GetOrCreate(kind+"|"+root, func() (bool, error) {
44 return true, walkSymlinks(root, real, kind)
45 }); err != nil {
46 return err
47 }
48 }
49 return nil
50 }
51
52 func walkSymlinks(root string, allowed []string, kind string) error {
53 return filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
54 if err != nil {
55 if p == root {
56 return nil
57 }
58 return err
59 }
60 if d.Type()&os.ModeSymlink == 0 {
61 return nil
62 }
63 target, err := filepath.EvalSymlinks(p)
64 if err != nil {
65 // Dangling; nothing to read through.
66 return nil
67 }
68 if !isBelowAny(target, allowed) {
69 return fmt.Errorf("symlink %q resolves to %q outside the paths Node.js is allowed to access; remove it or add its target to security.node.permissions.%s", p, target, kind)
70 }
71 return nil
72 })
73 }
74
75 // topLevelDirs drops dirs nested below another entry so they are not walked twice.
76 func topLevelDirs(dirs []string) []string {
77 sorted := slices.Clone(dirs)
78 slices.Sort(sorted)
79 var top []string
80 for _, d := range sorted {
81 if len(top) > 0 && isBelow(d, top[len(top)-1]) {
82 continue
83 }
84 top = append(top, d)
85 }
86 return top
87 }
88
89 func isBelowAny(p string, dirs []string) bool {
90 return slices.ContainsFunc(dirs, func(d string) bool { return isBelow(p, d) })
91 }
92
93 func isBelow(p, dir string) bool {
94 return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, string(filepath.Separator))+string(filepath.Separator))
95 }