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

#nosec G115 doesn't work in particular cases if it's preceded by an open bracket { #1240

Open
paul-at-cybr opened this issue Oct 10, 2024 · 3 comments

Comments

@paul-at-cybr
Copy link

paul-at-cybr commented Oct 10, 2024

Summary

In specific cases of int conversion, // #nosec G115 fails to disable the warning if there's an open bracket between the comment and the issue it's addressing.

Steps to reproduce the behavior

package main

import "fmt"

func main() {
	ten := 10
	uintTen := uint(10)
	configVal := uint(ten) // #nosec G115 -- this works
	inputSlice := []int{1, 2, 3, 4, 5}

	if len(inputSlice) <= int(uintTen) { // #nosec G115 -- this works
		fmt.Println("hello world!")
	}

	if len(inputSlice) <= int(configVal) { // #nosec G115 -- this fails
		fmt.Println("hello world!")
	}

	if len(inputSlice) <= int(configVal) /* #nosec G115 -- this works, but is horrible */ {
		fmt.Println("hello world!")
	}

	// #nosec G115 - This also works, although the documentation implies it shouldn't
	if len(inputSlice) <= int(configVal) {
		fmt.Println("hello world!")
	}
}

gosec version

Version: 2.21.4
Git tag: v2.21.4
Build date: 2024-09-26T11:55:22Z

Go version (output of 'go version')

go version go1.23.2 linux/amd64

Operating system / Environment

Any

Expected behavior

A nosec comment that always works

Actual behavior

A nosec comment that sometimes doesn't work

@ccojocar
Copy link
Member

Did you try to place the comment on above the if you want to ignore?

@paul-at-cybr
Copy link
Author

paul-at-cybr commented Oct 12, 2024

Huh, that worked - Updating the title and description to be more accurate, and added a little bit to the example.

@paul-at-cybr paul-at-cybr changed the title #nosec doesn't work if it's preceded by an open bracket { #nosec G115 doesn't work in particular cases if it's preceded by an open bracket { Oct 12, 2024
@ccojocar
Copy link
Member

The nosec typically is ignoring an entire AST node, and not just a line. We did some improvements to be more fine grained, but it seems that this case is not handled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants