forgejo/modules
Jason Song e253888a0e
Fix isAllowed of escapeStreamer (#22814)
The use of `sort.Search` is wrong: The slice should be sorted, and
`return >= 0` doen't mean it exists, see the
[manual](https://pkg.go.dev/sort#Search).

Could be fixed like this if we really need it:

```diff
diff --git a/modules/charset/escape_stream.go b/modules/charset/escape_stream.go
index 823b63513..fcf1ffbc1 100644
--- a/modules/charset/escape_stream.go
+++ b/modules/charset/escape_stream.go
@@ -20,6 +20,9 @@ import (
 var defaultWordRegexp = regexp.MustCompile(`(-?\d*\.\d\w*)|([^\` + "`" + `\~\!\@\#\$\%\^\&\*\(\)\-\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\?\s\x00-\x1f]+)`)

 func NewEscapeStreamer(locale translation.Locale, next HTMLStreamer, allowed ...rune) HTMLStreamer {
+       sort.Slice(allowed, func(i, j int) bool {
+               return allowed[i] < allowed[j]
+       })
        return &escapeStreamer{
                escaped:                 &EscapeStatus{},
                PassthroughHTMLStreamer: *NewPassthroughStreamer(next),
@@ -284,14 +287,8 @@ func (e *escapeStreamer) runeTypes(runes ...rune) (types []runeType, confusables
 }

 func (e *escapeStreamer) isAllowed(r rune) bool {
-       if len(e.allowed) == 0 {
-               return false
-       }
-       if len(e.allowed) == 1 {
-               return e.allowed[0] == r
-       }
-
-       return sort.Search(len(e.allowed), func(i int) bool {
+       i := sort.Search(len(e.allowed), func(i int) bool {
                return e.allowed[i] >= r
-       }) >= 0
+       })
+       return i < len(e.allowed) && e.allowed[i] == r
 }
```

But I don't think so, a map is better to do it.
2023-02-09 20:51:36 +08:00
..
actions Fix actions workflow branches match bug (#22724) 2023-02-02 20:40:08 +08:00
activitypub Add Chef package registry (#22554) 2023-02-06 09:49:21 +08:00
analyze
auth Map OIDC groups to Orgs/Teams (#21441) 2023-02-08 14:44:42 +08:00
avatar
base
cache
charset Fix isAllowed of escapeStreamer (#22814) 2023-02-09 20:51:36 +08:00
container
context Map OIDC groups to Orgs/Teams (#21441) 2023-02-08 14:44:42 +08:00
csv
doctor
emoji
eventsource
generate
git Fix blame view missing lines (#22826) 2023-02-09 11:51:02 +08:00
gitgraph Refactor git command package to improve security and maintainability (#22678) 2023-02-04 10:30:43 +08:00
graceful
hcaptcha
highlight
hostmatcher
html
httpcache Add some comments for recent code (#22725) 2023-02-02 11:39:38 -06:00
httplib
indexer
issue/template
json
lfs
log Improve trace logging for pulls and processes (#22633) 2023-02-03 18:11:48 -05:00
markup Fix README TOC links (#22577) 2023-01-31 13:21:29 +08:00
mcaptcha
metrics include build info in Prometheus metrics (#22819) 2023-02-08 19:54:01 +02:00
migration Fix restore repo bug, clarify the problem of ForeignIndex (#22776) 2023-02-07 09:18:52 +08:00
mirror
nosql
notification Implement actions (#21937) 2023-01-31 09:45:19 +08:00
options
packages Use import of OCI structs (#22765) 2023-02-06 10:07:09 +00:00
paginator
password
pprof
private Implement actions (#21937) 2023-01-31 09:45:19 +08:00
process Improve trace logging for pulls and processes (#22633) 2023-02-03 18:11:48 -05:00
proxy
proxyprotocol
public
queue
recaptcha
references Use correct captured group range when parsing cross-reference (#22672) 2023-01-31 10:08:05 +01:00
regexplru
repository Map OIDC groups to Orgs/Teams (#21441) 2023-02-08 14:44:42 +08:00
secret
session
setting Add Chef package registry (#22554) 2023-02-06 09:49:21 +08:00
sitemap
ssh
storage Implement actions (#21937) 2023-01-31 09:45:19 +08:00
structs change org_type.go to visible_type.go and fix the notes (#22752) 2023-02-09 06:40:34 +00:00
svg
sync
system
templates Implement actions (#21937) 2023-01-31 09:45:19 +08:00
test
timeutil
translation
turnstile Add new captcha: cloudflare turnstile (#22369) 2023-02-05 15:29:03 +08:00
typesniffer
updatechecker
upload
uri
user
util Add Chef package registry (#22554) 2023-02-06 09:49:21 +08:00
validation Map OIDC groups to Orgs/Teams (#21441) 2023-02-08 14:44:42 +08:00
watcher
web Map OIDC groups to Orgs/Teams (#21441) 2023-02-08 14:44:42 +08:00
webhook