🐛 fix: Guard engine helper registration and template adapter races#483
🐛 fix: Guard engine helper registration and template adapter races#483Copilot wants to merge 16 commits into
Conversation
Agent-Logs-Url: https://github.com/gofiber/template/sessions/3c6010a8-4292-477e-ae24-290aa3d2023a Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/3c6010a8-4292-477e-ae24-290aa3d2023a Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/2deee2c8-8f35-4459-85b4-9e75fde27bce Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/883ab869-6196-4fea-8744-af04a6fbbbeb Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/7bbee3e4-0d27-487f-bb6a-8e1b1fbc20fe Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/7bbee3e4-0d27-487f-bb6a-8e1b1fbc20fe Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/66f14082-3bbc-47f9-b4a1-550c5c32c067 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/c046bd1c-1802-4b89-8695-62cb48b66076 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/c046bd1c-1802-4b89-8695-62cb48b66076 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/872f3cb1-fc49-4ca8-91e8-ac3671898d5c Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
|
@copilot The amber template has a race condition during unit-tests. |
Agent-Logs-Url: https://github.com/gofiber/template/sessions/6a9ca69c-e9b2-42db-88f6-a70ad1314acb Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/6a9ca69c-e9b2-42db-88f6-a70ad1314acb Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Fixed in 50e1a17. The amber adapter now serializes access to the upstream package-global |
|
@copilot race condition in django tests: === NAME Test_HelperOutputIsEscaped |
Agent-Logs-Url: https://github.com/gofiber/template/sessions/9212b475-ec11-4ea7-989b-fb94df3a3e28 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Agent-Logs-Url: https://github.com/gofiber/template/sessions/9212b475-ec11-4ea7-989b-fb94df3a3e28 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Fixed in 2dca1a5. The django adapter now stops mutating pongo2’s global autoescape setting during |
|
@copilot Running [/home/runner/go/bin/golangci-lint run --path-mode=abs --tests=false --timeout=5m] in [/home/runner/work/template/template/mustache] ...
|
Agent-Logs-Url: https://github.com/gofiber/template/sessions/93560d61-7321-4779-9a67-01962f9f11b6 Co-authored-by: gaby <835733+gaby@users.noreply.github.com>
Fixed in 6d5b760. I renamed the local |
There was a problem hiding this comment.
Pull request overview
This PR hardens the core template Engine and several adapters against races, and adds security-focused regression tests and documentation notes around escaping/trust boundaries across engines.
Changes:
- Initialize
Engine.Funcmapon-demand inAddFunc/AddFuncMapto support zero-value Engine usage safely. - Serialize access to upstream package-global state in adapters (Django/pongo2 autoescape; Amber compiler func map) and add Mustache partial-name sanitization + traversal tests.
- Add per-engine XSS/regression tests and document security guidance in engine READMEs.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| template.go | Initializes Funcmap lazily during helper registration under lock. |
| template_test.go | Adds regression tests ensuring AddFunc/AddFuncMap initialize Funcmap. |
| ace/security_test.go | Adds XSS/layout escaping regression tests for Ace templates. |
| ace/README.md | Adds Security guidance and adjusts code snippet formatting. |
| amber/amber.go | Serializes global amber.FuncMap usage during compilation to avoid races. |
| amber/security_test.go | Adds XSS/layout escaping regression tests for Amber templates. |
| amber/README.md | Adds Security guidance and adjusts code snippet formatting. |
| django/django.go | Serializes pongo2 autoescape toggling at render time to prevent cross-engine races. |
| django/security_test.go | Adds XSS/layout/helper escaping regression tests for Django templates. |
| django/README.md | Adds Security guidance for auto-escape and safe output. |
| handlebars/security_test.go | Adds XSS/layout/helper escaping regression tests for Handlebars templates. |
| handlebars/README.md | Adds Security guidance and adjusts code snippet formatting. |
| html/security_test.go | Adds XSS/layout + contextual escaping regression tests for html/template adapter. |
| html/README.md | Adds Security guidance and adjusts code snippet formatting. |
| jet/security_test.go | Adds XSS/layout/helper escaping regression tests for Jet templates. |
| jet/README.md | Adds Security guidance and adjusts code snippet formatting. |
| mustache/mustache.go | Adds partial name sanitization and safer path/root handling for partial loading. |
| mustache/security_test.go | Adds XSS/layout escaping tests plus partial traversal tests. |
| mustache/README.md | Adds Security guidance and adjusts code snippet formatting. |
| pug/security_test.go | Adds XSS/layout escaping regression tests for Pug templates. |
| pug/README.md | Adds Security guidance and adjusts code snippet formatting. |
| slim/security_test.go | Adds XSS/layout/helper escaping regression tests for Slim templates. |
| slim/README.md | Adds Security guidance and adjusts code snippet formatting. |
| README.md | Removes stray whitespace in top-level README. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func sanitizePartialName(name string) (string, error) { | ||
| cleanName := strings.ReplaceAll(strings.TrimSpace(name), `\`, "/") | ||
| cleanName = path.Clean(cleanName) | ||
|
|
||
| switch { | ||
| case cleanName == "." || cleanName == "..": | ||
| return "", fmt.Errorf("mustache: invalid partial path %q", name) | ||
| case path.IsAbs(cleanName): | ||
| return "", fmt.Errorf("mustache: invalid partial path %q", name) | ||
| case strings.HasPrefix(cleanName, "../"): | ||
| return "", fmt.Errorf("mustache: invalid partial path %q", name) | ||
| } | ||
|
|
||
| return cleanName, nil |
Uh oh!
There was an error while loading. Please reload this page.