gitea icon indicating copy to clipboard operation
gitea copied to clipboard

Enable gocritic equalFold and autofix issues

Open silverwind opened this issue 5 months ago • 8 comments

Enable https://go-critic.com/overview.html#equalfold, which is marked experimental, so was not enabled by default and ran make lint-go-fix. Reasons why strings.EqualFold is preferred here.

silverwind avatar Jun 10 '25 09:06 silverwind

I won't block it but IMO EqualFold is not a right approach if we only want to handle ASCII chars. Sometimes it is just abused.

func TestString(t *testing.T) {
	s1 := "K"
	s2 := "\u212A"
	assert.True(t, strings.EqualFold(s1, s2))
}

wxiaoguang avatar Jun 10 '25 09:06 wxiaoguang

I see it as a benefit that it handles unicode as well. More often than not you want unicode comparison, especially when dealing with user-submitted data.

silverwind avatar Jun 10 '25 09:06 silverwind

Why it makes sense to use Unicode for HTTP headers or something else? Doesn't it break the standard if the headers are "folded" by unicode chars?

wxiaoguang avatar Jun 10 '25 09:06 wxiaoguang

In short:

  • If we'd like to check "if the string can pass", EqualFold relaxes the charsets, it might be abused or introduce security problems.
  • If we'd like to check "if the string should not pass", EqualFold make the result more strict, in most cases it should be fine.

wxiaoguang avatar Jun 10 '25 09:06 wxiaoguang

Looks like there is a ASCII-only variant ascii.EqualFold:

https://pkg.go.dev/net/http/internal/ascii#EqualFold

Not sure if usable because its in internal namespace.

silverwind avatar Jun 10 '25 09:06 silverwind

Looks like there is a ASCII-only variant ascii.EqualFold:

https://pkg.go.dev/net/http/internal/ascii#EqualFold

Yup, we need to implement one by ourself, something like "strcasecmp/stricmp" in C, or we can just call it util.AsciiEqualFold or util.StrEqualNoCase

wxiaoguang avatar Jun 10 '25 09:06 wxiaoguang

util.AsciiEqualFold sounds good, we can use it for all the header comparisons. Maybe even open a golang issue so that they make that function part of the public API.

silverwind avatar Jun 10 '25 10:06 silverwind

Related golang issue: https://github.com/golang/go/issues/68736

silverwind avatar Jun 10 '25 10:06 silverwind

Fix http auth header parsing #34936

wxiaoguang avatar Jul 03 '25 00:07 wxiaoguang

Ok, I guess for the remaining case, we can enable this lint rule.

silverwind avatar Jul 03 '25 06:07 silverwind

Fresh branch and PR: https://github.com/go-gitea/gitea/pull/34952

silverwind avatar Jul 04 '25 14:07 silverwind