denoland/deno · error · TypeError

Invalid status text

Error message

Invalid status text: "${init.statusText}"

What it means

initializeAResponse validates init.statusText against the reason-phrase regex (visible ASCII plus tab and space, no control characters). A status text containing CR, LF, other control bytes, or non-Latin-1 characters fails the regex and throws TypeError 'Invalid status text: "..."' — the CRLF restriction prevents response splitting via the status line.

Solutions

  1. Keep statusText to plain visible text: "OK", "Created", "Upstream Error"
  2. Strip CR/LF from any dynamic text: text.replace(/[\r\n\x00-\x1f]/g, " ")
  3. Put machine-readable data in headers or the body, never in statusText

Example fix

// before
new Response("ok", { status: 200, statusText: "OK\r\nX-Injected: 1" }); // throws

// after
new Response("ok", { status: 200, statusText: "OK" });
Defensive patterns

Strategy: validation

Validate before calling

function safeStatusText(text) {
  return String(text ?? "").replace(/[\r\n\x00-\x1f\x7f]/g, " ").trim() || "OK";
}
new Response(body, { status: 200, statusText: safeStatusText(dynamicText) });

Type guard

const REASON_PHRASE_OK = /^[\t \x21-\x7e\x80-\xff]*$/;
function isValidStatusText(t: string): boolean {
  return REASON_PHRASE_OK.test(t);
}

Try / catch

try {
  return new Response(body, { status, statusText });
} catch (err) {
  if (err instanceof TypeError && err.message.includes("Invalid status text")) {
    return new Response(body, { status }); // omit statusText entirely
  }
  throw err;
}

Prevention

When it happens

Trigger: new Response(body, { status: 200, statusText: "OK\r\nSet-Cookie: x=1" }); statusText containing \n; statusText with a control character like \x01.

Common situations: Putting dynamic/header-like content into statusText; forwarding statusText from an upstream server verbatim (rare upstream bugs send embedded CRLF); template strings with trailing newlines.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/aaefee41c809b22a. Report an issue: GitHub.

Appendix: source

Thrown at ext/fetch/23_response.js:528

  response,
  init,
  bodyWithType,
  prefix,
  context,
) {
  // 1.
  if ((init.status < 200 || init.status > 599) && init.status != 101) {
    throw new RangeError(
      `The status provided (${init.status}) is not equal to 101 and outside the range [200, 599]`,
    );
  }

  // 2.
  if (
    init.statusText &&
    RegExpPrototypeExec(REASON_PHRASE_RE, init.statusText) === null
  ) {
    throw new TypeError(
      `Invalid status text: "${init.statusText}"`,
    );
  }

  // 3.
  response[_response].status = init.status;

  // 4.
  response[_response].statusMessage = init.statusText;
  // 5.
  if (init.headers !== undefined) {
    const list = responseHeaderList(response);
    if (
      !tryFillSingleContentTypeHeader(
        list,
        init.headers,
      )
    ) {

View on GitHub (pinned to 9ad36f7a2c)