Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup/improve safety: remove goto and potentially unbounded for loop #120

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

epelc
Copy link
Contributor

@epelc epelc commented Dec 4, 2024

This pr contains several fixes but primarily aims to make analyzeEncodeModeFromRaw safer by removing the use of a goto statement along with complex boolean checking. Aim is to make this library safer to use in production with web services.

  • reduces the need for fuzing by using simpler logic
  • passes existing tests
  • loop cannot explode and run unbounded on bad input(ie if boolean logic was subtly incorrect it only looks at each byte once instead of multiple times)

Also includes cleanups to further document some methods/variables and remove unused error return value.

epelc added 2 commits December 3, 2024 22:09
  - reduce usage of goto to simplify code
  - remove unused and unchecked error return value
  - removes usage of goto and potentially neverending loop which could lead to DOS attack
  - reduce requirement for fuzzing
@epelc epelc changed the title Cleanup safety Cleanup/improve safety: remove goto and potentially unbounded for loop Dec 4, 2024
break
// Start from most inclusive and work backwards
// Check for each kind starting with most common then expand out to least inclusive.
if analyzeNum(rune(byt)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Is there any meaning of converting a byte to a rune? raw is a byte slice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Characters are runes in go. Using rune instead of byte encourages comparing to a single character which is the point of these functions since they aren’t Unicode aware.

@@ -193,14 +193,15 @@ func draw(mat qrcode.Matrix, opt *outputImageOptions) image.Image {
if !validLogoImage(w, h, logoWidth, logoHeight, opt.logoSizeMultiplier) {
log.Printf("w=%d, h=%d, logoW=%d, logoH=%d, logo is over than 1/%d of QRCode \n",
w, h, logoWidth, logoHeight, opt.logoSizeMultiplier)
goto done

return dc.Image()
Copy link
Owner

Choose a reason for hiding this comment

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

I couldn't agree that goto is unsafe here, replacing goto by directly return is not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It’s one less goto to have to check for security issues. Usually they are dangerous during for loops as you can easily end up with unbounded loops or other issues especially if you aren’t fuzzing.

In this case it’s simple but I think just adding the return here or switching this to an if else on the ‘dc.DrawImage’ is an easy way to avoid it and thus make security reviews for new users have less red flags to check.

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