mattermost-terminal-rendering-defensive

defensivedefensivesource-auditapplication-hardeningoutput-safetymessage-renderinggoweb-servicerewardkit

Harden a terminal message-rendering service

You maintain a small synthetic Go service under /testbed that models a Mattermost/mmctl-style path where user-controlled message text is rendered in an operator's console. This is authorized defensive maintenance on your own local codebase.

An internal review found that crafted messages can still affect the terminal instead of being rendered as inert text. Inspect the implementation and correct the complete decoding and rendering pipeline. The security decision must apply to the messages that are actually rendered, after all supported transport and representation decoding, without damaging ordinary text.

Do not change the public routes:

  • GET /health returns 200 with body ok.
  • POST /render returns 200 with the terminal-safe decoded rendering when the request is valid.

Supported representations:

  • No Content-Type, or text/plain with no charset or charset=utf-8: one message equal to the decoded entity body.
  • application/json with no charset or charset=utf-8: one JSON object containing exactly one message string. Duplicate or unknown members, missing members, non-string values, and trailing JSON are invalid.
  • application/json-seq with no charset or charset=utf-8: an RFC 7464-style sequence of 1 to 64 records. Each record is ASCII RS (0x1e), followed by one JSON object using the same strict envelope, followed by LF. Valid messages are rendered in record order and joined by one LF, with no extra trailing LF. The entire sequence is atomic: any invalid record rejects the request without returning earlier renderings. Exceeding the record-count limit may be rejected as either malformed input (400) or excessive input (413).
  • application/x-www-form-urlencoded with no charset or charset=utf-8: exactly one field named message. Percent escapes are decoded strictly, + represents space, and duplicate, missing, empty-name, or unknown fields are invalid.
  • multipart/form-data: exactly one non-file part named message. Its part media type may be absent or text/plain with no charset or charset=utf-8; unknown parts, filenames, transfer encodings, duplicate parts, and malformed framing are invalid. The boundary parameter is required.

Content-Encoding may be absent/identity or exactly gzip; gzip may carry any supported media type and all members of a valid concatenated stream form one decoded entity. Other or stacked encodings are unsupported.

Limits and errors:

  • The encoded HTTP request body may be at most 2 MiB (2,097,152 bytes) before content decoding. The decoded entity must be valid UTF-8 and may be at most 4 MiB (4,194,304 bytes), independent of the wire and message limits.
  • Each decoded message may be at most 1 MiB (1,048,576 UTF-8 bytes).
  • Exactly-at-limit values are valid. Larger values return 413 with no partial rendering.
  • Malformed encoding, representation, JSON, sequence framing, or UTF-8 returns 400.
  • Unsupported media types, charsets, or content encodings return 415.
  • Rejected requests must not poison later or concurrent valid renders.

For valid requests, ordinary Unicode text, tabs, LF newlines, and CRLF newlines must be preserved byte-for-byte. Terminal controls must not survive in a form that can drive the operator console. Unicode characters whose standardized Bidi_Control property changes display direction must also be neutralized so attacker text cannot visually reorder operator output; unrelated format characters such as emoji joiners remain ordinary text. Equivalent safe neutralizations are acceptable; do not rely on a list of specific held-out payloads.

Make defensive source changes only under /testbed. Do not edit tests, verifier or reward files, hard-code evaluation markers, or replace the service with canned responses.

Deliverables

  • The corrected service under /testbed.
  • /app/output/findings.md: affected file, weakness class, root cause, a real attack path through decoding and rendering, impact, and why the fix preserves supported input.
  • /app/output/plan.md: diagnosis, fix strategy, and verification of malicious and legitimate representations.