Skip to content

JSON template parameters with control characters produce invalid script JSON #1038

Closed

Description

@henricop

processTemplateParams(..., isJson=true) escapes quotes, backslashes, and <, but leaves control characters such as newlines, tabs, and carriage returns unescaped. In a JSON script with processTemplateParams: true, inserting these values produces invalid JSON.

Reproduced on current main using the exported unhead/utils helper:

import { processTemplateParams } from 'unhead/utils'

const result = processTemplateParams(
  '{"name":"%value"}',
  { value: 'line1\nline2' },
  undefined,
  true,
)
JSON.parse(result) // SyntaxError: Bad control character in string literal

The same issue affects TemplateParamsPlugin for opted-in application/json and application/ld+json script content. JSON string control characters must be escaped (RFC 8259, section 7).

I plan to contribute a focused fix using JSON string escaping while retaining the existing < escaping. Regression tests will cover rendered SSR scripts, control-character round trips, quote/backslash/less-than handling, and unchanged plain-text substitutions. This work is AI-assisted.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions