Skip to main content
Menu
Advisories

Summary The include validator resolved include globs with Go's filepath.Glob, which doesn't understand POSIX bracket expressions

GHSA-8MRC-PVVV-F7P8Published Updated GitHub

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

  1. In internal/config/includes.go (lines 71-100), configIncludeValidator.validate expands each include statement via nginx.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 {
  1. 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, "[!", "[^")
}
  1. Go's path/filepath.Match (called by filepath.Glob via nginx.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.Match parses [[: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.Glob returns matches = [] and err = nil.
  • When Nginx parses the same directive, ngx_conf_include calls ngx_open_glob -> POSIX glob(3), which expands [[:alpha:]]*.conf to every file starting with an alphabetic character (such as conf.d/custom.conf).
  • Similarly, in internal/config/includes.go (lines 153-163), includeWalker.include (used by VisitStatementsWithIncludes to inspect effective directives across included files) calls matches, err := nginx.Glob(pattern) directly without glibcGlobPattern(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

  1. In a directory containing evil.conf, compare Go's filepath.Glob with POSIX glob(3) on [[:alpha:]]*.conf:
  • filepath.Glob("conf.d/[[:alpha:]]*.conf") returns matches = [], err = nil because filepath.Match closes the bracket class at the first ] in [:alpha:] and requires a literal ] as the second character.
  • POSIX glob("conf.d/[[:alpha:]]*.conf", ...) matches conf.d/evil.conf.
  1. In internal/config/includes.go (lines 79-99), because matches is empty and err is nil, the for _, match := range matches loop does not execute and validateConfigIncludes returns nil without calling ValidateConfigContentBytes on conf.d/evil.conf.
  2. In internal/config/includes.go (line 160), calling VisitStatementsWithIncludes on a config containing include conf.d/[!0]*.conf; or include conf.d/[[:alpha:]]*.conf; yields zero statements from conf.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)

Affected versions

References

GitHub · 0xJacky/nginx-ui