mirror of
https://codeberg.org/davrot/forgejo.git
synced 2025-06-17 08:00:03 +02:00
fix: url validation in webhook add/edit API (#7932)
Cherry-pick from 972381097c
(see #7909).
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7932
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Antonin Delpeuch <antonin@delpeuch.eu>
Co-committed-by: Antonin Delpeuch <antonin@delpeuch.eu>
This commit is contained in:
parent
b2a3966e64
commit
2b30c83a0c
3 changed files with 116 additions and 0 deletions
86
routers/api/v1/utils/hook_test.go
Normal file
86
routers/api/v1/utils/hook_test.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"forgejo.org/models/unittest"
|
||||
"forgejo.org/modules/structs"
|
||||
"forgejo.org/services/contexttest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTestHookValidation(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
t.Run("Test Validation", func(t *testing.T) {
|
||||
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
||||
contexttest.LoadRepo(t, ctx, 1)
|
||||
contexttest.LoadGitRepo(t, ctx)
|
||||
contexttest.LoadRepoCommit(t, ctx)
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
|
||||
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
||||
Type: "gitea",
|
||||
Config: map[string]string{
|
||||
"content_type": "json",
|
||||
"url": "https://example.com/webhook",
|
||||
},
|
||||
})
|
||||
assert.Equal(t, 0, ctx.Resp.WrittenStatus()) // not written yet
|
||||
})
|
||||
|
||||
t.Run("Test Validation with invalid URL", func(t *testing.T) {
|
||||
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
||||
contexttest.LoadRepo(t, ctx, 1)
|
||||
contexttest.LoadGitRepo(t, ctx)
|
||||
contexttest.LoadRepoCommit(t, ctx)
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
|
||||
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
||||
Type: "gitea",
|
||||
Config: map[string]string{
|
||||
"content_type": "json",
|
||||
"url": "example.com/webhook",
|
||||
},
|
||||
})
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
||||
})
|
||||
|
||||
t.Run("Test Validation with invalid webhook type", func(t *testing.T) {
|
||||
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
||||
contexttest.LoadRepo(t, ctx, 1)
|
||||
contexttest.LoadGitRepo(t, ctx)
|
||||
contexttest.LoadRepoCommit(t, ctx)
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
|
||||
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
||||
Type: "unknown",
|
||||
Config: map[string]string{
|
||||
"content_type": "json",
|
||||
"url": "example.com/webhook",
|
||||
},
|
||||
})
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
||||
})
|
||||
|
||||
t.Run("Test Validation with empty content type", func(t *testing.T) {
|
||||
ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks")
|
||||
contexttest.LoadRepo(t, ctx, 1)
|
||||
contexttest.LoadGitRepo(t, ctx)
|
||||
contexttest.LoadRepoCommit(t, ctx)
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
|
||||
checkCreateHookOption(ctx, &structs.CreateHookOption{
|
||||
Type: "unknown",
|
||||
Config: map[string]string{
|
||||
"url": "https://example.com/webhook",
|
||||
},
|
||||
})
|
||||
assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus())
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue