DEV Community

Koussay Mehdouani
Koussay Mehdouani

Posted on

My first GitLab contribution: teaching gitlab-shell to explain its own errors

Some error messages tell you exactly what went wrong. Others just shrug.

In gitlab-shell — the Go program that handles Git over SSH for GitLab — a failed JSON parse produced this, and only this:

parsing failed
Enter fullscreen mode Exit fullscreen mode

No URL. No content type. Nothing about which request had just been parsed. If you were the operator staring at that log line, you knew something had failed and nothing else. Issue #888 in gitlab-shell asked for the obvious thing: when parsing fails, say what you were parsing.

I'm a high-school student in Tunisia, and that issue became my first contribution to GitLab.

The change

The fix itself is small. When the JSON parsing path in gitlab-shell fails now, it logs the request context before returning the error — the content type, and the URL it was talking to — using LabKit, GitLab's structured logging library.

One thing deliberately did not change: the returned error is still exactly parsing failed. Other code and other people's log searches match on that string, so the context goes to the logs, not into the error. The first version of my patch had loosened a couple of test expectations to make them pass; the review pushed back, the expectations went back to being strict, and I added a regression test for the new logging instead. That was round one.

What three rounds of review actually taught me

The automated reviewer on my merge request (!1664) was not a rubber stamp. It found real things, including one I should have caught myself.

Round two was a security catch. My first version logged the full request URL. URLs carry query strings, and query strings can carry tokens. The reviewer asked for the URL to be sanitized before it touched a log file — scheme, host and path only, with the query, fragment and any embedded credentials stripped. It was right, and it is the kind of mistake you only have to be taught once: a log line is a publication, and anything you would not paste in public does not belong in one.

Round three was about a test that could not fail. I had written an assertion that a sensitive string did not appear in the logged values — but the assertion ran against a list, in a form that would pass even if the string was there. A check that passes no matter what is worse than no check, because it gives you confidence you have not earned. The fix was to join the values into one string first, so the assertion could genuinely fail — and I verified that it does fail against the old code.

The part nobody warns you about

My fork's CI pipelines never ran. GitLab requires identity verification to run pipelines on my account, and verification wants a bank card, which I do not have. So the merge request carries a note from me explaining exactly that: the full test suite passes locally (31 packages), and the pipeline gap is an account limitation, not a code failure. Maintainers can run the pipeline on their side. If you are a young contributor without a card, this wall exists — it is not a rejection, and saying so plainly on the MR is better than staying silent.

Where it stands

After the third round, the automated review approved the merge request. It is now waiting on a human maintainer's review, which is where it should be — I am not claiming a merge that has not happened. Whatever the verdict, the three rounds already did their work:

  1. Error messages are written for the person who reads them at 3 a.m. Context is not decoration.
  2. Sanitize before you log. URLs, headers and tokens leak through helpfulness.
  3. A test that cannot fail is a lie you tell yourself. Make it prove it can fail first.

If you are looking for a first contribution of your own: pick the error message that annoyed you, in a project you actually use, and make it say what it knows

Top comments (4)

Collapse
 
beusebiu profile image
Eusebiu Balan •

Ran into the other side of "context is not decoration": too much reporting, not too little. The OAuth guard reported every bearer token it rejected as an error, so an MCP client still holding an expired token kept filling one issue in my error tracker, while the client itself just got its normal 401.

Now only that rejection stays out of the tracker. Every other OAuth server error still reports.

Collapse
 
koussay_mehdouani_0edec78 profile image
Koussay Mehdouani •

That's the mirror image of the same problem — thanks for sharing it. My rule from the MR was "context is not decoration"; yours is the complement: an expected rejection isn't an error. A client holding an expired token getting its normal 401 is the system working, not failing — and reporting it as an error just trains you to ignore the tracker. Same principle from both ends: a signal is written for whoever reads it, and noise is its own kind of missing context. "Only that rejection stays out, everything else still reports" is exactly the right line to draw.

Collapse
 
maxbuildsdev profile image
Max Kaya •

The "parsing failed" string staying frozen is the right call, and the way to keep both is a sentinel: var ErrParsingFailed = errors.New("parsing failed") and return fmt.Errorf("%w: content-type=%s url=%s", ErrParsingFailed, ct, safe). Callers switch to errors.Is(err, ErrParsingFailed) and the message can grow context without breaking anyone - the compat problem only exists because matching happens on the text.

For the parse case specifically, the single most useful field is usually the offset, not the URL: json.SyntaxError carries Offset, and json.UnmarshalTypeError carries Field and Value. "parsing failed at byte 318, field request.headers" tells the operator whether the client sent HTML (an error page from a proxy) or a genuinely truncated body - and the first 64 bytes of the body, with values redacted, settles the HTML-vs-JSON question in one glance.

One thing worth checking about the sanitizer: scheme+host+path still publishes private project paths, and gitlab-shell talks to endpoints that embed namespace/project in the path. If those logs ship to a shared ELK, the redaction is only half done.

Did you fuzz the sanitizer itself - user:pass@host, idn hosts, ?a=1#b - or only assert against the one URL that leaked?

Collapse
 
koussay_mehdouani_0edec78 profile image
Koussay Mehdouani •

Thanks, Max — the sentinel is a good call, and errors.Is is the right long-term shape. I kept this MR minimal on purpose: the frozen string is what existing log-matching relies on, so I did not want to change the return shape in the same fix. The offset point is strong too — json.SyntaxError.Offset would have told me HTML-vs-truncated in one line, and it is worth a follow-up. On the sanitizer: you are right, the path still carries namespace/project and I only asserted against the leaked URL and its variants — no fuzzing yet. I will take that as a follow-up to the MR.