Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Upgrade Altcha to v2. Has internal breaking changes in its function and architecture. Closes #3170.

Kailash Nadh515b73213cff
diff --git a/go.mod b/go.mod
index 501add7e..e39563c5 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.27.2
require (
github.com/Masterminds/sprig/v3 v3.3.0
- github.com/altcha-org/altcha-lib-go v1.0.0
+ github.com/altcha-org/altcha-lib-go/v2 v2.4.1
github.com/coreos/go-oidc/v3 v3.21.0
github.com/disintegration/imaging v1.6.2
github.com/emersion/go-message v0.18.2
diff --git a/go.sum b/go.sum
index 3b03f3c2..fd9390bb 100644
--- a/go.sum
+++ b/go.sum
@@ -8,8 +8,8 @@ github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAw
github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
-github.com/altcha-org/altcha-lib-go v1.0.0 h1:7oPti0aUS+YCep8nwt5b9g4jYfCU55ZruWESL8G9K5M=
-github.com/altcha-org/altcha-lib-go v1.0.0/go.mod h1:I8ESLVWR9C58uvGufB/AJDPhaSU4+4Oh3DLpVtgwDAk=
+github.com/altcha-org/altcha-lib-go/v2 v2.4.1 h1:Y1lIhe+Y4QI/w1KV0wsCEfCzSmaBWJgu4I3gAKtHYaU=
+github.com/altcha-org/altcha-lib-go/v2 v2.4.1/go.mod h1:n3KVRk3ziFGC5GIPkiSFMSROrAZ0wezy88///KeYdRo=
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/boombuler/barcode v1.1.0 h1:ChaYjBR63fr4LFyGn8E8nt7dBSt3MiU3zMOZqFvVkHo=
github.com/boombuler/barcode v1.1.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
diff --git a/internal/captcha/captcha.go b/internal/captcha/captcha.go
index 403ed28e..a7d30b1a 100644
--- a/internal/captcha/captcha.go
+++ b/internal/captcha/captcha.go
@@ -6,12 +6,13 @@ import (
"encoding/json"
"fmt"
"io"
+ "math/big"
"net/http"
"net/url"
"strings"
"time"
- "github.com/altcha-org/altcha-lib-go"
+ "github.com/altcha-org/altcha-lib-go/v2"
"github.com/knadh/listmonk/internal/tmptokens"
)
@@ -117,21 +118,35 @@ func (c *Captcha) GetProvider() string {
func (c *Captcha) GenerateChallenge() (string, error) {
switch c.provider {
case ProviderAltcha:
+ if c.altcha.Complexity < 0 {
+ c.altcha.Complexity = 30000
+ }
+
+ // Keep complexity as the maximum number of SHA-256 attempts.
+ // The complexity param from Altcha v1 is gone, so retrofit the
+ // value in user settings to the next config.
+ n, err := rand.Int(rand.Reader, big.NewInt(int64(c.altcha.Complexity)+1))
+ if err != nil {
+ return "", fmt.Errorf("error generating Altcha counter: %w", err)
+ }
+
+ counter := int(n.Int64())
exp := time.Now().Add(5 * time.Minute)
- challenge, err := altcha.CreateChallenge(altcha.ChallengeOptions{
- Algorithm: altcha.SHA256,
- MaxNumber: int64(c.altcha.Complexity),
- SaltLength: 12,
- HMACKey: c.altcha.HMACKey,
- Expires: &exp,
+ challenge, err := altcha.CreateChallenge(altcha.CreateChallengeOptions{
+ Algorithm: string(altcha.SHA256),
+ Cost: 1,
+ Counter: &counter,
+ DeriveKey: altcha.DeriveKeySHA(),
+ HMACSignatureSecret: c.altcha.HMACKey,
+ ExpiresAt: &exp,
})
if err != nil {
- return "", fmt.Errorf("failed to create Altcha challenge: %w", err)
+ return "", fmt.Errorf("error creating Altcha challenge: %w", err)
}
challengeJSON, err := json.Marshal(challenge)
if err != nil {
- return "", fmt.Errorf("failed to marshal Altcha challenge: %w", err)
+ return "", fmt.Errorf("error marshalling Altcha challenge: %w", err)
}
return string(challengeJSON), nil
@@ -185,20 +200,36 @@ func (c *Captcha) verifyHCaptcha(token string) (error, bool) {
// verifyAltcha verifies an Altcha response.
func (c *Captcha) verifyAltcha(payload string) (error, bool) {
- valid, err := altcha.VerifySolution(payload, c.altcha.HMACKey, true)
+ data, err := base64.StdEncoding.DecodeString(payload)
+ if err != nil {
+ return fmt.Errorf("error decoding captcha solution: %w", err), false
+ }
+
+ var solution altcha.Payload
+ if err := json.Unmarshal(data, &solution); err != nil {
+ return fmt.Errorf("error parsing captcha solution: %w", err), false
+ }
+
+ result, err := altcha.VerifySolution(altcha.VerifySolutionOptions{
+ Challenge: solution.Challenge,
+ Solution: solution.Solution,
+ DeriveKey: altcha.DeriveKeySHA(),
+ HMACSignatureSecret: c.altcha.HMACKey,
+ })
if err != nil {
- return fmt.Errorf("failed to verify captcha solution: %w", err), false
+ return fmt.Errorf("error verifying captcha solution: %w", err), false
}
- if !valid {
+ if !result.Verified || result.Expired {
return fmt.Errorf("captcha verification failed"), false
}
// Disallow token reuse.
- if _, err := tmptokens.Check(payload); err == nil {
+ id := "altcha:" + solution.Challenge.Signature
+ if _, err := tmptokens.Check(id); err == nil {
return fmt.Errorf("captcha token already used"), false
}
- tmptokens.Set(payload, 5*time.Minute, nil)
+ tmptokens.Set(id, 5*time.Minute, nil)
return nil, true
}
diff --git a/static/admin/assets/js/views/forms.js b/static/admin/assets/js/views/forms.js
index afae2ed4..093a708b 100644
--- a/static/admin/assets/js/views/forms.js
+++ b/static/admin/assets/js/views/forms.js
@@ -78,7 +78,7 @@ function component(lists = []) {
if (sub.captcha_enabled) {
if (sub.captcha_provider === 'altcha') {
h += '\n'
- + ` <altcha-widget challengeurl="${root}/api/public/captcha/altcha"></altcha-widget>\n`
+ + ` <altcha-widget challenge="${root}/api/public/captcha/altcha"></altcha-widget>\n`
+ ` <${'script'} type="module" src="${root}/public/static/altcha.umd.js" async defer></${'script'}>\n`;
} else if (sub.captcha_provider === 'hcaptcha') {
h += '\n'
diff --git a/static/admin/build.mjs b/static/admin/build.mjs
index 092b577d..2fb57ee6 100644
--- a/static/admin/build.mjs
+++ b/static/admin/build.mjs
@@ -32,7 +32,7 @@ async function build() {
await cp(path.join(root, 'node_modules/@knadh/oat/oat.min.css'), path.join(dist, 'oat.min.css'));
// ALTCHA is loaded directly by public subscription forms.
- await cp(path.join(root, 'node_modules/altcha/dist/altcha.umd.cjs'), path.join(root, '../public/static/altcha.umd.js'));
+ await cp(path.join(root, 'node_modules/altcha/dist/main/altcha.umd.cjs'), path.join(root, '../public/static/altcha.umd.js'));
// Entry points = modules loaded directly by a <script> tag: the global main.js and
// every per-view module under assets/js/views/.
diff --git a/static/admin/bun.lock b/static/admin/bun.lock
index 524b99be..638b28cc 100644
--- a/static/admin/bun.lock
+++ b/static/admin/bun.lock
@@ -17,15 +17,13 @@
"@knadh/oat": "^1.0.0",
"@lezer/highlight": "^1.2.3",
"alpinejs": "^3.15.12",
- "altcha": "^2.3.0",
+ "altcha": "^3.4.1",
"chart.js": "^4.4.1",
"squire-rte": "2.4.8",
},
},
},
"packages": {
- "@altcha/crypto": ["@altcha/[email protected]", "", {}, "sha512-qZMdnoD3lAyvfSUMNtC2adRi666Pxdcw9zqfMU5qBOaJWqpN9K+eqQGWqeiKDMqL0SF+EytNG4kR/Pr/99GJ6g=="],
-
"@codemirror/autocomplete": ["@codemirror/[email protected]", "", { "dependencies": { "@codemirror/language": "^6.0.0", "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.17.0", "@lezer/common": "^1.0.0" } }, "sha512-tlosUqb+3BbxCxZdu4tKeRghPFC+QM7q4X5YhKV2eCmPG+1r2F3f4AaSz5sCrFqUtX4Jh20VFTKecl16MgiV9g=="],
"@codemirror/commands": ["@codemirror/[email protected]", "", { "dependencies": { "@codemirror/language": "^6.0.0", "@codemirror/state": "^6.7.0", "@codemirror/view": "^6.27.0", "@lezer/common": "^1.1.0" } }, "sha512-Ryk9y9T0FFVF0cUGhAknveAyUOl/A1qReTFi+qPKtOh2Z9F4AUBz3XOrYD4ZEgZirdugVzHvd/2/Wcwy5OliTg=="],
@@ -68,20 +66,20 @@
"@marijn/find-cluster-break": ["@marijn/[email protected]", "", {}, "sha512-FY+MKLBoTsLNJF/eLWaOsXGdz6uh3Iu1axjPf6TUq92IYumcTcXWHoS747JARLkcdlJ/Waiaxc5wQfFO8jC6NA=="],
- "@rollup/rollup-linux-x64-gnu": ["@rollup/[email protected]", "", { "os": "linux", "cpu": "x64" }, "sha512-xuglR2rBVHA5UsI8h8UbX4VJ470PtGCf5Vpswh7p2ukaqBGFTnsfzxUBetoWBWymHMxbIG0Cmx7Y9qDZzr648w=="],
-
"@vue/reactivity": ["@vue/[email protected]", "", { "dependencies": { "@vue/shared": "3.1.5" } }, "sha512-1tdfLmNjWG6t/CsPldh+foumYFo3cpyCHgBYQ34ylaMsJ+SNHQ1kApMIa8jN+i593zQuaw3AdWH0nJTARzCFhg=="],
"@vue/shared": ["@vue/[email protected]", "", {}, "sha512-oJ4F3TnvpXaQwZJNF3ZK+kLPHKarDmJjJ6jyzVNDKH9md1dptjC7lWR//jrGuLdek/U6iltWxqAnYOu8gCiOvA=="],
"alpinejs": ["[email protected]", "", { "dependencies": { "@vue/reactivity": "~3.1.1" } }, "sha512-nJvPAQVNPdZZ0NrExJ/kzQco3ijR8LwvCOadQecllESiqT4NyZ/57sN9V2XyvhlBGAbmlKYgeWZvYdKq99ij/Q=="],
- "altcha": ["[email protected]", "", { "dependencies": { "@altcha/crypto": "^0.0.1" }, "optionalDependencies": { "@rollup/rollup-linux-x64-gnu": "4.18.0" } }, "sha512-vl8I0dQvSQB7/Mx09XuWZ1+LdSP7vEda6OLbg9kUQ2ZO2LT7MzgUyLK7Iips+GAV6c0ntVcS1XWOqhEPpwbDhQ=="],
+ "altcha": ["[email protected]", "", { "dependencies": { "hash-wasm": "^4.12.0" } }, "sha512-K+5Mm1kvdFb+MWr6zIttXXvpGWXTYKD/SxYgXG3TbeeVjEny9DgqyevcuXui4GLtx0roIqC2KYYhSnp5/Vdzbg=="],
"chart.js": ["[email protected]", "", { "dependencies": { "@kurkle/color": "^0.3.0" } }, "sha512-GIjfiT9dbmHRiYi6Nl2yFCq7kkwdkp1W/lp2J99rX0yo9tgJGn3lKQATztIjb5tVtevcBtIdICNWqlq5+E8/Pw=="],
"crelt": ["[email protected]", "", {}, "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA=="],
+ "hash-wasm": ["[email protected]", "", {}, "sha512-+/2B2rYLb48I/evdOIhP+K/DD2ca2fgBjp6O+GBEnCDk2e4rpeXIK8GvIyRPjTezgmWn9gmKwkQjjx6BtqDHVQ=="],
+
"squire-rte": ["[email protected]", "", {}, "sha512-pUp+LhZf2cUlhRKnD40DopUCdhmHlYUF7zGYqOBr8I/tQk/qZie/nXQ1DQcKlXqlr8Bw16TpE/7LQWxUeVrFNg=="],
"style-mod": ["[email protected]", "", {}, "sha512-i/n8VsZydrugj3Iuzll8+x/00GH2vnYsk1eomD8QiRrSAeW6ItbCQDtfXCeJHd0iwiNagqjQkvpvREEPtW3IoQ=="],
diff --git a/static/admin/package.json b/static/admin/package.json
index 2d308f49..04e06dde 100644
--- a/static/admin/package.json
+++ b/static/admin/package.json
@@ -21,7 +21,7 @@
"@knadh/oat": "^1.0.0",
"@lezer/highlight": "^1.2.3",
"alpinejs": "^3.15.12",
- "altcha": "^2.3.0",
+ "altcha": "^3.4.1",
"chart.js": "^4.4.1",
"squire-rte": "2.4.8"
}
diff --git a/static/public/templates/subscription-form.html b/static/public/templates/subscription-form.html
index 15858e10..a1b05dcc 100644
--- a/static/public/templates/subscription-form.html
+++ b/static/public/templates/subscription-form.html
@@ -35,7 +35,7 @@
<div class="h-captcha" data-sitekey="{{ .Data.Captcha.Key }}"></div>
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
{{ else if eq .Data.Captcha.Provider "altcha" }}
- <altcha-widget challengeurl="{{ .RootURL }}/api/public/captcha/altcha"></altcha-widget>
+ <altcha-widget challenge="{{ .RootURL }}/api/public/captcha/altcha"></altcha-widget>
<script type="module" src="{{ .RootURL }}/public/static/altcha.umd.js" async defer></script>
{{ end }}
</div>