FiloSottile/age · error

too much trailing whitespace

Error message

too much trailing whitespace

What it means

drainTrailing is allowed to consume at most maxWhitespace bytes of trailing whitespace after the armor footer. If it fills the limit reader without reaching EOF, the file ends with more whitespace than permitted, so the reader fails with this error to bound memory and reject degenerate inputs.

Source

Thrown at armor/armor.go:126

		} else if err != nil && err != io.EOF {
			return nil, err
		}
		line = bytes.TrimSuffix(line, []byte("\n"))
		line = bytes.TrimSuffix(line, []byte("\r"))
		return line, nil
	}

	const maxWhitespace = 1024
	drainTrailing := func() error {
		buf, err := io.ReadAll(io.LimitReader(r.r, maxWhitespace))
		if err != nil {
			return err
		}
		if len(bytes.TrimSpace(buf)) != 0 {
			return errors.New("trailing data after armored file")
		}
		if len(buf) == maxWhitespace {
			return errors.New("too much trailing whitespace")
		}
		return io.EOF
	}

	var removedWhitespace int
	for !r.started {
		line, err := getLine()
		if err != nil {
			return 0, r.setErr(err)
		}
		// Ignore leading whitespace.
		if len(bytes.TrimSpace(line)) == 0 {
			removedWhitespace += len(line) + 1
			if removedWhitespace > maxWhitespace {
				return 0, r.setErr(errors.New("too much leading whitespace"))
			}
			continue
		}

View on GitHub (pinned to b74dce4cdb)

Solutions

  1. Trim the excessive trailing whitespace from the file before decryption.
  2. Fix the upstream producer that appends the whitespace padding.
  3. Stream-process the input to cut it off right after the END footer line.

Example fix

// before: armored file followed by megabytes of newlines
// after: truncate after footer
sed -n '/END AGE ENCRYPTED FILE---/q;p' padded.age > clean.age
Defensive patterns

Strategy: validation

Validate before calling

// Bound trailing whitespace before decrypting
func trimTrailingWS(path string) error {
    data, err := os.ReadFile(path)
    if err != nil {
        return err
    }
    return os.WriteFile(path, bytes.TrimRight(data, " \t\r\n"), 0o600)
}

Try / catch

_, err := io.ReadAll(armor.NewReader(f))
if err != nil && err.Error() == "too much trailing whitespace" {
    // trim trailing whitespace from the file and retry once
}

Prevention

When it happens

Trigger: Reading an armored file that ends with a very large run of whitespace (more than maxWhitespace bytes) after the footer line — e.g. padding added by a script, disk preallocation, or a corrupted file filled with spaces/newlines.

Common situations: Logs or payloads padded with trailing newlines/spaces, files produced by buggy tools that append whitespace padding, or streams that keep sending whitespace after the armor ends.

Related errors


AI-assisted analysis of FiloSottile/age@b74dce4cdb (2026-08-31). Data as JSON: /api/errors/64e633bd4f691a7a. Report an issue: GitHub.