Skip to content

fix(register)!: validate JSON response - #109

Merged
bassosimone merged 2 commits into
mainfrom
fix/dontpanic
Oct 8, 2026
Merged

bassosimone merged 2 commits into
mainfrom
fix/dontpanic

Conversation

@bassosimone

@bassosimone bassosimone commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

This patch updates cmd/register/main.go to handle json.Unmarshal errors, which are possible depending on what the response looks like. For example, a truncated response could cause Unmarshal to fail.

While there, prefer an error message and Exit(1) to panicking when the register API call failed.

While there, notice that the response has many pointers, and act accordingly. Make sure it's well formed to avoid null dereferences a few lines below.

BREAKING CHANGE: panic exits with exit code 2 but I'm not convinced about keeping 2 in the new code: 2 is usually reserved for "usage error", and in the current codebase makes sense for "validating" input quickly, while 1 seems a better semantic translation of "the response does not look good, sorry, goodbye!".

This change is Reviewable

@bassosimone bassosimone changed the title Fix/dontpanic fix(register)!: handle json.Unmarshal error Oct 8, 2026
Comment thread cmd/register/main.go Outdated
Comment thread cmd/register/main.go Outdated
Comment thread cmd/register/main.go
Comment thread cmd/register/main.go Outdated
Comment thread cmd/register/main.go Outdated
This patch updates `cmd/register/main.go` to handle
`json.Unmarshal` errors, which are possible depending
on what the response looks like. For example, a
truncated response could cause `Unmarshal` to fail.

We use `rtx.Must`, which logs and exits `1`.

While there, prefer an error message and `Exit(1)` to
panicking when the `register` API call failed.

While there, notice that the response has many pointers,
and act accordingly. Make sure it's well formed to
avoid null dereferences a few lines below.

Based on review comments, we normalized the file itself
to use `log.Fatal/log.Fatalf` on error, so that the
error message always consistently goes to the stderr.

Also, based on review comments, we move all the
checks at top, to ensure that the request is well
formed with non-empty fields, to avoid writing just
some of the files and then exiting because the
data we received from the remote service is wrong.

While there, note that logging the response is
not a good idea once we get `200 OK`, because doing
this might put the service account key on the systemd
journal, which allows anyone in `adm` to read it.

BREAKING CHANGE: `panic` exits with exit code `2` but I'm
not convinced about keeping `2` in the new code: `2` is
usually reserved for "usage error", and in the current
codebase makes sense for "validating" input quickly, while
`1` seems a better semantic translation of "the response
does not look good, sorry, goodbye!".
@bassosimone
bassosimone marked this pull request as ready for review October 8, 2026 14:35
@bassosimone bassosimone changed the title fix(register)!: handle json.Unmarshal error fix(register)!: validate JSON response Oct 8, 2026
@bassosimone

Copy link
Copy Markdown
Member Author

@robertodauria I think I have addressed your feedback, can you take another look? 🙏

@robertodauria
robertodauria self-requested a review October 8, 2026 14:59
@robertodauria

Copy link
Copy Markdown
Contributor

LGTM. Thanks!

@bassosimone
bassosimone merged commit fcb97dd into main Oct 8, 2026
7 checks passed
@bassosimone
bassosimone deleted the fix/dontpanic branch October 8, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants