Skip to content

chore: Add missing error checks identified by CodeQL#1381

Open
jkennedyvz wants to merge 2 commits into
mainfrom
jk/code-quality
Open

chore: Add missing error checks identified by CodeQL#1381
jkennedyvz wants to merge 2 commits into
mainfrom
jk/code-quality

Conversation

@jkennedyvz

Copy link
Copy Markdown
Contributor

CodeQL identified 8 instances where error values were assigned but never checked before using the returned data. This commit fixes 7 of them (the 8th is in a separate PR for TOTP nil pointer dereference).

Fixed locations:

  • backend/authschemes/localauth/services.go:53 Check error from FindUserAuthByUserID before using authData

  • backend/services/operation_vars.go:37, 58, 170 Check error from lookupOperation before using operation.ID in auth checks Check error from ListOperationVars before iterating over results

  • backend/services/user_groups.go:321, 365 Check error from lookupOperation before using operation.ID in auth checks

  • backend/workers/email.go:111 Check error from database Select, log and continue on failure

These were all potential bugs where:

  1. nil pointer dereferences could occur (services.go)
  2. authorization checks could use invalid operation IDs (operation_vars, user_groups)
  3. database errors were silently ignored (email worker)

Related to security assessment code quality findings.

I confirm that this contribution is made under the terms of the license found in the root directory of this repository's source tree and that I have the authority necessary to make this contribution on behalf of its copyright owner.

CodeQL identified 8 instances where error values were assigned but never
checked before using the returned data. This commit fixes 7 of them
(the 8th is in a separate PR for TOTP nil pointer dereference).

Fixed locations:
- backend/authschemes/localauth/services.go:53
  Check error from FindUserAuthByUserID before using authData

- backend/services/operation_vars.go:37, 58, 170
  Check error from lookupOperation before using operation.ID in auth checks
  Check error from ListOperationVars before iterating over results

- backend/services/user_groups.go:321, 365
  Check error from lookupOperation before using operation.ID in auth checks

- backend/workers/email.go:111
  Check error from database Select, log and continue on failure

These were all potential bugs where:
1. nil pointer dereferences could occur (services.go)
2. authorization checks could use invalid operation IDs (operation_vars, user_groups)
3. database errors were silently ignored (email worker)

Related to security assessment code quality findings.
Comment thread backend/workers/email.go
Limit(50))
if err != nil {
w.logger.Error("Failed to fetch emails from queue", "error", err)
time.Sleep(sleepDuration * time.Second)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be better to be explicit about the wait here, instead of just using the last value. I don't know which is more correct, but I lean towards this:

time.Sleep(w.SleepAfterWorkDuration* time.Millisecond)

on the basis that this error likely means that the database is down. Probably it's going to take minutes to come back up, so maybe wait the full minute? But I can see alternate arguments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using exponential backoff might make sense as well so we're not hammering it

@jrozner jrozner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this error handling is fine and we should do it. Go 1.13 introduced error wrapping and there's been consistent work toward shifting in this direction. We have some wrapping indirection here to support HTTP errors transparently. I'm wondering if we still need this or we can simplify it? Either way, adding these errors in does no harm, we'll just need to update these in the future if we rework the error wrapping.

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.

3 participants