update to allow security on hugo to build in config in another dir

This commit is contained in:
2026-03-28 23:31:46 +00:00
parent 98a0e1eda9
commit 76a74bfd04
2 changed files with 38 additions and 26 deletions

View File

@@ -52,3 +52,6 @@ enableGitInfo = true
[[module.mounts]]
source = "/srv/configs"
target = "configs"
[security]
[security.funcs]
readFile = ["^/srv/configs/.*", "^/srv/docs/private/.*"]

View File

@@ -5,37 +5,46 @@
{{- $path := .path -}}
{{- $context := .context -}}
{{/* Map absolute paths to relative mount points */}}
{{- $originalPath := $path -}}
{{- $path = replace $path "/srv/docs/private/" "content/private/" -}}
{{- $path = replace $path "/srv/configs/" "configs/" -}}
{{- $path = strings.TrimLeft "/" $path -}}
{{- $isMarkdown := strings.HasSuffix (lower $path) ".md" -}}
{{- $p := "" -}}
{{- $content := "" -}}
{{- $found := false -}}
{{- $content := "" -}}
{{- $isMarkdown := strings.HasSuffix (lower $path) ".md" -}}
{{/* 1. Try to find a Hugo Page if it's Markdown */}}
{{- if $isMarkdown -}}
{{- $variations := slice $path (replace $path "content/" "") (replace $path "private/" "") -}}
{{- range $v := $variations -}}
{{- if not $p }}{{ $p = $context.Site.GetPage $v }}{{ end -}}
{{- if not $p }}{{ $p = $context.Site.GetPage (printf "/%s" $v) }}{{ end -}}
{{- if not $p }}{{ $p = $context.Site.GetPage (strings.TrimSuffix ".md" $v) }}{{ end -}}
{{- end -}}
{{/* 1. Try absolute path directly (enabled by security settings in hugo.toml) */}}
{{- if fileExists $path -}}
{{- $content = readFile $path -}}
{{- $found = true -}}
{{- end -}}
{{- if $p -}}
{{- $content = $p.Content -}}
{{- $found = true -}}
{{- else -}}
{{/* 2. Fallback to readFile - check multiple path variations */}}
{{- $pathsToTry := slice $path (printf "content/%s" $path) (printf "configs/%s" $path) -}}
{{- range $v := $pathsToTry -}}
{{- if and (not $found) (fileExists $v) -}}
{{- $content = readFile $v -}}
{{- $found = true -}}
{{/* 2. Map absolute paths to relative mount points if not found directly */}}
{{- if not $found -}}
{{- $mappedPath := replace $path "/srv/docs/private/" "content/private/" -}}
{{- $mappedPath = replace $mappedPath "/srv/configs/" "configs/" -}}
{{- $mappedPath = strings.TrimLeft "/" $mappedPath -}}
{{- $p := "" -}}
{{/* Try to find a Hugo Page if it's Markdown */}}
{{- if $isMarkdown -}}
{{- $variations := slice $mappedPath (replace $mappedPath "content/" "") (replace $mappedPath "private/" "") -}}
{{- range $v := $variations -}}
{{- if not $p }}{{ $p = $context.Site.GetPage $v }}{{ end -}}
{{- if not $p }}{{ $p = $context.Site.GetPage (printf "/%s" $v) }}{{ end -}}
{{- if not $p }}{{ $p = $context.Site.GetPage (strings.TrimSuffix ".md" $v) }}{{ end -}}
{{- end -}}
{{- end -}}
{{- if $p -}}
{{- $content = $p.Content -}}
{{- $found = true -}}
{{- else -}}
{{/* Fallback to readFile - check multiple path variations */}}
{{- $pathsToTry := slice $mappedPath (printf "content/%s" $mappedPath) (printf "configs/%s" $mappedPath) -}}
{{- range $v := $pathsToTry -}}
{{- if and (not $found) (fileExists $v) -}}
{{- $content = readFile $v -}}
{{- $found = true -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}