1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package admin
import (
"os"
"path/filepath"
"strings"
"testing"
)
// Regression: the raw file API must canonicalize repeated "/" and "data/"
// prefixes, or "//players/..." reaches the real file while the classifiers
// see a non-player path and skip the redaction / online refusal.
func TestCanonicalDataRelPathStripsRepeatedPrefixes(t *testing.T) {
cases := map[string]string{
"players/accounts/x.yaml": "players/accounts/x.yaml",
"/players/accounts/x.yaml": "players/accounts/x.yaml",
"//players/accounts/x.yaml": "players/accounts/x.yaml",
"data/players/accounts/x.yaml": "players/accounts/x.yaml",
"data/data/players/accounts/x.yaml": "players/accounts/x.yaml",
"../etc/passwd": "etc/passwd",
}
for in, want := range cases {
if got := canonicalDataRelPath(in); got != want {
t.Errorf("canonicalDataRelPath(%q) = %q, want %q", in, got, want)
}
}
}
// Regression: a valid-but-non-mapping body used to pass through unchanged and
// blank password_hash. It must now be refused, and a mapping body must keep
// the on-disk hash.
func TestRestoreAccountSecretRefusesNonMappingBody(t *testing.T) {
dir := t.TempDir()
full := filepath.Join(dir, "players", "accounts", "alice.yaml")
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(full, []byte("name: alice\npassword_hash: secret\n"), 0600); err != nil {
t.Fatal(err)
}
if _, err := restoreAccountSecret("players/accounts/alice.yaml", full, []byte("# comment only\n")); err == nil {
t.Fatal("non-mapping body accepted; the password hash would be blanked")
}
out, err := restoreAccountSecret("players/accounts/alice.yaml", full, []byte("name: alice\npassword_hash: \"[redacted]\"\n"))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(out), "secret") {
t.Fatalf("password hash not preserved: %s", out)
}
}
|