Skip to content

Trailing Newline Inspector

Run the Trailing Newline Inspector MicroSim Fullscreen

About This MicroSim

A key can look right on a settings page and still be wrong. This MicroSim shows six stored versions of one fake key, sk_test_abc123, one character per box. Invisible characters get their own labeled yellow box: ␠ for a space, \n for a line break, \r for a carriage return.

Each value goes through three steps:

  1. The value is sent to the vendor as stored. The vendor answers 200 only for an exact match, and 401 for anything else.
  2. The value is trimmed, which removes spaces, tabs and line breaks from the two ends only, and sent again.
  3. A start-up schema check looks at the trimmed value. It stops the app if the value is empty, doesn't start with sk_test_ or sk_live_, or contains any whitespace. It never calls the vendor.

The six values are illustrative and use a fake key.

How to Use

  1. Read the stored value and how it got that way.
  2. Set the three guesses below the drawing: Raw (200 or 401), After trim (200 or 401), and Schema stops (Yes or No).
  3. Press Lock in. Watch each step run and see whether each guess was right, with the reason.
  4. Press Next value and repeat for all six. The summary shows your score out of 18.

Iframe Embed Code

You can add this MicroSim to any web page by adding this to your HTML:

<iframe src="https://arunbatchu.github.io/good-enough-engineering/sims/trailing-newline-inspector/main.html"
        height="524px"
        width="100%"
        scrolling="no"></iframe>

Lesson Plan

Learning Objective

The learner will demonstrate, for six stored versions of one API key shown character by character, whether the vendor accepts the key as stored, after trimming, and whether a start-up schema check rejects it, getting at least 16 of 18 predictions right.

Grade Level

Adult professional (Good Enough Engineering)

Duration

10 minutes

Prerequisites

API key, environment variable, 401 Unauthorized, the trailing newline trap and whitespace trimming, from Chapter 6. Env schema validation comes in the section after the sim; the line at the top of the sim gives its rules.

Activities

  1. Predict (6 min): Commit all three guesses for each value before pressing Lock in.
  2. Sort (2 min): From the summary, name which values trim fixes, which the schema catches, and which nothing at start-up catches.
  3. Connect (2 min): Value 6 passes every start-up check. What must the adapter do with the vendor's 401 so the screen doesn't say "no results"?

Common Misconceptions

  • If the value looks right on a settings page, it is right.
  • Trimming fixes every bad key.
  • A key that passes the start-up check is sure to work.
  • A 401 means the data isn't there.

Assessment

Mastery is 16 of 18 predictions correct. The running count shows "Right so far: n of 18."

References

  1. Chapter 6: Secrets and Configuration, sections "The Trailing Newline Trap," "Whitespace Trimming" and "An Env Schema That Checks at Start-Up."
  2. POSIX printf - prints only what you give it, with no line break added.
  3. MDN: String.prototype.trim() - removes whitespace and line breaks from both ends of a string.