Summary The include validator resolved include globs with Go's filepath.Glob, which doesn't understand POSIX bracket expressions
Description
The include validator resolved include globs with Go's filepath.Glob, which doesn't understand POSIX bracket expressions. Patterns such as [[:alpha:]], [[:alnum:]], [[.x.]] and [[=x=]] matched no files during validation, while nginx resolves includes with glibc glob(3) and loads the matching files, so restricted directives in them (njs load_module, js_*, user root) were never checked. The include walker used by the other directive checks also skipped glob(3)'s [!...] form. This is an incomplete fix of the [!...] handling added for GHSA-xwxf-xc5j-p7hx. Exploiting it needs an administrator-equivalent credential and a target file that already contains restricted directives. Patterns with a leading ], such as []a-z], are rejected by Go as bad patterns, so validation already failed closed for them.
Patches
Fixed in e627f5a561 and 64460e6c69. Released in 2.9.4. Include patterns that use POSIX bracket expressions are now rejected, and include globs are resolved the way glibc glob(3) resolves them.
Workarounds
Limit config write access (administrators, api:write tokens, cluster nodes) to trusted principals.
---
Summary
In internal/config/includes.go, validateConfigIncludes (lines 30-105) resolves include statements using Go's filepath.Glob (nginx.Glob(glibcGlobPattern(pattern))) and treats patterns that return zero matches (matches = [], err = nil) as valid ("Globs with no matches remain valid"). Because glibcGlobPattern (lines 184-186) only replaces [! with [^, while Nginx resolves include globs at runtime using POSIX glob(3) (ngx_open_glob), include patterns using POSIX bracket character classes (such as include conf.d/[[:alpha:]]*.conf; or include conf.d/[[:alnum:]]*.conf;) or leading-] character classes (include conf.d/[]a-z]*.conf;) return zero matches and nil error in Go's filepath.Glob while matching target files in glob(3). Also, VisitStatementsWithIncludes (internal/config/includes.go, line 160) invokes nginx.Glob(pattern) without calling glibcGlobPattern(pattern). As a result, included configuration files matched by glob(3) escape ValidateConfigContentBytes and VisitStatementsWithIncludes inspection while still being loaded by nginx.
Details
- In
internal/config/includes.go(lines 71-100),configIncludeValidator.validateexpands eachincludestatement vianginx.Glob(glibcGlobPattern(pattern)):
if err := visitConfigStatements(content, func(statement []string) error {
if len(statement) != 2 || !strings.EqualFold(statement[0], "include") {
return nil
}
pattern := statement[1]
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(validator.base, pattern)
}
matches, err := nginx.Glob(glibcGlobPattern(pattern))
if err != nil {
return fmt.Errorf("resolve nginx include %s: %w", pattern, err)
}
for _, match := range matches {glibcGlobPattern(internal/config/includes.go, lines 180-186) only translates[!into[^:
// glibcGlobPattern rewrites the glob(3) negated character class "[!...]",
// which nginx resolves includes with, into the "[^...]" form Go's matcher
// understands. Without it Go matches nothing for "[!x]" while nginx loads the
// files, so they would escape validation.
func glibcGlobPattern(pattern string) string {
return strings.ReplaceAll(pattern, "[!", "[^")
}- Go's
path/filepath.Match(called byfilepath.Globvianginx.Glob) does not implement POSIX character classes ([:alpha:],[:alnum:],[:lower:],[:digit:],[:xdigit:]) or POSIX leading-]bracket rules:
- For a pattern such as
conf.d/[[:alpha:]]*.conf,filepath.Matchparses[[:alpha:]as a bracket expression containing the characters[,:,a,l,p,h, closed by the first], followed by the literal character]and*.conf. Unless a filename literally has]as its second character,filepath.Globreturnsmatches = []anderr = nil. - When Nginx parses the same directive,
ngx_conf_includecallsngx_open_glob-> POSIXglob(3), which expands[[:alpha:]]*.confto every file starting with an alphabetic character (such asconf.d/custom.conf). - Similarly, in
internal/config/includes.go(lines 153-163),includeWalker.include(used byVisitStatementsWithIncludesto inspect effective directives across included files) callsmatches, err := nginx.Glob(pattern)directly withoutglibcGlobPattern(pattern):
func (walker *includeWalker) include(pattern string, depth int) {
if strings.Contains(pattern, "$") {
return
}
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(walker.base, pattern)
}
matches, err := nginx.Glob(pattern)
if err != nil {
return
}Because VisitStatementsWithIncludes does not apply glibcGlobPattern, any include conf.d/[!0]*.conf; statement is also skipped by VisitStatementsWithIncludes.
Verification
- In a directory containing
evil.conf, compare Go'sfilepath.Globwith POSIXglob(3)on[[:alpha:]]*.conf:
filepath.Glob("conf.d/[[:alpha:]]*.conf")returnsmatches = [],err = nilbecausefilepath.Matchcloses the bracket class at the first]in[:alpha:]and requires a literal]as the second character.- POSIX
glob("conf.d/[[:alpha:]]*.conf", ...)matchesconf.d/evil.conf.
- In
internal/config/includes.go(lines 79-99), becausematchesis empty anderrisnil, thefor _, match := range matchesloop does not execute andvalidateConfigIncludesreturnsnilwithout callingValidateConfigContentBytesonconf.d/evil.conf. - In
internal/config/includes.go(line 160), callingVisitStatementsWithIncludeson a config containinginclude conf.d/[!0]*.conf;orinclude conf.d/[[:alpha:]]*.conf;yields zero statements fromconf.d/evil.conf.
Impact
An authenticated user with configuration editing privileges can include existing or uploaded files on the server using glob(3) bracket syntax ([[:alpha:]], [[:alnum:]], []a-z], or [!...] for VisitStatementsWithIncludes consumers) so that validateConfigIncludes and VisitStatementsWithIncludes see zero matches and skip all directive policy checks (lua_package_path, init_by_lua_block, content_by_lua_block, load_module, access_log, error_log, ssl_certificate_key, etc.), while Nginx loads and executes the unvalidated directives upon nginx -t / nginx -s reload.
Remediation
Reject bracket constructs where Go's filepath.Match semantics diverge from POSIX glob(3) (such as [:, [], [!], [^], or backslash-escaped bracket ranges) in validateConfigIncludes, and apply glibcGlobPattern consistently in includeWalker.include:
--- a/internal/config/includes.go
+++ b/internal/config/includes.go
@@ -75,6 +75,9 @@
pattern := statement[1]
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(validator.base, pattern)
+ }
+ if hasUnsupportedGlobSyntax(pattern) {
+ return fmt.Errorf("unsupported nginx include glob syntax: %s", pattern)
}
matches, err := nginx.Glob(glibcGlobPattern(pattern))
if err != nil {
@@ -157,7 +160,7 @@
if !filepath.IsAbs(pattern) {
pattern = filepath.Join(walker.base, pattern)
}
- matches, err := nginx.Glob(pattern)
+ matches, err := nginx.Glob(glibcGlobPattern(pattern))
if err != nil {
return
}
@@ -184,3 +187,7 @@
func glibcGlobPattern(pattern string) string {
return strings.ReplaceAll(pattern, "[!", "[^")
}
+
+func hasUnsupportedGlobSyntax(pattern string) bool {
+ return strings.Contains(pattern, "[:") || strings.Contains(pattern, "[]") || strings.Contains(pattern, "[!]") || strings.Contains(pattern, "[^]")
+}--- Reported by Serhii Shunko (@ccsimplyspolit)