Skip to content

Commit ac341b8

Browse files
committed
cmd/go/internal/pkg: fail on bad filenames
Unhidden filenames with forbidden characters in subdirectories now correctly fail the build instead of silently being skipped. Previously this behavior would only trigger on files in the root of the embedded directory. Fixes #54003 Change-Id: I52967d90d6b7929e4ae474b78d3f88732bdd3ac4 Reviewed-on: https://go-review.googlesource.com/c/go/+/670615 Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 12e11e7 commit ac341b8

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,12 +2227,20 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
22272227
rel := filepath.ToSlash(str.TrimFilePathPrefix(path, pkgdir))
22282228
name := d.Name()
22292229
if path != file && (isBadEmbedName(name) || ((name[0] == '.' || name[0] == '_') && !all)) {
2230-
// Ignore bad names, assuming they won't go into modules.
2231-
// Also avoid hidden files that user may not know about.
2230+
// Avoid hidden files that user may not know about.
22322231
// See golang.org/issue/42328.
22332232
if d.IsDir() {
22342233
return fs.SkipDir
22352234
}
2235+
// Ignore hidden files.
2236+
if name[0] == '.' || name[0] == '_' {
2237+
return nil
2238+
}
2239+
// Error on bad embed names.
2240+
// See golang.org/issue/54003.
2241+
if isBadEmbedName(name) {
2242+
return fmt.Errorf("cannot embed file %s: invalid name %s", rel, name)
2243+
}
22362244
return nil
22372245
}
22382246
if d.IsDir() {

src/cmd/go/testdata/script/embed.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,29 @@ go build -x
9595
[symlink] stderr 'x.go:5:12: pattern symdir/x.txt: cannot embed file symdir[\\/]x.txt: in non-directory symdir'
9696
[symlink] env 'GODEBUG='
9797

98+
# build rejects names in subdirectories with invalid punctuation
99+
cp x.go6 x.go
100+
mkdir photos/subdir
101+
cp x.txt photos/subdir/foo.jpg
102+
cp x.txt 'photos/subdir/2022-07-22T15''02''45Z.jpg'
103+
! go build -x
104+
stderr '^x.go:5:12: pattern photos/\*: cannot embed file photos/subdir/2022-07-22T15''02''45Z.jpg: invalid name 2022-07-22T15''02''45Z.jpg$'
105+
[!GOOS:windows] mv 'photos/subdir/2022-07-22T15''02''45Z.jpg' photos/subdir/2022-07-22T15:02:45Z.jpg
106+
[!GOOS:windows] ! go build -x
107+
[!GOOS:windows] stderr '^x.go:5:12: pattern photos/\*: cannot embed file photos/subdir/2022-07-22T15:02:45Z.jpg: invalid name 2022-07-22T15:02:45Z.jpg$'
108+
rm photos
109+
110+
# build ignores hidden names in subdirectories with invalid punctuation
111+
cp x.go6 x.go
112+
mkdir photos/subdir
113+
[!GOOS:windows] cp x.txt photos/subdir/.2022-07-22T15:02:45Z.jpg
114+
[!GOOS:windows] cp x.txt photos/subdir/_2022-07-22T15:02:45Z.jpg
115+
cp x.txt 'photos/subdir/.2022-07-22T15''02''45Z.jpg'
116+
cp x.txt 'photos/subdir/_2022-07-22T15''02''45Z.jpg'
117+
cp x.txt photos/subdir/foo.jpg
118+
go build -x
119+
rm photos
120+
98121
-- x.go --
99122
package p
100123

@@ -142,13 +165,23 @@ import "embed"
142165

143166
//go:embed symdir/*
144167
var X embed.FS
168+
145169
-- x.go5 --
146170
package p
147171

148172
import "embed"
149173

150174
//go:embed symdir/x.txt
151175
var Z string
176+
177+
-- x.go6 --
178+
package p
179+
180+
import "embed"
181+
182+
//go:embed photos/*
183+
var X embed.FS
184+
152185
-- x.txt --
153186
hello
154187

0 commit comments

Comments
 (0)