snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped

This commit is contained in:
upstream-mirror
2026-09-15 21:41:59 +05:00
commit 52ae0c5802
6280 changed files with 624193 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
// https://github.com/go-gitea/gitea/pull/37492#issuecomment-4355482585
// End users need it "since it is the only one open-source option in Gitea besides the image captcha"
package mcaptcha
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"strings"
"gitea.dev/modules/json"
"gitea.dev/modules/setting"
"gitea.dev/modules/util"
)
func Verify(ctx context.Context, token string) (bool, error) {
c := &client{
ServerURL: setting.Service.McaptchaURL,
SiteKey: setting.Service.McaptchaSitekey,
Secret: setting.Service.McaptchaSecret,
Token: token,
}
return c.Verify(ctx)
}
type client struct {
ServerURL string
Secret string
SiteKey string
Token string
}
func (c *client) Verify(ctx context.Context) (bool, error) {
verifyURL := strings.TrimSuffix(c.ServerURL, "/") + "/api/v1/pow/siteverify"
reqParams := map[string]string{"secret": c.Secret, "key": c.SiteKey, "token": c.Token}
reqBody, _ := json.Marshal(reqParams)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, verifyURL, bytes.NewReader(reqBody))
if err != nil {
return false, fmt.Errorf("unable to create verify request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return false, fmt.Errorf("unable to complete verify request: %w", err)
}
defer res.Body.Close()
respContent, err := io.ReadAll(io.LimitReader(res.Body, 16*1024))
if err != nil {
return false, fmt.Errorf("unable to read response body: %w", err)
}
if res.StatusCode != http.StatusOK {
return false, fmt.Errorf("unexpected response status %d: %q", res.StatusCode, util.TruncateRunes(util.UnsafeBytesToString(respContent), 100))
}
var resp struct {
Valid bool `json:"valid"`
}
err = json.Unmarshal(respContent, &resp)
if err != nil {
return false, fmt.Errorf("unable to decode response: %w", err)
}
return resp.Valid, nil
}

View File

@@ -0,0 +1,53 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package mcaptcha
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"gitea.dev/modules/json"
"gitea.dev/modules/setting"
"gitea.dev/modules/test"
"github.com/stretchr/testify/assert"
)
func TestMCaptchaVerify(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v1/pow/siteverify":
var req map[string]string
_ = json.NewDecoder(r.Body).Decode(&req)
if req["key"] == "test-site-key" {
w.WriteHeader(http.StatusOK)
assert.Equal(t, "test-secret", req["secret"])
resp := map[string]bool{"valid": req["token"] == "token-valid"}
_ = json.NewEncoder(w).Encode(resp)
} else {
w.WriteHeader(http.StatusBadRequest)
}
}
}))
defer srv.Close()
defer test.MockVariableValue(&setting.Service)()
setting.Service.McaptchaURL = strings.TrimSuffix(srv.URL, "/") + "/"
setting.Service.McaptchaSitekey = "test-site-key"
setting.Service.McaptchaSecret = "test-secret"
valid, err := Verify(t.Context(), "token-valid")
assert.NoError(t, err)
assert.True(t, valid)
valid, err = Verify(t.Context(), "token-invalid")
assert.NoError(t, err)
assert.False(t, valid)
setting.Service.McaptchaSitekey = "test-site-key-invalid"
valid, err = Verify(t.Context(), "token-invalid")
assert.Error(t, err)
assert.False(t, valid)
}