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 real := evalSymlinks(roots)
36
37 for _, root := range topLevelDirs(real) {
38 if _, err := e.nodeSymlinkChecks.GetOrCreate(kind+"|"+root, func() (bool, error) {
39 return true, walkSymlinks(root, real, kind)
40 }); err != nil {
41 return err
42 }
43 }
44 return nil
45 }
46
47 // CheckReadPath fails if filename resolves outside the allowRead roots.
48 // Used by in-process tools (e.g. ESBuild) that read files on their own,
49 // so they get the same boundary as the Node.js tools.
50 func (e *Exec) CheckReadPath(filename string) error {
51 _, err := e.readPathChecks.GetOrCreate(filename, func() (bool, error) {
52 roots := e.nodeReadRoots("")
53 if slices.Contains(roots, "*") {
54 return true, nil
55 }
56 target, err := filepath.EvalSymlinks(filename)
57 if err != nil {
58 return false, err
59 }
60 if !isBelowAny(target, evalSymlinks(roots)) {
61 return false, fmt.Errorf("%q resolves to %q outside the project; only files below the working directory or the paths in security.node.permissions.allowRead can be imported", filename, target)
62 }
63 return true, nil
64 })
65 return err
66 }
67
68 // evalSymlinks resolves paths, dropping the ones that cannot be resolved.
69 func evalSymlinks(paths []string) []string {
70 var real []string
71 for _, p := range paths {
72 if p, err := filepath.EvalSymlinks(p); err == nil {
73 real = append(real, p)
74 }
75 }
76 return real
77 }
78
79 func walkSymlinks(root string, allowed []string, kind string) error {
80 return filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
81 if err != nil {
82 if p == root {
83 return nil
84 }
85 return err
86 }
87 if d.Type()&os.ModeSymlink == 0 {
88 return nil
89 }
90 target, err := filepath.EvalSymlinks(p)
91 if err != nil {
92 // Dangling; nothing to read through.
93 return nil
94 }
95 if !isBelowAny(target, allowed) {
96 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)
97 }
98 return nil
99 })
100 }
101
102 // topLevelDirs drops dirs nested below another entry so they are not walked twice.
103 func topLevelDirs(dirs []string) []string {
104 sorted := slices.Clone(dirs)
105 slices.Sort(sorted)
106 var top []string
107 for _, d := range sorted {
108 if len(top) > 0 && isBelow(d, top[len(top)-1]) {
109 continue
110 }
111 top = append(top, d)
112 }
113 return top
114 }
115
116 func isBelowAny(p string, dirs []string) bool {
117 return slices.ContainsFunc(dirs, func(d string) bool { return isBelow(p, d) })
118 }
119
120 func isBelow(p, dir string) bool {
121 return p == dir || strings.HasPrefix(p, strings.TrimSuffix(dir, string(filepath.Separator))+string(filepath.Separator))
122 }