Skip to content

Commit 9ffd563

Browse files
committed
fix(tomcat): surface non-IsNotExist errors when checking META-INF/context.xml
os.Stat errors other than IsNotExist (permissions/IO) were treated as "no context.xml" and silently fell back to a default descriptor, dropping user-provided Realm/Resource config. Return the error instead.
1 parent 35d0138 commit 9ffd563

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/java/containers/tomcat.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,14 @@ func (t *TomcatContainer) Finalize() error {
607607
appContextXML := filepath.Join(buildDir, "META-INF", "context.xml")
608608
var contextContent string
609609

610-
if _, err := os.Stat(appContextXML); err == nil {
610+
_, appStatErr := os.Stat(appContextXML)
611+
if appStatErr != nil && !os.IsNotExist(appStatErr) {
612+
// A non-IsNotExist error (permissions/IO) must not be silently
613+
// treated as "no context.xml" — that would drop user-provided
614+
// Realm/Resource config. Surface it.
615+
return fmt.Errorf("failed to check META-INF/context.xml: %w", appStatErr)
616+
}
617+
if appStatErr == nil {
611618
xmlBytes, err := os.ReadFile(appContextXML)
612619
if err != nil {
613620
return fmt.Errorf("failed to read META-INF/context.xml: %w", err)

src/java/containers/tomcat_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,18 @@ var _ = Describe("Tomcat Container", func() {
139139
Expect(string(content)).To(ContainSubstring("reloadable=\"false\""))
140140
})
141141

142+
It("returns an error when META-INF/context.xml cannot be stat'd (non-IsNotExist)", func() {
143+
// Make META-INF a regular file so os.Stat(META-INF/context.xml) fails
144+
// with ENOTDIR (not IsNotExist). The buildpack must surface this rather
145+
// than silently falling back to a default descriptor, which would drop
146+
// user-provided Realm/Resource config.
147+
Expect(os.WriteFile(filepath.Join(buildDir, "META-INF"), []byte("not a dir"), 0644)).To(Succeed())
148+
149+
err := container.Finalize()
150+
Expect(err).To(HaveOccurred())
151+
Expect(err.Error()).To(ContainSubstring("META-INF/context.xml"))
152+
})
153+
142154
It("merges META-INF/context.xml with realm configuration", func() {
143155
metaInfDir := filepath.Join(buildDir, "META-INF")
144156
os.MkdirAll(metaInfDir, 0755)

0 commit comments

Comments
 (0)