Files
masyunis/internal/cmd/mkmsi/build.go
T
sol_bot Bot caf4d72880 Update to Anubis v1.28.2 (#1)
This upgrades the Masyunis fork from Anubis v1.25.0 to the latest stable release, **v1.28.2** (“Wuk Lamat Echo 2”), while keeping the fork’s custom styling and removals.

## Approach

The Gitea repo is a tree snapshot of v1.25.0, not a git-level fork of [TecharoHQ/anubis](https://github.com/TecharoHQ/anubis). The histories share no commits, so a merge or rebase onto the upstream tag is not usable.

The first commit on this branch replaces the tree with a snapshot of the **v1.28.2** tag (`git rm -r` plus `git checkout v1.28.2 -- .`; the import tree is identical to the tag). A short patch series on top restores the Masyunis customizations.

## Customizations kept

- Masyunya mascots (`web/static/img/{happy,pensive,reject}.webp`), hashes match current `master`.
- `web/static/img/ATTRIBUTIONS.txt` stays deleted.
- Challenge footer: drop the `made_with` (Canada / maple-leaf) line and the CELPHASE mascot credit; keep “Protected by Anubis from Techaro”. The generated `web/index_templ.go` matches `go generate ./web`.
- Short README (the old `anubisc` typo is fixed).
- Version stamped `v1.28.2/masyunis` via Makefile ldflags. `anubis.go` `init()` does not overwrite a link-time version; `go run` / `go test` still report `devel` on purpose.
- Upstream `.github`, `.devcontainer`, `.husky`, and `.vscode` stay deleted.
- `"uk"` is removed from `lib/localization/locales/manifest.json` **on purpose**, because of Alpine locale problems. `uk.json` and its test entry stay; the served manifest lists 27 languages.
- `package.json` `prepare` no longer runs `husky` (the hooks directory is gone). The rest of the script is unchanged. `husky` remains in devDependencies, as upstream left it.

## Deploy notes

- **Cookie rename (v1.27):** with the default `DYNAMIC_COOKIE_SUFFIX=true`, cookie names become e.g. `techaro.lol-anubis-auth-<hash>`. Existing clients get a **one-time re-challenge**. That is expected, not a loop.
- **Docker / ko / yeet builds** skip the Makefile ldflags, so the displayed version is `<git-version>/masyunis`. It is `v1.28.2/masyunis` only when built from a `v1.28.2` tag (or via `make build` / `make prebaked-build`).
- Go 1.27.3 is required (upstream bump in v1.28.2). Thoth (`THOTH_URL` / `THOTH_TOKEN`) was removed upstream; those settings now warn and do nothing. New WASM challenge methods stay disabled unless opted in.

## Verification

- `make build` succeeds. `./var/anubis --version` prints `Anubis v1.28.2/masyunis`.
- `SKIP_INTEGRATION=1 go test ./...` passes (unit tests; integration package is skipped).
- Local challenge/reject pages (en and ru) show Masyunya, version `v1.28.2/masyunis`, and no Canada / CELPHASE / mascot-credit text. Screenshots:

![Challenge page (en)](https://git.unvr.site/attachments/36112ff0-5155-495c-8bd2-59606ee3bd64)
![Reject page (en)](https://git.unvr.site/attachments/2cad0256-1b0e-4f71-8d06-d1a447fd5263)

- Upstream Playwright tests (`make test` without `SKIP_INTEGRATION`) fail on this box because the browser download fails. A clean v1.28.2 tree fails the same three Playwright tests here, so this is not a fork regression.
- `npm audit` reports **4** findings in upstream’s lockfile (2 moderate, 2 high: `brace-expansion`, `fast-uri`, `lodash.trim`, `source-map-js`). Left alone.

Please review and merge when ready. I will not merge this myself.

Reviewed-on: #1
Co-authored-by: Lead <3+lead_bot@noreply.git.unvr.site>
2026-11-28 12:10:27 +00:00

202 lines
6.5 KiB
Go

package main
import (
"archive/zip"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
)
// ErrWixlDroppedAttribute reports that wixl did not understand part of the wxs.
//
// wixl writes a warning to stderr and then exits 0 with an installer that is
// missing whatever it did not understand, so its exit status cannot be trusted
// on its own.
var ErrWixlDroppedAttribute = errors.New("mkmsi: wixl rejected part of the wxs")
// zipNameRe pulls the version and architecture out of a yeet zip name such as
// anubis-1.26.2-1-gec3cce8a-dev-windows-amd64.zip.
var zipNameRe = regexp.MustCompile(`^anubis-(.+)-windows-(amd64|arm64)\.zip$`)
// versionFromZipName extracts the Anubis version from a zip file's name.
func versionFromZipName(name string) (string, error) {
m := zipNameRe.FindStringSubmatch(filepath.Base(name))
if m == nil {
return "", fmt.Errorf("%w: %q is not an Anubis Windows zip", ErrBadVersion, name)
}
return m[1], nil
}
// archFromZipName extracts the Go architecture from a zip file's name.
func archFromZipName(name string) (string, error) {
m := zipNameRe.FindStringSubmatch(filepath.Base(name))
if m == nil {
return "", fmt.Errorf("%w: %q is not an Anubis Windows zip", ErrBadVersion, name)
}
return m[2], nil
}
// checkWixlOutput fails when wixl reported that it could not handle something.
func checkWixlOutput(stderr string) error {
for line := range strings.SplitSeq(stderr, "\n") {
if strings.Contains(line, "CRITICAL") {
return fmt.Errorf("%w: %s", ErrWixlDroppedAttribute, strings.TrimSpace(line))
}
}
return nil
}
// unzip extracts src into dest, stripping the single top level directory that
// yeet puts in its archives. It returns the list of extracted file paths,
// relative to dest and sorted, so downstream steps are reproducible, along
// with the single modification time yeet stamped every entry with.
//
// That shared mtime is handed back rather than rediscovered so files this
// program generates itself after unzipping -- the config templates
// stageConfigTemplates writes -- can be stamped with it too. Without that,
// only files that came straight out of the zip would have a reproducible
// mtime, and the CAB archive wixl builds embeds every file's mtime
// regardless of where it came from.
func unzip(src, dest string) ([]string, time.Time, error) {
r, err := zip.OpenReader(src)
if err != nil {
return nil, time.Time{}, fmt.Errorf("mkmsi: cannot open %s: %w", src, err)
}
defer func() { _ = r.Close() }()
if len(r.File) == 0 {
return nil, time.Time{}, fmt.Errorf("mkmsi: %s has no entries", src)
}
// yeet stamps every entry in the zip with the same mtime; verified
// empirically across a real build's 151 entries. Any entry's mtime is
// therefore as good as any other's.
srcMTime := r.File[0].Modified
var names []string
for _, f := range r.File {
// yeet wraps everything in one directory named after the package.
rel := f.Name
if i := strings.IndexByte(rel, '/'); i >= 0 {
rel = rel[i+1:]
}
if rel == "" {
continue
}
// Reject traversal before touching the filesystem: the zip is built
// locally, but this program also runs over files from CI artifacts.
out := filepath.Join(dest, filepath.FromSlash(rel))
if !strings.HasPrefix(out, filepath.Clean(dest)+string(os.PathSeparator)) {
return nil, time.Time{}, fmt.Errorf("mkmsi: refusing path %q outside %s", f.Name, dest)
}
if f.FileInfo().IsDir() {
if err := os.MkdirAll(out, 0o755); err != nil {
return nil, time.Time{}, fmt.Errorf("mkmsi: cannot create %s: %w", out, err)
}
continue
}
if err := extractOne(f, out); err != nil {
return nil, time.Time{}, err
}
names = append(names, rel)
}
slices.Sort(names)
return names, srcMTime, nil
}
// extractOne writes a single zip entry to disk.
func extractOne(f *zip.File, out string) error {
if err := os.MkdirAll(filepath.Dir(out), 0o755); err != nil {
return fmt.Errorf("mkmsi: cannot create %s: %w", filepath.Dir(out), err)
}
rc, err := f.Open()
if err != nil {
return fmt.Errorf("mkmsi: cannot read %s from zip: %w", f.Name, err)
}
defer func() { _ = rc.Close() }()
w, err := os.OpenFile(out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode().Perm()|0o200)
if err != nil {
return fmt.Errorf("mkmsi: cannot create %s: %w", out, err)
}
if _, err := io.Copy(w, rc); err != nil {
_ = w.Close()
return fmt.Errorf("mkmsi: cannot write %s: %w", out, err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("mkmsi: cannot close %s: %w", out, err)
}
// yeet pins every zip entry's modification time to a fixed epoch so its
// zips are byte-identical across builds. os.OpenFile above stamps the
// file with the current wall clock instead, and the CAB archive wixl
// builds embeds each file's mtime -- so without restoring it here, the
// CAB stream (and therefore the MSI) differs on every build even when
// the input zip is identical. This must run after w.Close(), not
// before: closing a file does not touch its mtime, but running before
// the copy finishes would use the file's creation time instead.
if err := os.Chtimes(out, f.Modified, f.Modified); err != nil {
return fmt.Errorf("mkmsi: cannot set mtime on %s: %w", out, err)
}
return nil
}
// concatFiles writes the contents of srcs, in order, to dest, then stamps it
// with mtime.
//
// The stamp matters for the same reason extractOne restores one on every
// file it extracts from the zip: dest is a File the MSI installs, so its
// mtime is embedded in the CAB archive wixl builds. Left at the wall clock
// os.WriteFile would otherwise use, it would make the CAB -- and therefore
// the MSI -- differ on every build even when every other input is identical.
// mtime is srcMTime, the single mtime yeet stamped every entry in the source
// zip with (see unzip), so these generated files end up sharing it rather
// than inventing a separate fixed point in time of their own.
func concatFiles(dest string, mtime time.Time, srcs ...string) error {
var buf []byte
for _, src := range srcs {
data, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("mkmsi: cannot read %s: %w", src, err)
}
buf = append(buf, data...)
}
if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
return fmt.Errorf("mkmsi: cannot create %s: %w", filepath.Dir(dest), err)
}
if err := os.WriteFile(dest, buf, 0o644); err != nil {
return fmt.Errorf("mkmsi: cannot write %s: %w", dest, err)
}
if err := os.Chtimes(dest, mtime, mtime); err != nil {
return fmt.Errorf("mkmsi: cannot set mtime on %s: %w", dest, err)
}
return nil
}