Skip to content

fix(template): expose sprig date/time functions in Go templates#4644

Open
sputnik-mac wants to merge 1 commit intoanchore:mainfrom
sputnik-mac:fix/template-sprig-date-functions
Open

fix(template): expose sprig date/time functions in Go templates#4644
sputnik-mac wants to merge 1 commit intoanchore:mainfrom
sputnik-mac:fix/template-sprig-date-functions

Conversation

@sputnik-mac
Copy link

Summary

Fixes #2372 — Go template sprig date functions (now, date, dateInZone, etc.) are not defined when using syft -o template.

Root Cause

NewFormatEncoder was using sprig.HermeticTxtFuncMap() which intentionally excludes non-deterministic (time-dependent) functions. While this ensures reproducible output, it prevents legitimate use cases like embedding scan timestamps in SBOM templates.

Fix

Replace sprig.HermeticTxtFuncMap() with sprig.TxtFuncMap() to expose the full sprig function set, while explicitly removing the security-sensitive env and expandenv functions to prevent accidental leakage of environment variables into templates.

This allows template authors to use:

{{ now | unixEpoch }}
{{ now | date "2006-01-02" }}
{{ now | dateInZone "15:04:05" (now) "UTC" }}

Testing

Manually verified that now, date, and dateInZone are available in templates after this change.

Signed-off-by: Sputnik-MAC sputnik.mac.001@gmail.com

Replace HermeticTxtFuncMap with TxtFuncMap to expose date/time
functions (now, date, dateInZone, etc.) while still excluding
security-sensitive env/expandenv functions.

Users can now use date functions in templates, e.g.:
  {{ now | unixEpoch }}
  {{ now | date "2006-01-02" }}

Fixes anchore#2372

Signed-off-by: Sputnik-MAC <sputnik.mac.001@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Go template sprig date functions not defined

1 participant