Skip to content

Report a malformed --account file instead of panicking - #1687

Open
arpitjain099 wants to merge 1 commit into
smallstep:masterfrom
arpitjain099:fix/oauth-account-file-assertions
Open

Report a malformed --account file instead of panicking#1687
arpitjain099 wants to merge 1 commit into
smallstep:masterfrom
arpitjain099:fix/oauth-account-file-assertions

Conversation

@arpitjain099

Copy link
Copy Markdown

Every value read out of the --account JSON uses a bare type assertion:

details := account["installed"].(map[string]interface{})
authzEp = details["auth_uri"].(string)
...
authzEp = account["auth_uri"].(string)

The file is read and json.Unmarshaled into map[string]interface{} before any network call, so its contents alone decide whether these run. Three shapes panic:

$ step oauth --account sa.json --bare
{"type":"service_account"}   panic: interface conversion: interface {} is nil, not string          cmd.go:452
{"installed":{}}             panic: interface conversion: interface {} is nil, not string          cmd.go:447
{"installed":"notamap"}      panic: interface conversion: interface {} is string, not map[string]... cmd.go:446

Any partial, truncated, or non-Google account file does this. Without STEPDEBUG the panic handler in root.go presents it as "Something unexpected happened" and asks the user to mail the output to info@smallstep.com, so it reads as an internal fault rather than a bad file.

The else branch has a separate bug:

return errors.Wrapf(err, "error reading %s: unsupported account type", filename)

err is nil by then, and errors.Wrapf(nil, ...) returns nil, so an unrecognised account file reported no error and execution continued with empty endpoints. I confirmed that: {"other":1} printed nothing and hung rather than failing.

Each value is now read with the two-value form and names the key that is missing or of the wrong type, and the unsupported-type branch returns a real error:

{"installed":"notamap"}      error reading ...: "installed" must be an object
{"installed":{}}             error reading ...: missing or invalid "auth_uri"
{"type":"service_account"}   error reading ...: missing or invalid "auth_uri"
{"other":1}                  error reading ...: unsupported account type

I moved the block into readAccountCredentials so it can be tested at all. command/oauth had no test file, and driving oauthCmd directly is not a viable test: it launches a browser and starts the real OAuth flow, which I do not want in your CI. The helper is a pure function of the filename, so the nine new tests cover the five error shapes, a missing file, and both well-formed shapes asserting the values and the do2lo flag. They panic without the change.

A well-formed installed file still parses to the same endpoints and credentials as before; the refactor is meant to be behaviour-preserving apart from the error paths.

Every value read out of the --account JSON used a bare type assertion, so
step oauth panics on the file's contents before any network call:

  step oauth --account sa.json --bare
  {"type":"service_account"}   panic: interface conversion: interface {} is nil, not string
  {"installed":{}}             panic: interface conversion: interface {} is nil, not string
  {"installed":"notamap"}      panic: interface conversion: interface {} is string, not map[string]interface {}

Any partial, truncated, or non-Google account file does this. Without
STEPDEBUG the panic handler reports it as "Something unexpected happened"
and asks the user to mail the output in.

The unsupported-account-type branch had a separate bug: it wrapped err,
which is nil at that point, and errors.Wrapf(nil, ...) returns nil. So an
unrecognised file reported no error at all and the flow continued with empty
endpoints. Confirmed: that path printed nothing and hung.

Moved the parsing into readAccountCredentials so it can be tested without
starting an OAuth flow, and read each value with the two-value form, naming
the key that is missing or of the wrong type.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
@CLAassistant

CLAassistant commented Aug 19, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added the needs triage Waiting for discussion / prioritization by team label Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs triage Waiting for discussion / prioritization by team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants