Import Anubis v1.28.2
Replace the tree with upstream TecharoHQ/anubis v1.28.2 (a1384fbf42e3). Masyunis customizations are re-applied in the following commits.
This commit is contained in:
1 parent
eb3e46d8d0
commit
e07de898cd
573 files changed
+35846
-6807
No files matched your search
@@ -0,0 +1,28 @@
|
||||
// Package challengepage holds the parts of the challenge page that every
|
||||
// JavaScript-driven challenge method needs.
|
||||
package challengepage
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"io"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
//go:generate ./build.sh
|
||||
|
||||
//go:embed static/js/bootstrap.mjs
|
||||
var scriptBytes string
|
||||
|
||||
// Bootstrap renders the inline watchdog script that re-injects main.mjs when
|
||||
// it fails to load. It must be rendered after the script tag it watches so
|
||||
// that the tag exists by the time this runs during page parse.
|
||||
func Bootstrap() templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
|
||||
if _, err := io.WriteString(w, "<script>"+scriptBytes+"</script>"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
LICENSE='/*
|
||||
@licstart The following is the entire license notice for the
|
||||
JavaScript code in this page.
|
||||
|
||||
Copyright (c) 2026 Xe Iaso <xe.iaso@techaro.lol>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
@licend The above is the entire license notice
|
||||
for the JavaScript code in this page.
|
||||
|
||||
## Rationale
|
||||
|
||||
This script catches `main.mjs` failing to load and re-injects it with
|
||||
jittered exponential backoff.
|
||||
|
||||
Without this any failures loading `main.mjs` leaves the challenge page
|
||||
sitting on the "Loading..." message forever with no error messaging
|
||||
outside of the browser console, which is unavailable on mobile browsers.
|
||||
|
||||
Disable loading this script at your peril. You have been warned.
|
||||
*/'
|
||||
|
||||
mkdir -p static/js
|
||||
|
||||
shopt -s nullglob globstar
|
||||
|
||||
for file in js/**/*.ts js/**/*.mjs; do
|
||||
# Mirrors web/build.sh: js/lib/ is for shared modules, not entry points.
|
||||
if [[ "$file" == js/lib/* ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
out="static/${file}"
|
||||
if [[ "$file" == *.ts ]]; then
|
||||
out="static/${file%.ts}.mjs"
|
||||
fi
|
||||
|
||||
mkdir -p "$(dirname "$out")"
|
||||
|
||||
esbuild "$file" --bundle --minify --target=chrome66 --outfile="$out" --banner:js="$LICENSE"
|
||||
done
|
||||
@@ -0,0 +1,20 @@
|
||||
package challengepage
|
||||
|
||||
import (
|
||||
"html"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
//go:generate go tool github.com/a-h/templ/cmd/templ generate
|
||||
|
||||
// wikipediaLink matches an escaped Wikipedia anchor tag in translated text.
|
||||
// The URL character set is restricted so that nothing in it can break out of
|
||||
// the href attribute.
|
||||
var wikipediaLink = regexp.MustCompile(`<a href="(https://[a-z-]{2,12}\.wikipedia\.org/wiki/[A-Za-z0-9_-]+)">(.*?)</a>`)
|
||||
|
||||
// explanationHTML escapes translated text and then restores only anchor tags
|
||||
// pointing at Wikipedia. Translations are community-contributed, so they must
|
||||
// never be rendered as raw HTML.
|
||||
func explanationHTML(text string) string {
|
||||
return wikipediaLink.ReplaceAllString(html.EscapeString(text), `<a href="$1">$2</a>`)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package challengepage
|
||||
|
||||
import "github.com/TecharoHQ/anubis/lib/localization"
|
||||
|
||||
// Explanation renders the "Why am I seeing this?" details block.
|
||||
templ Explanation(localizer *localization.SimpleLocalizer) {
|
||||
<details>
|
||||
<p>
|
||||
@templ.Raw(explanationHTML(localizer.T("simplified_explanation")))
|
||||
</p>
|
||||
<p>
|
||||
{ localizer.T("jshelter_note") }
|
||||
</p>
|
||||
</details>
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
// Code generated by templ - DO NOT EDIT.
|
||||
|
||||
// templ: version: v0.3.1070
|
||||
package challengepage
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import "github.com/TecharoHQ/anubis/lib/localization"
|
||||
|
||||
// Explanation renders the "Why am I seeing this?" details block.
|
||||
func Explanation(localizer *localization.SimpleLocalizer) templ.Component {
|
||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||
return templ_7745c5c3_CtxErr
|
||||
}
|
||||
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
|
||||
if !templ_7745c5c3_IsBuffer {
|
||||
defer func() {
|
||||
templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err == nil {
|
||||
templ_7745c5c3_Err = templ_7745c5c3_BufErr
|
||||
}
|
||||
}()
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
|
||||
if templ_7745c5c3_Var1 == nil {
|
||||
templ_7745c5c3_Var1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<details><p>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ.Raw(explanationHTML(localizer.T("simplified_explanation"))).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "</p><p>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
var templ_7745c5c3_Var2 string
|
||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(localizer.T("jshelter_note"))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `explanation.templ`, Line: 12, Col: 33}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</p></details>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
@@ -0,0 +1,103 @@
|
||||
package challengepage
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/TecharoHQ/anubis/lib/localization"
|
||||
)
|
||||
|
||||
func TestExplanationHTML(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "plain text",
|
||||
in: "hello & goodbye",
|
||||
want: "hello & goodbye",
|
||||
},
|
||||
{
|
||||
name: "wikipedia link",
|
||||
in: `called <a href="https://en.wikipedia.org/wiki/Proof_of_work">Proof of Work</a>.`,
|
||||
want: `called <a href="https://en.wikipedia.org/wiki/Proof_of_work">Proof of Work</a>.`,
|
||||
},
|
||||
{
|
||||
name: "localized wikipedia link",
|
||||
in: `<a href="https://de.wikipedia.org/wiki/Proof_of_Work">Proof of Work</a>`,
|
||||
want: `<a href="https://de.wikipedia.org/wiki/Proof_of_Work">Proof of Work</a>`,
|
||||
},
|
||||
{
|
||||
name: "script tag",
|
||||
in: `<script>alert(1)</script>`,
|
||||
want: `<script>alert(1)</script>`,
|
||||
},
|
||||
{
|
||||
name: "javascript href",
|
||||
in: `<a href="javascript:alert(1)">x</a>`,
|
||||
want: `<a href="javascript:alert(1)">x</a>`,
|
||||
},
|
||||
{
|
||||
name: "attribute breakout",
|
||||
in: `<a href="https://en.wikipedia.org/wiki/X" onclick="alert(1)">x</a>`,
|
||||
want: `<a href="https://en.wikipedia.org/wiki/X" onclick="alert(1)">x</a>`,
|
||||
},
|
||||
{
|
||||
name: "markup inside link text",
|
||||
in: `<a href="https://en.wikipedia.org/wiki/X"><img src=x onerror=alert(1)></a>`,
|
||||
want: `<a href="https://en.wikipedia.org/wiki/X"><img src=x onerror=alert(1)></a>`,
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := explanationHTML(tt.in); got != tt.want {
|
||||
t.Errorf("explanationHTML(%q)\n got: %s\nwant: %s", tt.in, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestExplanationRendersLink makes sure no shipped translation of
|
||||
// simplified_explanation renders its Proof of Work link as escaped text.
|
||||
func TestExplanationRendersLink(t *testing.T) {
|
||||
files, err := filepath.Glob("../../localization/locales/*.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, fname := range files {
|
||||
lang := strings.TrimSuffix(filepath.Base(fname), ".json")
|
||||
if lang == "manifest" {
|
||||
continue
|
||||
}
|
||||
|
||||
t.Run(lang, func(t *testing.T) {
|
||||
data, err := os.ReadFile(fname)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(data), "wikipedia.org") {
|
||||
t.Skip("translation has no link")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
req.Header.Set("Accept-Language", lang)
|
||||
|
||||
var sb strings.Builder
|
||||
if err := Explanation(localization.GetLocalizer(req)).Render(t.Context(), &sb); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := sb.String()
|
||||
|
||||
if strings.Contains(out, "<a") || strings.Contains(out, "</a") {
|
||||
t.Errorf("rendered explanation contains escaped anchor markup: %s", out)
|
||||
}
|
||||
if !strings.Contains(out, `<a href="https://`) {
|
||||
t.Errorf("rendered explanation has no link: %s", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
// Watch for `main.mjs` failing to load and re-injects it with jittered
|
||||
// exponential backoff.
|
||||
//
|
||||
// Without this any failures loading `main.mjs` leaves the challenge page
|
||||
// sitting on the "Loading..." message forever with no error messaging
|
||||
// outside of the browser console, which is unavailable on mobile browsers.
|
||||
//
|
||||
// This is the worst way for Anubis to fail because it requires the user
|
||||
// to manually reload the webpage, which fails the main design goal of
|
||||
// the proof of work challenges: no user interaction is required to pass
|
||||
// challenges. Paradoxically, this would increase the level of load to
|
||||
// an already overwhelmed server, making the problem much worse.
|
||||
//
|
||||
// This is injected into the page as a raw constant embedded from this
|
||||
// file instead of loaded dynamically so that things are more reliable.
|
||||
//
|
||||
// A few notes on the implementation of this and why it makes some weird
|
||||
// assumptions about the browser environment:
|
||||
//
|
||||
// - Loading a module script uses a slightly different internal fetch path
|
||||
// than loading it as a normal javascript script. As such, if you try to
|
||||
// re-load _the exact same_ URL you end up getting a cached fetch
|
||||
// failure, which then amplifies the problem and makes the logic _never_
|
||||
// load. This is bad, so we have to inject extra URL parameters to the
|
||||
// Anubis server.
|
||||
// - The canonical way to solve this in a `<script>` tag is the
|
||||
// `script.onerror` property, which sadly does not fire for every kind of
|
||||
// failure that is relevant to Anubis. As a result we have to use a
|
||||
// watchdog timer to ensure the `main.mjs` file actually booted.
|
||||
|
||||
import { backoffDelay } from "@lib/backoff";
|
||||
import { g, h } from "@lib/xeact.mjs";
|
||||
|
||||
const getMany = (ids: string[]): (HTMLElement | null)[] => ids.map(g);
|
||||
|
||||
(() => {
|
||||
const tag = g("anubis-main");
|
||||
if (!tag) {
|
||||
console.debug(
|
||||
"can't find anubis main.mjs element via ID `anubis-main`, bailing",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const src = tag.getAttribute("src");
|
||||
if (!src) {
|
||||
console.debug(
|
||||
"can't find src attribute of script element `anubis-main`, bailing",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const joiner = src.indexOf("?") === -1 ? "?" : "&";
|
||||
const MAX_ATTEMPTS = 4;
|
||||
const BASE_DELAY_MS = 750;
|
||||
const MAX_DELAY_MS = 10000;
|
||||
// XXX(Xe): this timer is overly generous to account for mobile connections
|
||||
// and/or German train Wi-Fi.
|
||||
const WATCHDOG_MS = 10000;
|
||||
let attempt = 0;
|
||||
let generation = 0;
|
||||
|
||||
const booted = () =>
|
||||
// @ts-ignore: global variable set by main.mjs
|
||||
(window as any).__anubisBooted === true;
|
||||
|
||||
const fail = () => {
|
||||
const els = getMany(["anubis-script-error", "status", "progress"]);
|
||||
if (els.filter((x) => x == null).length !== 0) {
|
||||
console.debug(
|
||||
"missing one of the following elements: anubis-script-error, status, progress. cannot proceed, bailing.",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const [el, status, progress] = els as HTMLElement[];
|
||||
el.style.display = "block";
|
||||
status.style.display = "none";
|
||||
progress.style.display = "none";
|
||||
};
|
||||
|
||||
const settleFor = (gen: number) => () => {
|
||||
if (booted() || gen !== generation) {
|
||||
return;
|
||||
}
|
||||
|
||||
generation++;
|
||||
attempt++;
|
||||
if (attempt > MAX_ATTEMPTS) {
|
||||
fail();
|
||||
return;
|
||||
}
|
||||
|
||||
const delay = backoffDelay(attempt, BASE_DELAY_MS, MAX_DELAY_MS);
|
||||
setTimeout(inject, delay);
|
||||
};
|
||||
|
||||
const inject = () => {
|
||||
if (booted()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const settle = settleFor(generation);
|
||||
const s = h("script", {
|
||||
async: true,
|
||||
type: "module",
|
||||
src: src + joiner + "anubisRetry=" + attempt,
|
||||
onerror: settle,
|
||||
});
|
||||
document.head.appendChild(s);
|
||||
setTimeout(settle, WATCHDOG_MS);
|
||||
};
|
||||
|
||||
const initial = settleFor(0);
|
||||
tag.onerror = initial;
|
||||
setTimeout(initial, WATCHDOG_MS);
|
||||
})();
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
@licstart The following is the entire license notice for the
|
||||
JavaScript code in this page.
|
||||
|
||||
Copyright (c) 2026 Xe Iaso <xe.iaso@techaro.lol>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
@licend The above is the entire license notice
|
||||
for the JavaScript code in this page.
|
||||
|
||||
## Rationale
|
||||
|
||||
This script catches `main.mjs` failing to load and re-injects it with
|
||||
jittered exponential backoff.
|
||||
|
||||
Without this any failures loading `main.mjs` leaves the challenge page
|
||||
sitting on the "Loading..." message forever with no error messaging
|
||||
outside of the browser console, which is unavailable on mobile browsers.
|
||||
|
||||
Disable loading this script at your peril. You have been warned.
|
||||
*/
|
||||
(()=>{var f=(e,t=750,n=1e4)=>Math.random()*Math.min(n,t*Math.pow(e,2));var d=(e,t={},n=[])=>{let s=typeof e=="function"?e(t):Object.assign(document.createElement(e),t);return Array.isArray(n)||(n=[n]),s.append(...n),s};var i=e=>document.getElementById(e);var _=e=>e.map(i);(()=>{let e=i("anubis-main");if(!e){console.debug("can't find anubis main.mjs element via ID `anubis-main`, bailing");return}let t=e.getAttribute("src");if(!t){console.debug("can't find src attribute of script element `anubis-main`, bailing");return}let n=t.indexOf("?")===-1?"?":"&",s=4,E=750,A=1e4,c=1e4,a=0,l=0,u=()=>window.__anubisBooted===!0,p=()=>{let r=_(["anubis-script-error","status","progress"]);if(r.filter(D=>D==null).length!==0){console.debug("missing one of the following elements: anubis-script-error, status, progress. cannot proceed, bailing.");return}let[o,M,T]=r;o.style.display="block",M.style.display="none",T.style.display="none"},m=r=>()=>{if(u()||r!==l)return;if(l++,a++,a>s){p();return}let o=f(a,E,A);setTimeout(y,o)},y=()=>{if(u())return;let r=m(l),o=d("script",{async:!0,type:"module",src:t+n+"anubisRetry="+a,onerror:r});document.head.appendChild(o),setTimeout(r,c)},b=m(0);e.onerror=b,setTimeout(b,c)})();})();
|
||||
Reference in new issue
Block a user