Skip to content

Commit 0244811

Browse files
committed
Fix more deepsource issues
1 parent 74d592e commit 0244811

File tree

8 files changed

+38
-19
lines changed

8 files changed

+38
-19
lines changed

cmd/gochan-migration/internal/pre2021/pre2021_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func setupMigrationTest(t *testing.T, outDir string, migrateInPlace bool) *Pre20
3838
}
3939
defer oldDbFile.Close()
4040

41-
newDbFile, err := os.OpenFile(migratedDBHost, os.O_CREATE|os.O_WRONLY, 0644)
41+
newDbFile, err := os.OpenFile(migratedDBHost, os.O_CREATE|os.O_WRONLY, 0600)
4242
if !assert.NoError(t, err) {
4343
t.FailNow()
4444
}

pkg/manage/actionsModPerm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func filterHitsCallback(writer http.ResponseWriter, request *http.Request, staff
219219
errEv.Err(err).Caller().RawJSON("postData", []byte(hit.PostData)).Msg("Unable to marshal un-minified post data")
220220
return nil, err
221221
}
222-
hitsJSON = append(hitsJSON, template.HTML(strings.ReplaceAll(jsonBuf.String(), "\n", "<br>")))
222+
hitsJSON = append(hitsJSON, template.HTML(strings.ReplaceAll(jsonBuf.String(), "\n", "<br>"))) // skipcq: GSC-G203
223223
}
224224
var buf bytes.Buffer
225225
if err = serverutil.MinifyTemplate(gctemplates.ManageFilterHits, map[string]any{

pkg/posting/formatting.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
msgfmtr MessageFormatter
1919
urlRE = regexp.MustCompile(`https?://(\S+)`)
2020
unsetBBcodeTags = []string{"center", "color", "img", "quote", "size"}
21-
diceRoller = regexp.MustCompile(`(?i)\[(\d*)d(\d+)(?:([+-])(\d+))?\]`)
21+
diceRoller = regexp.MustCompile(`(?i)(\S*)\[(\d*)d(\d+)(?:([+-])(\d+))?\](\S*)`)
2222
)
2323

2424
// InitPosting prepares the formatter and the temp post pruner
@@ -142,13 +142,13 @@ func ApplyDiceRoll(p *gcsql.Post) (rollSum int, err error) {
142142
continue
143143
}
144144
numDice := 1
145-
if roll[1] != "" {
146-
numDice, err = strconv.Atoi(roll[1])
145+
if roll[2] != "" {
146+
numDice, err = strconv.Atoi(roll[2])
147147
if err != nil {
148148
return 0, err
149149
}
150150
}
151-
dieSize, err := strconv.Atoi(roll[2])
151+
dieSize, err := strconv.Atoi(roll[3])
152152
if err != nil {
153153
return 0, err
154154
}
@@ -157,27 +157,27 @@ func ApplyDiceRoll(p *gcsql.Post) (rollSum int, err error) {
157157
}
158158
for i := 0; i < numDice; i++ {
159159
rollSum += rand.Intn(dieSize) + 1 // skipcq: GSC-G404
160-
switch roll[3] {
160+
switch roll[4] {
161161
case "+":
162-
mod, err := strconv.Atoi(roll[4])
162+
mod, err := strconv.Atoi(roll[5])
163163
if err != nil {
164164
return 0, err
165165
}
166166
rollSum += mod
167167
case "-":
168-
mod, err := strconv.Atoi(roll[4])
168+
mod, err := strconv.Atoi(roll[5])
169169
if err != nil {
170170
return 0, err
171171
}
172172
rollSum -= mod
173173
}
174174
}
175-
words[w] = fmt.Sprintf(`<span class="dice-roll">%dd%d`, numDice, dieSize)
176-
if roll[3] != "" {
177-
words[w] += roll[3] + roll[4]
175+
words[w] = fmt.Sprintf(`%s<span class="dice-roll">%dd%d`, roll[1], numDice, dieSize)
176+
if roll[4] != "" {
177+
words[w] += roll[4] + roll[5]
178178
}
179-
words[w] += fmt.Sprintf(" = %d</span>", rollSum)
179+
words[w] += fmt.Sprintf(" = %d</span>%s", rollSum, roll[6])
180180
}
181-
p.Message = template.HTML(strings.Join(words, " "))
181+
p.Message = template.HTML(strings.Join(words, " ")) // skipcq: GSC-G203
182182
return
183183
}

pkg/posting/formatting_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ var (
7474
expectMin: 1,
7575
expectMax: 8,
7676
},
77+
{
78+
desc: "before[1d6]after, no space",
79+
post: gcsql.Post{
80+
MessageRaw: "before[1d6]after",
81+
},
82+
matcher: regexp.MustCompile(`before<span class="dice-roll">1d6 = \d</span>after`),
83+
expectMin: 1,
84+
expectMax: 6,
85+
},
86+
{
87+
desc: "before [1d6] after, no space (test for injection)",
88+
post: gcsql.Post{
89+
MessageRaw: `<script>alert("lol")</script>[1d6]<script>alert("lmao")</script>`,
90+
},
91+
expectError: false,
92+
matcher: regexp.MustCompile(`&lt;script&gt;alert\(&#34;lol&#34;\)&lt;/script&gt;<span class="dice-roll">1d6 = \d</span>&lt;script&gt;alert\(&#34;lmao&#34;\)&lt;/script&gt;`),
93+
expectMin: 1,
94+
expectMax: 6,
95+
},
7796
}
7897
)
7998

pkg/server/serverutil/antispam_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type checkRefererTestCase struct {
5252
func TestCheckReferer(t *testing.T) {
5353
config.SetVersion("4.0.0")
5454
systemCriticalConfig := config.GetSystemCriticalConfig()
55-
req, err := http.NewRequest("GET", "http://gochan.org", nil)
55+
req, err := http.NewRequest("GET", "https://gochan.org", nil)
5656
if !assert.NoError(t, err) {
5757
t.FailNow()
5858
}

pkg/server/serverutil/util_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type isRequestingJSONTestCase struct {
3636
}
3737

3838
func TestIsRequestingJSON(t *testing.T) {
39-
req, _ := http.NewRequest("GET", "http://localhost:8080", nil)
39+
req, _ := http.NewRequest("GET", "https://localhost:8080", nil)
4040
assert.False(t, IsRequestingJSON(req))
4141
for _, tc := range isRequestingJSONTestCases {
4242
t.Run("GET "+tc.val, func(t *testing.T) {
@@ -70,7 +70,7 @@ func (w *testResponseWriter) WriteHeader(s int) {
7070
}
7171

7272
func TestDeleteCookie(t *testing.T) {
73-
req, _ := http.NewRequest("GET", "http://localhost:8080", nil)
73+
req, _ := http.NewRequest("GET", "https://localhost:8080", nil)
7474
writer := testResponseWriter{
7575
header: make(http.Header),
7676
}

tools/get_js.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
case _:
2626
out_dir = sys.argv[1]
2727

28-
with urlopen(DOWNLOAD_URL) as response:
28+
with urlopen(DOWNLOAD_URL) as response: # skipcq: BAN-B310
2929
data = response.read()
3030
tar_bytes = gzip.decompress(data)
3131
buf = io.BytesIO(tar_bytes)

tools/selenium_testing/tests/test_staff_permissions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def setUpClass(cls):
2727
req = Request(urljoin(options.site, "manage/actions"))
2828
# modern browsers add pretty printing to JSON so we need to pass the session cookie to a request to get the raw action list data
2929
req.add_header("Cookie", f"sessiondata={cookie}")
30-
with urlopen(req) as resp:
30+
with urlopen(req) as resp: # skipcq: BAN-B310
3131
global actions
3232
actions = json.load(resp)
3333

0 commit comments

Comments
 (0)