kubernetes / kubernetes/website

layouts/index.headers does not reject whitespace in css/js front matter, unlike layouts/index.redirects

Open
#56,731 3 comments 0 reactions 0 assignees View on GitHub
kind/bug needs-triage priority/awaiting-more-evidence
Dominant language
HTML
Stars
5.4k
Forks
15.7k
Avg merge
4d 18h
Merged PRs (30d)
204

Description

## What happened

`layouts/index.headers` builds the generated `_headers` file from page front
matter. It reads `.Params.css` and `.Params.js`, splits each on commas, trims
spaces, and emits a `Link:` line per element.

The `HEADERS` output format is declared `isPlainText = true` in `hugo.toml`, so
Hugo applies no escaping to what the template emits. Newlines in the front
matter value are neither rejected nor stripped.

In the `_headers` format a line at column zero opens a new path block and
indented lines below it are headers for that path. A `css:` value containing
newlines therefore produces extra path blocks and extra headers in the generated
file rather than a single `Link:` entry, including a `/*` block that applies to
every path.

## What I expected

The sibling template that generates the other Netlify config file already
guards against this. `layouts/index.redirects`:

```gotemplate
{{- if findRE `\s` $alias -}}
{{- errorf "alias %q on %s contains whitespace" $alias $owner -}}
{{- end -}}
```

I expected `layouts/index.headers` to apply the same rule, since it does the
same kind of interpolation into a generated config file.

## How to reproduce

Build a minimal Hugo site with an unmodified copy of `layouts/index.headers`
and the `[mediaTypes."text/netlify"]`, `[outputFormats.HEADERS]` and
`[params.pushAssets]` blocks from `hugo.toml`, using Hugo v0.144.2 extended as
pinned in `netlify.toml`. Add a page with:

```yaml
---
title: example
css: "/css/style_example.css>; rel=preload; as=style\n\n/*\n X-Example: injected"
---
```

The generated `public/_headers` contains an `/*` block with `X-Example`.
`scripts/check-headers-file.sh` passes it, because that script only greps for
`noindex`.

## Notes

Nothing in `content/` today uses this form, so there is no effect on the current
site output.

Fix proposed in #56729.

_This issue was written in part with the assistance of generative AI._

Contributor guide

Open the contributing guide

Research direction

Start with layouts/index.headers and compare its interpolation with the whitespace check in layouts/index.redirects. Reproduce the front matter case from the issue, then inspect scripts/check-headers-file.sh and verify that generated public/_headers no longer contains injected path blocks or headers. A proposed fix is referenced in #56729.

Written by the indexing model from the issue text.

Assessment

Tech stack
css, html, javascript
Domain
build-system, security
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.