feat: add browser upload queue and trusted sessions with bounded projections
This commit is contained in:
1 parent
84d52390f9
commit
b90aef79d0
79 files changed
+1962
-196
No files matched your search
@@ -0,0 +1,85 @@
|
||||
package geckorelay
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
const DistributionPrefix = "/distribution/v0.0.1/"
|
||||
|
||||
// Updated only after the reproducible source/notice bundle has been reviewed.
|
||||
const DistributionManifestSHA256 = "84953114f5e393dd97d9f948a5bc51c95b9355b58461dd3355e068a96cb2e381"
|
||||
|
||||
type DistributionFile struct {
|
||||
File string `json:"file"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Size int64 `json:"size"`
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
|
||||
var distributionName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._+-]{0,150}$`)
|
||||
|
||||
func VerifyDistribution(dir string) (map[string]DistributionFile, error) {
|
||||
return verifyDistribution(dir, DistributionManifestSHA256)
|
||||
}
|
||||
|
||||
func verifyDistribution(dir, expected string) (map[string]DistributionFile, error) {
|
||||
path := filepath.Join(dir, "manifest.json")
|
||||
info, err := os.Lstat(path)
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
|
||||
return nil, fmt.Errorf("Gecko distribution manifest missing or invalid")
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sum := sha256.Sum256(raw)
|
||||
if hex.EncodeToString(sum[:]) != expected {
|
||||
return nil, fmt.Errorf("Gecko distribution manifest checksum mismatch")
|
||||
}
|
||||
var manifest struct {
|
||||
Format string `json:"format"`
|
||||
Version string `json:"version"`
|
||||
Files []DistributionFile `json:"files"`
|
||||
}
|
||||
if json.Unmarshal(raw, &manifest) != nil || manifest.Format != "pageswiki.gecko-distribution.v1" || manifest.Version != "v0.0.1" || len(manifest.Files) > 512 {
|
||||
return nil, fmt.Errorf("invalid Gecko distribution format")
|
||||
}
|
||||
files := map[string]DistributionFile{}
|
||||
for _, entry := range manifest.Files {
|
||||
if !distributionName.MatchString(entry.File) || entry.File == "manifest.json" || entry.Size < 1 || entry.Size > 2<<30 || len(entry.SHA256) != 64 {
|
||||
return nil, fmt.Errorf("invalid distribution entry")
|
||||
}
|
||||
if _, exists := files[entry.File]; exists {
|
||||
return nil, fmt.Errorf("duplicate distribution entry")
|
||||
}
|
||||
info, err := os.Lstat(filepath.Join(dir, entry.File))
|
||||
if err != nil || !info.Mode().IsRegular() || info.Size() != entry.Size {
|
||||
return nil, fmt.Errorf("distribution file missing or invalid: %s", entry.File)
|
||||
}
|
||||
file, err := os.Open(filepath.Join(dir, entry.File))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
_, err = io.Copy(hash, io.LimitReader(file, entry.Size+1))
|
||||
file.Close()
|
||||
if err != nil || hex.EncodeToString(hash.Sum(nil)) != entry.SHA256 {
|
||||
return nil, fmt.Errorf("distribution file checksum mismatch: %s", entry.File)
|
||||
}
|
||||
files[entry.File] = entry
|
||||
}
|
||||
for _, name := range []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"} {
|
||||
if _, ok := files[name]; !ok {
|
||||
return nil, fmt.Errorf("distribution incomplete: %s", name)
|
||||
}
|
||||
}
|
||||
files["manifest.json"] = DistributionFile{File: "manifest.json", SHA256: expected, Size: int64(len(raw)), Kind: "manifest"}
|
||||
return files, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package geckorelay
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDistribution(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, err := VerifyDistribution(dir); err == nil {
|
||||
t.Fatal("missing distribution accepted")
|
||||
}
|
||||
names := []string{"index.html", "README.txt", "gecko.js", "gecko.wasm.zst", "firefox.tar.gz", "firefox.tar.gz.notices.txt", "wrapper.tar.gz", "wrapper-MPL.txt", "emscripten.tar.gz", "emscripten.tar.gz.notices.txt", "rust-src.tar.xz", "rust-src.tar.xz.notices.txt", "zstddec.tgz", "zstddec.tgz.notices.txt", "NotoSansCJKjp-Regular.otf", "NotoSansCJK-LICENSE.txt"}
|
||||
content := []byte("synthetic source and notice")
|
||||
hash := sha256.Sum256(content)
|
||||
var entries []DistributionFile
|
||||
for _, name := range names {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), content, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries = append(entries, DistributionFile{File: name, Size: int64(len(content)), SHA256: hex.EncodeToString(hash[:])})
|
||||
}
|
||||
raw, _ := json.Marshal(map[string]any{"format": "pageswiki.gecko-distribution.v1", "version": "v0.0.1", "files": entries})
|
||||
if err := os.WriteFile(filepath.Join(dir, "manifest.json"), raw, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sum := sha256.Sum256(raw)
|
||||
want := hex.EncodeToString(sum[:])
|
||||
files, err := verifyDistribution(dir, want)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
relay, _ := New(testKey, dir)
|
||||
relay.Distribution = files
|
||||
for _, name := range []string{"index.html", "firefox.tar.gz", "manifest.json", "unknown", "../manifest.json"} {
|
||||
req := httptest.NewRequest("GET", DistributionPrefix+name, nil)
|
||||
req.Header.Set(GrantHeader, Sign(testKey, Grant{Subject: "test:user", Nonce: "download-test-0123456789", Expires: time.Now().Add(30 * time.Second).Unix()}))
|
||||
rec := httptest.NewRecorder()
|
||||
relay.ServeHTTP(rec, req)
|
||||
expected := 200
|
||||
if strings.Contains(name, "unknown") || strings.Contains(name, "../") {
|
||||
expected = 404
|
||||
}
|
||||
if rec.Code != expected {
|
||||
t.Fatalf("%s: %d", name, rec.Code)
|
||||
}
|
||||
if expected == 200 && rec.Header().Get("ETag") == "" {
|
||||
t.Fatal("no ETag")
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "README.txt"), []byte("tampered"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := verifyDistribution(dir, want); err == nil {
|
||||
t.Fatal("tampered notice accepted")
|
||||
}
|
||||
if err := os.Remove(filepath.Join(dir, "README.txt")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(filepath.Join(dir, "gecko.js"), filepath.Join(dir, "README.txt")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := verifyDistribution(dir, want); err == nil {
|
||||
t.Fatal("symlink accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDistributionRealBundle(t *testing.T) {
|
||||
dir := os.Getenv("PAGESWIKI_GECKO_TEST_BUNDLE")
|
||||
if dir == "" {
|
||||
t.Skip("opt-in complete distribution")
|
||||
}
|
||||
if _, err := VerifyDistribution(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -28,6 +29,8 @@ const (
|
||||
|
||||
type Relay struct {
|
||||
Key, Assets string
|
||||
Distribution map[string]DistributionFile
|
||||
downloads chan struct{}
|
||||
mu sync.Mutex
|
||||
active map[string]bool
|
||||
used map[string]int64
|
||||
@@ -40,7 +43,7 @@ func New(key, assets string) (*Relay, error) {
|
||||
if len(key) < 32 {
|
||||
return nil, errors.New("relay key must contain at least 32 bytes")
|
||||
}
|
||||
r := &Relay{Key: key, Assets: assets, active: map[string]bool{}, used: map[string]int64{}}
|
||||
r := &Relay{Key: key, Assets: assets, active: map[string]bool{}, used: map[string]int64{}, downloads: make(chan struct{}, 2)}
|
||||
r.dial = func(ctx context.Context, host string, port uint16) (net.Conn, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancel()
|
||||
@@ -72,6 +75,47 @@ func (r *Relay) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
json.NewEncoder(w).Encode(map[string]any{"active_sessions": active, "received_bytes": r.rx.Load(), "sent_bytes": r.tx.Load(), "rejected": r.rejected.Load(), "profile": "tcp-80-443", "max_sessions": 4, "max_streams": maxStreams, "bytes_per_second": bytesPerSecond, "max_session_bytes": maxBytes})
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(req.URL.Path, DistributionPrefix) {
|
||||
name := strings.TrimPrefix(req.URL.Path, DistributionPrefix)
|
||||
entry, ok := r.Distribution[name]
|
||||
if !ok {
|
||||
http.NotFound(w, req)
|
||||
return
|
||||
}
|
||||
select {
|
||||
case r.downloads <- struct{}{}:
|
||||
defer func() { <-r.downloads }()
|
||||
default:
|
||||
http.Error(w, "download limit", 429)
|
||||
return
|
||||
}
|
||||
w.Header().Set("ETag", `"`+entry.SHA256+`"`)
|
||||
w.Header().Set("Cache-Control", "private, max-age=86400, immutable")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; frame-ancestors 'self'")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
switch {
|
||||
case name == "index.html":
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
case strings.HasSuffix(name, ".txt"):
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
case name == "manifest.json":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="`+name+`"`)
|
||||
}
|
||||
// ServeContent avoids ServeFile's /index.html -> ./ redirect, which
|
||||
// would escape the explicit file allowlist at the proxy boundary.
|
||||
file, err := os.Open(filepath.Join(r.Assets, name))
|
||||
if err != nil {
|
||||
http.NotFound(w, req)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
http.ServeContent(w, req, name, time.Time{}, file)
|
||||
return
|
||||
}
|
||||
if req.URL.Path == AssetsPrefix+"gecko.js" || req.URL.Path == AssetsPrefix+"gecko.wasm.zst" {
|
||||
w.Header().Set("Cross-Origin-Resource-Policy", "same-origin")
|
||||
w.Header().Set("Cache-Control", "private, max-age=86400, immutable")
|
||||
|
||||
@@ -35,6 +35,17 @@ func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
panel := "auth"
|
||||
switch r.FormValue("action") {
|
||||
case "save_trusted_session_policy":
|
||||
panel = "auth-sessions"
|
||||
days, err := strconv.Atoi(r.PostForm.Get("trusted_session_days"))
|
||||
if err != nil || days < 0 || days > 30 {
|
||||
http.Error(w, "trusted session lifetime must be between 0 and 30 days", 400)
|
||||
return
|
||||
}
|
||||
if err := s.store.SetConfigInDomain(domainUUID, "auth_trusted_session_days", strconv.Itoa(days)); err != nil {
|
||||
http.Error(w, "could not save session policy", 500)
|
||||
return
|
||||
}
|
||||
case "save_session_policy":
|
||||
panel = "auth-sessions"
|
||||
minutes, err := strconv.Atoi(r.FormValue("session_minutes"))
|
||||
|
||||
@@ -51,4 +51,8 @@ func TestBrowserCookieLoginLogoutChromium(t *testing.T) {
|
||||
if err := s.store.DB.QueryRow(`SELECT count(*) FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&revoked); err != nil || revoked != 1 {
|
||||
t.Fatalf("browser revocations=%d %v", revoked, err)
|
||||
}
|
||||
var lifetime int64
|
||||
if err := s.store.DB.QueryRow(`SELECT expires_at_ms-auth_time_ms FROM auth_sessions WHERE revoked_at_ms>0`).Scan(&lifetime); err != nil || lifetime != (30*24*time.Hour).Milliseconds() {
|
||||
t.Fatalf("trusted browser lifetime=%d %v", lifetime, err)
|
||||
}
|
||||
}
|
||||
@@ -74,7 +74,7 @@ func (s *Server) handleCLIAuth(w http.ResponseWriter, r *http.Request, action st
|
||||
writeEventJSON(w, 400, map[string]string{"error": "invalid_request"})
|
||||
return
|
||||
}
|
||||
id, device, code, err := s.store.StartCLIAuth(ctx, domain, cliOrigin(r), challenge)
|
||||
id, device, code, err := s.store.StartCLIAuthForLogin(ctx, domain, cliOrigin(r), challenge, r.PostForm.Get("remember") == "1")
|
||||
if err != nil {
|
||||
cliAuthError(w, err)
|
||||
return
|
||||
@@ -140,6 +140,9 @@ func (s *Server) verifyCLIAuth(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
data := map[string]any{"CLIAuth": true, "CLIOrigin": grant.Origin, "CLIRequest": id}
|
||||
if grant.Remember {
|
||||
data["CLIRememberDays"] = s.store.TrustedSessionDays(domain)
|
||||
}
|
||||
if grant.State != "pending" {
|
||||
data["CLIDone"] = true
|
||||
s.renderAuthPage(w, r, data)
|
||||
|
||||
@@ -28,6 +28,14 @@ func cliForm(s *Server, path string, form url.Values, cookie, origin string) *ht
|
||||
}
|
||||
|
||||
func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
for _, remember := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "normal", true: "trusted"}[remember], func(t *testing.T) {
|
||||
testCLIAuthOIDCFlow(t, remember)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func testCLIAuthOIDCFlow(t *testing.T, remember bool) {
|
||||
s, api, domain := configFixture(t)
|
||||
principal := s.mustCLIPrincipal(t, domain, api)
|
||||
// An enrolled local factor must not add a second challenge to OIDC consent.
|
||||
@@ -43,7 +51,13 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
}
|
||||
verifier := strings.Repeat("x", 43)
|
||||
sum := sha256.Sum256([]byte(verifier))
|
||||
start := cliForm(s, "/_/auth/cli/start", url.Values{"code_challenge": {base64.RawURLEncoding.EncodeToString(sum[:])}, "code_challenge_method": {"S256"}}, "", "")
|
||||
startForm := url.Values{"code_challenge": {base64.RawURLEncoding.EncodeToString(sum[:])}, "code_challenge_method": {"S256"}}
|
||||
expectedSeconds := 24 * 60 * 60
|
||||
if remember {
|
||||
startForm.Set("remember", "1")
|
||||
expectedSeconds *= 30
|
||||
}
|
||||
start := cliForm(s, "/_/auth/cli/start", startForm, "", "")
|
||||
if start.Code != 200 {
|
||||
t.Fatalf("start %d %s", start.Code, start.Body.String())
|
||||
}
|
||||
@@ -63,7 +77,12 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
t.Fatalf("verify landing: %d %s", verify.Code, verify.Body.String())
|
||||
}
|
||||
begin := httptest.NewRecorder()
|
||||
s.beginOIDC(begin, httptest.NewRequest("GET", "https://localhost/_/auth/oidc?cli="+id, nil))
|
||||
// The browser query must not override the CLI's persisted choice.
|
||||
tamperedRemember := "1"
|
||||
if remember {
|
||||
tamperedRemember = "0"
|
||||
}
|
||||
s.beginOIDC(begin, httptest.NewRequest("GET", "https://localhost/_/auth/oidc?cli="+id+"&remember="+tamperedRemember, nil))
|
||||
if begin.Code != 302 {
|
||||
t.Fatalf("oidc begin %d %s", begin.Code, begin.Body.String())
|
||||
}
|
||||
@@ -86,6 +105,9 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
for _, cookie := range complete.Result().Cookies() {
|
||||
if cookie.Name == authCookieName {
|
||||
browser = cookie.Value
|
||||
if cookie.MaxAge != expectedSeconds {
|
||||
t.Fatalf("cookie lifetime %d, want %d", cookie.MaxAge, expectedSeconds)
|
||||
}
|
||||
}
|
||||
}
|
||||
if browser == "" {
|
||||
@@ -128,6 +150,7 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
var result struct {
|
||||
Token string `json:"access_token"`
|
||||
Username string `json:"username"`
|
||||
Expires int `json:"expires_in"`
|
||||
}
|
||||
if err := json.Unmarshal(exchange.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -135,6 +158,9 @@ func TestCLIAuthOIDCFlow(t *testing.T) {
|
||||
if !strings.HasPrefix(result.Token, "pw_api_") || result.Username != "admin" || result.Token == browser {
|
||||
t.Fatal("invalid delegated credential")
|
||||
}
|
||||
if result.Expires > expectedSeconds || result.Expires < expectedSeconds-10 {
|
||||
t.Fatalf("CLI lifetime %d, want approximately %d", result.Expires, expectedSeconds)
|
||||
}
|
||||
if _, err := s.store.PrincipalForSession(domain, result.Token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,10 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type oidcChallenge struct{ Verifier, Nonce, Return, Issuer, ClientID, BrowserHash, CLIRequest string }
|
||||
type oidcChallenge struct {
|
||||
Verifier, Nonce, Return, Issuer, ClientID, BrowserHash, CLIRequest string
|
||||
Remember bool
|
||||
}
|
||||
|
||||
func logOIDCFailure(stage, domainUUID, issuer string, err error) {
|
||||
host := "invalid"
|
||||
@@ -57,6 +60,7 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 8*time.Second)
|
||||
defer cancel()
|
||||
cliRequest := r.URL.Query().Get("cli")
|
||||
remember := r.URL.Query().Get("remember") == "1"
|
||||
returnPath := safeReturnPath(r.URL.Query().Get("return"))
|
||||
if cliRequest != "" {
|
||||
if !cliSecureTransport(r) || !cliProofPattern.MatchString(cliRequest) {
|
||||
@@ -68,6 +72,12 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
returnPath = cliVerifyPath(cliRequest)
|
||||
grant, err := s.store.CLIAuthRequest(ctx, domainUUID, cliOrigin(r), cliRequest)
|
||||
if err != nil {
|
||||
http.Error(w, "invalid CLI request", 400)
|
||||
return
|
||||
}
|
||||
remember = grant.Remember
|
||||
}
|
||||
metadata, err := fetchOIDCMetadata(ctx, config.Issuer)
|
||||
if err != nil {
|
||||
@@ -77,7 +87,7 @@ func (s *Server) beginOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
verifier, nonce, browser := randomURLToken(32), randomURLToken(24), randomURLToken(32)
|
||||
browserDigest := sha256.Sum256([]byte(browser))
|
||||
state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: returnPath, Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:]), CLIRequest: cliRequest}, 5*time.Minute)
|
||||
state, err := s.store.CreateAuthChallenge(domainUUID, "", "oidc", oidcChallenge{Verifier: verifier, Nonce: nonce, Return: returnPath, Issuer: config.Issuer, ClientID: config.ClientID, BrowserHash: base64.RawURLEncoding.EncodeToString(browserDigest[:]), CLIRequest: cliRequest, Remember: remember}, 5*time.Minute)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
@@ -205,7 +215,7 @@ func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "No local account matches the configured OIDC identity field", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
token, err := s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
|
||||
token, lifetime, err := s.store.CreateLoginSession(domainUUID, principal.UUID, 0, true, challenge.Remember)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
@@ -217,7 +227,7 @@ func (s *Server) completeOIDC(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds()))
|
||||
http.Redirect(w, r, challenge.Return, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -244,7 +254,7 @@ func (s *Server) requestMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
if principalErr == nil {
|
||||
select {
|
||||
case s.smtpSlots <- struct{}{}:
|
||||
token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email}, 15*time.Minute)
|
||||
token, err := s.store.CreateAuthChallenge(domainUUID, principal.UUID, "magic-link", magicLinkChallenge{Return: safeReturnPath(r.FormValue("return")), Email: principal.Email, Remember: r.PostForm.Get("remember") == "1"}, 15*time.Minute)
|
||||
if err != nil {
|
||||
<-s.smtpSlots
|
||||
break
|
||||
@@ -282,12 +292,12 @@ func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
token, err := s.store.CreateBrowserAuthSession(domainUUID, principalUUID, 0)
|
||||
token, lifetime, err := s.store.CreateLoginSession(domainUUID, principalUUID, 0, true, payload.Remember)
|
||||
if err != nil {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds()))
|
||||
http.Redirect(w, r, safeReturnPath(payload.Return), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -306,6 +316,8 @@ func (s *Server) renderAuthPage(w http.ResponseWriter, r *http.Request, data map
|
||||
data["LDAPEnabled"] = config.Enabled
|
||||
}
|
||||
data["Locale"] = s.getLocale(domainUUID)
|
||||
data["TrustedSessionDays"] = s.store.TrustedSessionDays(domainUUID)
|
||||
data["RememberRequested"] = r.PostForm.Get("remember") == "1"
|
||||
transparency := s.homeGlassTransparency(domainUUID)
|
||||
data["HomeGlassTransparency"] = transparency
|
||||
data["HomeGlassOpacity"] = 100 - transparency
|
||||
|
||||
@@ -62,7 +62,11 @@ func requestIsHTTPS(r *http.Request) bool {
|
||||
}
|
||||
|
||||
func (s *Server) setAuthCookie(w http.ResponseWriter, r *http.Request, token string) {
|
||||
http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)) * 60})
|
||||
s.setAuthCookieForSeconds(w, r, token, s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r))*60)
|
||||
}
|
||||
|
||||
func (s *Server) setAuthCookieForSeconds(w http.ResponseWriter, r *http.Request, token string, seconds int) {
|
||||
http.SetCookie(w, &http.Cookie{Name: authCookieName, Value: token, Path: "/", HttpOnly: true, Secure: requestIsHTTPS(r), SameSite: http.SameSiteLaxMode, MaxAge: seconds})
|
||||
}
|
||||
|
||||
func (s *Server) authTokenFromRequest(r *http.Request) string {
|
||||
@@ -160,6 +164,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
var principal *store.Principal
|
||||
mfaLevel := 0
|
||||
remember := r.PostForm.Get("remember") == "1"
|
||||
if action == "setup" && setup && s.validBootstrapToken(domainUUID, r.FormValue("bootstrap_token")) {
|
||||
principal, err = s.store.BootstrapOwner(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email"))
|
||||
if err == nil {
|
||||
@@ -171,7 +176,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
} else if action == "login" && !setup {
|
||||
principal, err = s.store.AuthenticatePassword(r.FormValue("username"), r.FormValue("password"))
|
||||
if err == nil && s.store.HasTOTP(principal.UUID) {
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallengeForLogin(domainUUID, principal.UUID, remember)
|
||||
if challengeErr != nil {
|
||||
err = challengeErr
|
||||
} else if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
@@ -191,7 +196,7 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
var principalUUID string
|
||||
principalUUID, err = s.store.CompleteTOTPLoginChallenge(domainUUID, challenge, r.FormValue("code"))
|
||||
principalUUID, remember, err = s.store.CompleteTOTPLoginChallengeForLogin(domainUUID, challenge, r.FormValue("code"))
|
||||
if err == nil {
|
||||
principal = &store.Principal{UUID: principalUUID}
|
||||
mfaLevel = 1
|
||||
@@ -201,18 +206,14 @@ func (s *Server) handleAuth(w http.ResponseWriter, r *http.Request) {
|
||||
err = store.ErrAccessDenied
|
||||
}
|
||||
if err == nil {
|
||||
var token string
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
token, err = s.store.CreateAuthSessionWithMFA(domainUUID, principal.UUID, mfaLevel)
|
||||
} else {
|
||||
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, mfaLevel)
|
||||
}
|
||||
token, lifetime, issueErr := s.store.CreateLoginSession(domainUUID, principal.UUID, mfaLevel, !strings.Contains(r.Header.Get("Accept"), "application/json"), remember)
|
||||
err = issueErr
|
||||
if err == nil {
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60})
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": int(lifetime.Seconds())})
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds()))
|
||||
http.Redirect(w, r, safeReturnPath(r.FormValue("return")), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -221,7 +221,7 @@ func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if s.store.HasTOTP(principal.UUID) {
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallenge(domainUUID, principal.UUID)
|
||||
challenge, challengeErr := s.store.CreateTOTPLoginChallengeForLogin(domainUUID, principal.UUID, r.PostForm.Get("remember") == "1")
|
||||
if challengeErr != nil {
|
||||
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
@@ -234,20 +234,15 @@ func (s *Server) handleLDAPLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.renderAuthPage(w, r, map[string]any{"MFA": true, "Return": returnPath})
|
||||
return
|
||||
}
|
||||
var token string
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
token, err = s.store.CreateAuthSession(domainUUID, principal.UUID)
|
||||
} else {
|
||||
token, err = s.store.CreateBrowserAuthSession(domainUUID, principal.UUID, 0)
|
||||
}
|
||||
token, lifetime, err := s.store.CreateLoginSession(domainUUID, principal.UUID, 0, !strings.Contains(r.Header.Get("Accept"), "application/json"), r.PostForm.Get("remember") == "1")
|
||||
if err != nil {
|
||||
http.Error(w, "authentication unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": s.store.AuthSessionLifetimeMinutes(domainUUID) * 60})
|
||||
writeEventJSON(w, http.StatusOK, map[string]any{"access_token": token, "token_type": "Bearer", "expires_in": int(lifetime.Seconds())})
|
||||
return
|
||||
}
|
||||
s.setAuthCookie(w, r, token)
|
||||
s.setAuthCookieForSeconds(w, r, token, int(lifetime.Seconds()))
|
||||
http.Redirect(w, r, returnPath, http.StatusSeeOther)
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSessionPolicyAdminUIAndCookie(t *testing.T) {
|
||||
@@ -48,3 +49,99 @@ func TestSessionPolicyAdminUIAndCookie(t *testing.T) {
|
||||
t.Fatalf("cookie policy %#v", cookies)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrustedSessionPolicyAndRevocation(t *testing.T) {
|
||||
s, admin, domain := configFixture(t)
|
||||
p := s.mustCLIPrincipal(t, domain, admin)
|
||||
for _, days := range []string{"30", "7", "0"} {
|
||||
form := url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {days}}
|
||||
w := configRequest(s, admin, "POST", "/-/sys/config/auth", form)
|
||||
if w.Code != 303 {
|
||||
t.Fatalf("policy %s: %d %s", days, w.Code, w.Body.String())
|
||||
}
|
||||
want := 24 * time.Hour
|
||||
if days == "30" {
|
||||
want *= 30
|
||||
}
|
||||
if days == "7" {
|
||||
want *= 7
|
||||
}
|
||||
for _, browser := range []bool{false, true} {
|
||||
token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, browser, true)
|
||||
if err != nil || lifetime != want {
|
||||
t.Fatalf("duration %v %v", lifetime, err)
|
||||
}
|
||||
if _, err = s.store.PrincipalForSession(domain, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.RevokeAuthSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
||||
t.Fatal("revoked trusted session accepted")
|
||||
}
|
||||
}
|
||||
w = httptest.NewRecorder()
|
||||
s.handleAuth(w, httptest.NewRequest("GET", "https://localhost/_/auth/login", nil))
|
||||
if strings.Contains(w.Body.String(), `id="auth-remember"`) != (days != "0") {
|
||||
t.Fatal("login choice does not reflect policy")
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"", "-1", "31", "abc"} {
|
||||
w := configRequest(s, admin, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {bad}})
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("invalid policy %q accepted: %d", bad, w.Code)
|
||||
}
|
||||
}
|
||||
user, err := s.store.CreatePasswordAccount(domain, "trusted-reader", "a long test password", "Reader", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "30"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, _, err := s.store.CreateLoginSession(domain, user.UUID, 0, false, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
w := configRequest(s, token, "POST", "/-/sys/config/auth", url.Values{"action": {"save_trusted_session_policy"}, "trusted_session_days": {"7"}})
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("reader changed trusted policy: %d", w.Code)
|
||||
}
|
||||
if err = s.store.SetPrincipalState(domain, user.UUID, "suspended"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
||||
t.Fatal("suspended trusted session accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrustedSessionExpiryIsFixed(t *testing.T) {
|
||||
s, admin, domain := configFixture(t)
|
||||
p := s.mustCLIPrincipal(t, domain, admin)
|
||||
token, lifetime, err := s.store.CreateLoginSession(domain, p, 0, false, true)
|
||||
if err != nil || lifetime != 30*24*time.Hour {
|
||||
t.Fatalf("trusted default: %v %v", lifetime, err)
|
||||
}
|
||||
var id string
|
||||
var expiry int64
|
||||
if err = s.store.DB.QueryRow(`SELECT uuid,expires_at_ms FROM auth_sessions ORDER BY rowid DESC LIMIT 1`).Scan(&id, &expiry); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.store.SetConfigInDomain(domain, "auth_trusted_session_days", "0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.PrincipalForSession(domain, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var after int64
|
||||
if err = s.store.DB.QueryRow(`SELECT expires_at_ms FROM auth_sessions WHERE uuid=?`, id).Scan(&after); err != nil || after != expiry {
|
||||
t.Fatalf("policy/read changed expiry: %d %d %v", expiry, after, err)
|
||||
}
|
||||
if _, err = s.store.DB.Exec(`UPDATE auth_sessions SET expires_at_ms=? WHERE uuid=?`, time.Now().Add(-time.Second).UnixMilli(), id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.PrincipalForSession(domain, token); err == nil {
|
||||
t.Fatal("expired trusted session accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package server
|
||||
|
||||
func init() {
|
||||
for lang, entries := range map[string]map[string]string{
|
||||
"en": {"auth.remember": "Keep me signed in on this trusted device", "auth.days": "days", "auth.remember_cli": "Keep this CLI signed in for up to", "auth.trusted_days": "Trusted device session (days; 0 disables)"},
|
||||
"ja": {"auth.remember": "信頼できる端末でログインを維持する", "auth.days": "日間", "auth.remember_cli": "CLIのログイン継続期間(最大)", "auth.trusted_days": "信頼できる端末の有効期間(日・0で無効)"},
|
||||
} {
|
||||
for key, value := range entries {
|
||||
translations[lang][key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBrowserUploadQueueChromium(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
for _, width := range []string{"1280", "390"} {
|
||||
t.Run(width, func(t *testing.T) { testBrowserUploadQueueChromium(t, width) })
|
||||
}
|
||||
}
|
||||
|
||||
func testBrowserUploadQueueChromium(t *testing.T, width string) {
|
||||
s, token, domain := configFixture(t)
|
||||
s.mux.HandleFunc("/v/browser/", s.handleBrowser)
|
||||
s.mux.HandleFunc("/v/upload", s.handleUpload)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/drop/existing.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.SaveRevisionContent(object, []byte("original"), "", "und", "text/plain", "existing.txt"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.store.CreatePageAtPathInDomain(domain, "/drop/sub/seed"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
script, err := os.ReadFile("internal/server/testdata/browser-upload.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var active, peak atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.Host = "localhost"
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
if r.Method == "POST" && r.URL.Path == "/v/upload" {
|
||||
n := active.Add(1)
|
||||
defer active.Add(-1)
|
||||
if n > peak.Load() {
|
||||
peak.Store(n)
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
if r.URL.Path == "/v/browser/drop" && r.URL.Query().Has("fixture") {
|
||||
rec := httptest.NewRecorder()
|
||||
s.withAuthorization(s.mux).ServeHTTP(rec, r)
|
||||
for key, values := range rec.Header() {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(rec.Code)
|
||||
w.Write([]byte(strings.Replace(rec.Body.String(), "</body>", "<script>"+string(script)+"</script></body>", 1)))
|
||||
return
|
||||
}
|
||||
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
||||
}))
|
||||
defer server.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
|
||||
defer cancel()
|
||||
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/browser/drop?fixture=1", t.TempDir(), "/tmp/pageswiki-browser-upload-"+width+".png", width, "850").CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("browser: %v %s", err, out)
|
||||
}
|
||||
if peak.Load() != 1 {
|
||||
t.Fatalf("parallel uploads: %d", peak.Load())
|
||||
}
|
||||
for _, path := range []string{"/drop/日本語 #1.txt", "/drop/sub/nested.txt"} {
|
||||
if _, err = s.store.ResolvePathInDomain(domain, path); err != nil {
|
||||
t.Fatal(path, err)
|
||||
}
|
||||
}
|
||||
if _, err = s.store.ResolvePathInDomain(domain, "/drop/cancel.txt"); err == nil {
|
||||
t.Fatal("cancelled file uploaded")
|
||||
}
|
||||
revs, err := s.store.ListRevisions(object)
|
||||
if err != nil || len(revs) != 1 {
|
||||
t.Fatal("existing file overwritten")
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,7 @@ func (s *Server) transpileMarkdownFromPath(r *http.Request, path string, mode ma
|
||||
|
||||
html := latest.Body
|
||||
if mode == markdownProjection {
|
||||
html = s.expandMacrosForRequest(r, html, path, s.domainUUIDForRequest(r))
|
||||
html = s.safeHTMLProjection(r, html, path, true)
|
||||
} else {
|
||||
return grammaratur.HTMLToMarkdownAuthoring(html)
|
||||
}
|
||||
|
||||
@@ -1078,49 +1078,50 @@ func (s *Server) handleConfig(
|
||||
s.getSystemNavItems(s.domainUUIDForRequest(r)),
|
||||
NavContext{},
|
||||
),
|
||||
"HomePath": homePath,
|
||||
"HomeWallpaperPath": homeWallpaperPath,
|
||||
"HomeWallpaperOpacity": homeWallpaperOpacity,
|
||||
"HomeGlassTransparency": s.homeGlassTransparency(domainUUID),
|
||||
"HomePalette": s.homePalette(domainUUID),
|
||||
"HomeLayout": s.homeLayout(s.domainUUIDForRequest(r)),
|
||||
"HomeBookmarks": s.homeBookmarksText(s.domainUUIDForRequest(r)),
|
||||
"VirtualFolderPages": s.getVirtualFolderPages(s.domainUUIDForRequest(r)),
|
||||
"WikiName": wikiName,
|
||||
"Locale": s.getLocale(s.domainUUIDForRequest(r)),
|
||||
"Timezone": s.getTimezoneName(s.domainUUIDForRequest(r)),
|
||||
"Theme": s.getTheme(s.domainUUIDForRequest(r)),
|
||||
"ThemeOptions": themeOptions(),
|
||||
"EditorKeymap": s.getEditorKeymap(s.domainUUIDForRequest(r)),
|
||||
"EditorFormat": s.getEditorFormat(s.domainUUIDForRequest(r)),
|
||||
"EditorPreviewDefault": s.getEditorPreviewDefault(s.domainUUIDForRequest(r)),
|
||||
"EditorSyntax": s.getEditorSyntaxHighlight(s.domainUUIDForRequest(r)),
|
||||
"EditorAutocomplete": s.getEditorAutocomplete(s.domainUUIDForRequest(r)),
|
||||
"ZombieGraceHours": zombieGraceHours,
|
||||
"Objects": recent,
|
||||
"ZombieObjects": zombies,
|
||||
"Domains": domains,
|
||||
"DomainEntries": domainEntries,
|
||||
"PeriodicalProjections": projections,
|
||||
"PeriodicalKinds": []string{"diary", "blog"},
|
||||
"CampusLegacyPeers": legacyPeers,
|
||||
"Campuses": campuses,
|
||||
"CampusManaged": campusManaged,
|
||||
"CampusOnline": campusOnline,
|
||||
"FederationInstances": federationInstances,
|
||||
"CampusTokens": campusTokens,
|
||||
"AuthPrincipals": authPrincipals,
|
||||
"AuthSessions": authSessions,
|
||||
"AuthSessionMinutes": s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)),
|
||||
"OIDCConfig": oidcConfig,
|
||||
"SMTPConfig": smtpConfig,
|
||||
"LDAPConfig": ldapConfig,
|
||||
"LDAPTest": r.URL.Query().Get("ldap_test"),
|
||||
"ObjectsCampusUUID": selectedCampusUUID,
|
||||
"ObjectsDomainUUID": selectedDomain.UUID,
|
||||
"ObjectsDomainHost": objectsDomainHost,
|
||||
"HomeNavItemsConfig": s.getHomeNavItems(s.domainUUIDForRequest(r)),
|
||||
"SystemNavItemsConfig": s.getSystemNavItems(s.domainUUIDForRequest(r)),
|
||||
"HomePath": homePath,
|
||||
"HomeWallpaperPath": homeWallpaperPath,
|
||||
"HomeWallpaperOpacity": homeWallpaperOpacity,
|
||||
"HomeGlassTransparency": s.homeGlassTransparency(domainUUID),
|
||||
"HomePalette": s.homePalette(domainUUID),
|
||||
"HomeLayout": s.homeLayout(s.domainUUIDForRequest(r)),
|
||||
"HomeBookmarks": s.homeBookmarksText(s.domainUUIDForRequest(r)),
|
||||
"VirtualFolderPages": s.getVirtualFolderPages(s.domainUUIDForRequest(r)),
|
||||
"WikiName": wikiName,
|
||||
"Locale": s.getLocale(s.domainUUIDForRequest(r)),
|
||||
"Timezone": s.getTimezoneName(s.domainUUIDForRequest(r)),
|
||||
"Theme": s.getTheme(s.domainUUIDForRequest(r)),
|
||||
"ThemeOptions": themeOptions(),
|
||||
"EditorKeymap": s.getEditorKeymap(s.domainUUIDForRequest(r)),
|
||||
"EditorFormat": s.getEditorFormat(s.domainUUIDForRequest(r)),
|
||||
"EditorPreviewDefault": s.getEditorPreviewDefault(s.domainUUIDForRequest(r)),
|
||||
"EditorSyntax": s.getEditorSyntaxHighlight(s.domainUUIDForRequest(r)),
|
||||
"EditorAutocomplete": s.getEditorAutocomplete(s.domainUUIDForRequest(r)),
|
||||
"ZombieGraceHours": zombieGraceHours,
|
||||
"Objects": recent,
|
||||
"ZombieObjects": zombies,
|
||||
"Domains": domains,
|
||||
"DomainEntries": domainEntries,
|
||||
"PeriodicalProjections": projections,
|
||||
"PeriodicalKinds": []string{"diary", "blog"},
|
||||
"CampusLegacyPeers": legacyPeers,
|
||||
"Campuses": campuses,
|
||||
"CampusManaged": campusManaged,
|
||||
"CampusOnline": campusOnline,
|
||||
"FederationInstances": federationInstances,
|
||||
"CampusTokens": campusTokens,
|
||||
"AuthPrincipals": authPrincipals,
|
||||
"AuthSessions": authSessions,
|
||||
"AuthSessionMinutes": s.store.AuthSessionLifetimeMinutes(s.domainUUIDForRequest(r)),
|
||||
"AuthTrustedSessionDays": s.store.TrustedSessionDays(s.domainUUIDForRequest(r)),
|
||||
"OIDCConfig": oidcConfig,
|
||||
"SMTPConfig": smtpConfig,
|
||||
"LDAPConfig": ldapConfig,
|
||||
"LDAPTest": r.URL.Query().Get("ldap_test"),
|
||||
"ObjectsCampusUUID": selectedCampusUUID,
|
||||
"ObjectsDomainUUID": selectedDomain.UUID,
|
||||
"ObjectsDomainHost": objectsDomainHost,
|
||||
"HomeNavItemsConfig": s.getHomeNavItems(s.domainUUIDForRequest(r)),
|
||||
"SystemNavItemsConfig": s.getSystemNavItems(s.domainUUIDForRequest(r)),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"errors"
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Projection only: no network fetch, shared rendered cache or write-back.
|
||||
const maxCSVMacroBytes = 256 << 10
|
||||
|
||||
var csvMacroRE = regexp.MustCompile(`^\{\{csv:(?:"([^"]+)"|([^\s}]+))(.*?)\}\}$`)
|
||||
var csvRangeRE = regexp.MustCompile(`^([A-Z]{1,2})([1-9][0-9]{0,3}):([A-Z]{1,2})([1-9][0-9]{0,3})$`)
|
||||
|
||||
type csvMacroOptions struct {
|
||||
Path, Revision string
|
||||
Row, EndRow, Column, EndColumn int
|
||||
Header bool
|
||||
}
|
||||
|
||||
func parseCSVMacro(raw string) (csvMacroOptions, error) {
|
||||
o := csvMacroOptions{Row: 1, EndRow: 200, Column: 1, EndColumn: 32, Header: true}
|
||||
m := csvMacroRE.FindStringSubmatch(raw)
|
||||
bad := errors.New("invalid CSV macro")
|
||||
if m == nil {
|
||||
return o, bad
|
||||
}
|
||||
o.Path = m[1]
|
||||
if o.Path == "" {
|
||||
o.Path = m[2]
|
||||
}
|
||||
if !strings.HasPrefix(o.Path, "/") || strings.HasPrefix(o.Path, "//") || path.Clean(o.Path) != o.Path || strings.ContainsAny(o.Path, "\\\x00\r\n") || len(o.Path) > 2048 {
|
||||
return o, bad
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, field := range strings.Fields(m[3]) {
|
||||
key, value, ok := strings.Cut(field, "=")
|
||||
if !ok || seen[key] {
|
||||
return o, bad
|
||||
}
|
||||
seen[key] = true
|
||||
switch key {
|
||||
case "header":
|
||||
if value != "true" && value != "false" {
|
||||
return o, bad
|
||||
}
|
||||
o.Header = value == "true"
|
||||
case "rev":
|
||||
if _, err := uuid.Parse(value); len(value) != 36 || err != nil {
|
||||
return o, bad
|
||||
}
|
||||
o.Revision = value
|
||||
case "range":
|
||||
parts := csvRangeRE.FindStringSubmatch(strings.ToUpper(value))
|
||||
if parts == nil {
|
||||
return o, bad
|
||||
}
|
||||
col := func(s string) int {
|
||||
n := 0
|
||||
for _, c := range s {
|
||||
n = n*26 + int(c-'A') + 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
o.Column, o.EndColumn = col(parts[1]), col(parts[3])
|
||||
o.Row, _ = strconv.Atoi(parts[2])
|
||||
o.EndRow, _ = strconv.Atoi(parts[4])
|
||||
if o.Column > o.EndColumn || o.EndColumn > 100 || o.EndColumn-o.Column >= 32 || o.Row > o.EndRow || o.EndRow > 2000 || o.EndRow-o.Row >= 200 {
|
||||
return o, bad
|
||||
}
|
||||
default:
|
||||
return o, bad
|
||||
}
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
func csvMacroTable(body []byte, o csvMacroOptions) (string, error) {
|
||||
if len(body) > maxCSVMacroBytes || !utf8.Valid(body) || strings.IndexByte(string(body), 0) >= 0 {
|
||||
return "", errors.New("invalid CSV input")
|
||||
}
|
||||
reader := csv.NewReader(strings.NewReader(strings.TrimPrefix(string(body), "\ufeff")))
|
||||
reader.FieldsPerRecord = -1
|
||||
var out strings.Builder
|
||||
out.WriteString(`<div class="csv-projection table-scroll"><table><caption><a href="` + html.EscapeString((&url.URL{Path: o.Path}).EscapedPath()) + `" title="Revision ` + html.EscapeString(o.Revision) + `">` + html.EscapeString(path.Base(o.Path)) + `</a></caption><tbody>`)
|
||||
for row := 1; row <= o.EndRow; row++ {
|
||||
record, err := reader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(record) > 100 {
|
||||
return "", errors.New("too many CSV columns")
|
||||
}
|
||||
if row < o.Row {
|
||||
continue
|
||||
}
|
||||
out.WriteString("<tr>")
|
||||
cell := "td"
|
||||
if o.Header && row == o.Row {
|
||||
cell = "th"
|
||||
}
|
||||
for col := o.Column; col <= o.EndColumn && col <= len(record); col++ {
|
||||
if len(record[col-1]) > 16<<10 {
|
||||
return "", errors.New("CSV cell too large")
|
||||
}
|
||||
// Never expand macros, formulas or markup from CSV cells.
|
||||
out.WriteString("<" + cell + ">" + html.EscapeString(record[col-1]) + "</" + cell + ">")
|
||||
}
|
||||
out.WriteString("</tr>")
|
||||
}
|
||||
out.WriteString("</tbody></table></div>")
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
func (s *Server) expandCSVMacro(r *http.Request, macro string) string {
|
||||
fallback := `<span class="projection-warning">` + html.EscapeString(translate(s.getLocale(s.domainUUIDForRequest(r)), "viewer.csv_unavailable")) + `</span>`
|
||||
o, err := parseCSVMacro(macro)
|
||||
if err != nil || r.Context().Err() != nil || s.store == nil || isAnonymousProjection(r) || !s.pathReadFilter(r)(o.Path) {
|
||||
return fallback
|
||||
}
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
object, err := s.store.ResolvePathInDomainContext(r.Context(), domain, o.Path)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
rev, err := s.store.GetLatestRevisionMetadataForLanguagesContext(r.Context(), object, s.revisionLanguageCandidates(r))
|
||||
if o.Revision != "" {
|
||||
rev, err = s.store.GetRevisionMetadataContext(r.Context(), o.Revision)
|
||||
}
|
||||
if err != nil || rev == nil || rev.UUID == "" || rev.ObjectUUID != object {
|
||||
return fallback
|
||||
}
|
||||
media := strings.ToLower(strings.TrimSpace(strings.Split(rev.ContentType, ";")[0]))
|
||||
if media != "text/csv" && media != "application/csv" {
|
||||
return fallback
|
||||
}
|
||||
size, err := s.store.DocumentSizeContext(r.Context(), rev.DocumentHash)
|
||||
if err != nil || size > maxCSVMacroBytes {
|
||||
return fallback
|
||||
}
|
||||
window, err := s.store.ReadDocumentWindowContext(r.Context(), rev.DocumentHash, 0, maxCSVMacroBytes)
|
||||
if err != nil || window.Truncated {
|
||||
return fallback
|
||||
}
|
||||
o.Revision = rev.UUID
|
||||
table, err := csvMacroTable(window.Data, o)
|
||||
if err != nil || r.Context().Err() != nil {
|
||||
return fallback
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
||||
func init() {
|
||||
translations["en"]["viewer.csv_unavailable"] = "CSV unavailable (access, format or size limit)."
|
||||
translations["ja"]["viewer.csv_unavailable"] = "CSVを表示できません(権限・形式・サイズ制限)。"
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/grammaratur"
|
||||
)
|
||||
|
||||
func TestCSVMacroParsingAndBounds(t *testing.T) {
|
||||
for _, raw := range []string{`{{csv:/data/a.csv}}`, `{{csv:"/data/日本語 table.csv" range=B2:D4 header=false}}`} {
|
||||
if _, err := parseCSVMacro(raw); err != nil {
|
||||
t.Fatal(raw, err)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{`{{csv:https://example.org/a.csv}}`, `{{csv://evil/a.csv}}`, `{{csv:/a/../secret}}`, `{{csv:/a range=A1:C201}}`, `{{csv:/a range=A1:AG2}}`, `{{csv:/a range=A2001:B2002}}`, `{{csv:/a range=D2:A1}}`, `{{csv:/a header=yes}}`, `{{csv:/a header=true header=false}}`, `{{csv:/a unsupported=1}}`, `{{csv:/a rev=bad}}`} {
|
||||
if _, err := parseCSVMacro(raw); err == nil {
|
||||
t.Fatal("accepted", raw)
|
||||
}
|
||||
}
|
||||
o, _ := parseCSVMacro(`{{csv:/data/a.csv range=A1:C3}}`)
|
||||
body := "\ufeffname,description,formula\r\n日本語,\"a,b\r\nnext\",=1+1\r\n<script>,{{children:/private}},end\r\n"
|
||||
got, err := csvMacroTable([]byte(body), o)
|
||||
if err != nil || !strings.Contains(got, "<th>name</th>") || !strings.Contains(got, "a,b\nnext") || !strings.Contains(got, "<script>") || !strings.Contains(got, "{{children:/private}}") {
|
||||
t.Fatalf("CSV: %s %v", got, err)
|
||||
}
|
||||
checkInertProjection(t, got)
|
||||
for _, bad := range [][]byte{[]byte("a,\"unterminated"), {0xff}, []byte("a,\x00"), []byte(strings.Repeat("x", maxCSVMacroBytes+1)), []byte(strings.Repeat("x", (16<<10)+1)), []byte(strings.Repeat("a,", 101))} {
|
||||
if _, err := csvMacroTable(bad, o); err == nil {
|
||||
t.Fatal("invalid/large CSV accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCSVMacroProjectionAuthorizationAndRevision(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
object, err := s.store.CreatePageAtPathInDomain(domain, "/data/report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := s.store.SaveRevisionContent(object, []byte("name,value\r\noriginal,1\r\n"), "", "und", "text/csv", "report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
macro := `{{csv:/data/report.csv range=A1:B2}}`
|
||||
got := s.safeHTMLProjection(r, "<p>"+macro+"</p><code>"+macro+"</code>", "/guide", true)
|
||||
if strings.Count(got, `class="csv-projection table-scroll"`) != 1 || !strings.Contains(got, "original") || !strings.Contains(got, "<code>"+macro+"</code>") || !strings.Contains(got, first.UUID) {
|
||||
t.Fatal(got)
|
||||
}
|
||||
second, err := s.store.SaveRevisionContent(object, []byte("name,value\nupdated,2\n"), "", "und", "text/csv", "report.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, macro, "/guide", true)
|
||||
if !strings.Contains(got, "updated") || !strings.Contains(got, second.UUID) || strings.Contains(got, "original") {
|
||||
t.Fatal("stale projection", got)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, `{{csv:/data/report.csv rev=`+first.UUID+`}}`, "/guide", true)
|
||||
if !strings.Contains(got, "original") || strings.Contains(got, "updated") {
|
||||
t.Fatal("pinned revision", got)
|
||||
}
|
||||
other, err := s.store.CreatePageAtPathInDomain(domain, "/data/other.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreign, err := s.store.SaveRevisionContent(other, []byte("SECRET_DIFFERENT_OBJECT"), "", "und", "text/csv", "other.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = s.safeHTMLProjection(r, `{{csv:/data/report.csv rev=`+foreign.UUID+`}}`, "/guide", true)
|
||||
if strings.Contains(got, "SECRET") || strings.Contains(got, "<table") {
|
||||
t.Fatal("foreign revision leaked", got)
|
||||
}
|
||||
// A fresh request with no credentials must not reuse another reader's table.
|
||||
denied := httptest.NewRequest("GET", "http://localhost/guide", nil)
|
||||
got = s.safeHTMLProjection(denied, macro, "/guide", true)
|
||||
if strings.Contains(got, "updated") || strings.Contains(got, "<table") {
|
||||
t.Fatal("unauthorized table", got)
|
||||
}
|
||||
public := r.WithContext(context.WithValue(r.Context(), anonymousProjectionKey{}, true))
|
||||
if got = s.safeHTMLProjection(public, macro, "/guide", true); strings.Contains(got, "<table") {
|
||||
t.Fatal("anonymous CSV expanded", got)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
cancel()
|
||||
if got = s.expandCSVMacro(r.WithContext(ctx), macro); strings.Contains(got, "<table") {
|
||||
t.Fatal("cancelled projection ran")
|
||||
}
|
||||
// Authoring must preserve the macro, never bake projected rows into HTML.
|
||||
source := "<p>" + macro + "</p>"
|
||||
md, err := grammaratur.HTMLToMarkdownAuthoring(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(grammaratur.MarkdownAuthoringHTML(md), macro) || strings.Contains(md, "updated") {
|
||||
t.Fatal("authoring expanded CSV", md)
|
||||
}
|
||||
// Four distinct CSV reads per projection, then leave authored syntax literal.
|
||||
input := strings.Repeat(macro, 2)
|
||||
for _, col := range []string{"A1:A2", "B1:B2", "A1:B1", "A2:B2"} {
|
||||
input += `{{csv:/data/report.csv range=` + col + `}}`
|
||||
}
|
||||
got = s.safeHTMLProjection(r, input, "/guide", true)
|
||||
if strings.Count(got, "<table>") != 5 || !strings.Contains(got, "{{csv:") {
|
||||
t.Fatal("CSV budget not enforced", got)
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,8 @@ func feedRoute(p string) bool { return p == "/v/feeds" || strings.HasPrefix(p, f
|
||||
func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
domain := s.domainUUIDForRequest(r)
|
||||
if !s.systemExtensionEnabled(domain, "organizer.feeds") {
|
||||
licenseDownload := strings.HasPrefix(r.URL.Path, geckoAPI+"distribution/v0.0.1/")
|
||||
if !licenseDownload && !s.systemExtensionEnabled(domain, "organizer.feeds") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
@@ -61,7 +62,7 @@ func (s *Server) handleFeeds(w http.ResponseWriter, r *http.Request) {
|
||||
linkMode = "tab"
|
||||
}
|
||||
}
|
||||
s.renderWithContext(w, r, "feeds.gohtml", map[string]any{"Title": "RSS", "FeedWritable": active, "FeedWikiPrefix": prefix, "FeedLinkMode": linkMode, "FeedShareX": s.systemExtensionEnabled(domain, "sharing.x")})
|
||||
s.renderWithContext(w, r, "feeds.gohtml", map[string]any{"Title": "RSS", "FeedTimezone": s.getTimezoneName(domain), "FeedWritable": active, "FeedWikiPrefix": prefix, "FeedLinkMode": linkMode, "FeedShareX": s.systemExtensionEnabled(domain, "sharing.x")})
|
||||
return
|
||||
}
|
||||
key := strings.TrimPrefix(r.URL.Path, feedsAPI)
|
||||
|
||||
@@ -48,7 +48,10 @@ func geckoIsolation(w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
func (s *Server) handleFeedBrowser(w http.ResponseWriter, r *http.Request, domain, actor string, active bool) {
|
||||
if !active || !s.geckoAvailable(domain) {
|
||||
path := strings.TrimPrefix(r.URL.Path, geckoAPI)
|
||||
// Recipients can still obtain sources after the viewer is disabled.
|
||||
distribution := strings.HasPrefix(path, strings.TrimPrefix(geckorelay.DistributionPrefix, "/"))
|
||||
if s.geckoConfig() == nil || (!distribution && (!active || !s.geckoAvailable(domain))) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
@@ -56,7 +59,6 @@ func (s *Server) handleFeedBrowser(w http.ResponseWriter, r *http.Request, domai
|
||||
http.Error(w, "invalid request", 400)
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, geckoAPI)
|
||||
switch path {
|
||||
case "":
|
||||
geckoIsolation(w)
|
||||
@@ -73,8 +75,10 @@ func (s *Server) handleFeedBrowser(w http.ResponseWriter, r *http.Request, domai
|
||||
return
|
||||
}
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
if !distribution {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
cfg := s.geckoConfig()
|
||||
ctx, cancel := context.WithCancel(r.Context())
|
||||
@@ -129,7 +133,7 @@ func (s *Server) handleFeedBrowser(w http.ResponseWriter, r *http.Request, domai
|
||||
proxy.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
|
||||
const geckoShell = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="/static/css/feed-gecko.css?v=1"><title>Gecko</title></head><body><p id="status" role="status"></p><canvas id="screen" tabindex="0" aria-label="Original article"></canvas><script type="module" src="/static/js/feed-gecko.js?v=1"></script></body></html>`
|
||||
const geckoShell = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="/static/css/feed-gecko.css?v=1"><title>Gecko</title></head><body><p id="status" role="status"></p><canvas id="screen" tabindex="0" aria-label="Original article"></canvas><script type="module" src="/static/js/feed-gecko.js?v=2"></script></body></html>`
|
||||
|
||||
func init() {
|
||||
translations["en"]["feeds.gecko"] = "Gecko RSS browser (experimental)"
|
||||
|
||||
@@ -23,17 +23,44 @@ import (
|
||||
// The real engine runs against a synthetic Wisp HTTP responder. This test never
|
||||
// dials an article server, production source, DNS name or external calendar.
|
||||
func TestFeedGeckoChromium(t *testing.T) {
|
||||
testFeedGeckoChromium(t, false)
|
||||
}
|
||||
|
||||
func TestFeedGeckoStartupFailureChromium(t *testing.T) {
|
||||
testFeedGeckoChromium(t, true)
|
||||
}
|
||||
|
||||
func testFeedGeckoChromium(t *testing.T, failure bool) {
|
||||
assets := os.Getenv("PAGESWIKI_GECKO_TEST_ASSETS")
|
||||
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" || assets == "" {
|
||||
t.Skip("opt-in real Gecko assets")
|
||||
}
|
||||
var connections atomic.Int64
|
||||
publicHTTPS := !failure && os.Getenv("PAGESWIKI_GECKO_PUBLIC_HTTPS_TEST") == "1"
|
||||
realRelay, err := geckorelay.New(geckoTestKey, assets)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if publicHTTPS {
|
||||
realRelay.Distribution, err = geckorelay.VerifyDistribution(assets)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if failure && strings.HasSuffix(r.URL.Path, "/NotoSansCJKjp-Regular.otf") {
|
||||
http.Error(w, "synthetic startup failure", 503)
|
||||
return
|
||||
}
|
||||
if publicHTTPS {
|
||||
realRelay.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if _, err := geckorelay.Verify(geckoTestKey, r.Header.Get(geckorelay.GrantHeader), time.Now()); err != nil {
|
||||
http.Error(w, "bad grant", 403)
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, "/assets/") {
|
||||
if strings.HasPrefix(r.URL.Path, "/assets/") || strings.HasPrefix(r.URL.Path, geckorelay.DistributionPrefix) {
|
||||
http.ServeFile(w, r, filepath.Join(assets, filepath.Base(r.URL.Path)))
|
||||
return
|
||||
}
|
||||
@@ -67,7 +94,7 @@ func TestFeedGeckoChromium(t *testing.T) {
|
||||
if !strings.Contains(requests[id], "\r\n\r\n") {
|
||||
continue
|
||||
}
|
||||
body := `<!doctype html><body style="margin:0;background:rgb(255,0,0)"><a style="display:block;width:300px;height:100px;background:blue;color:white" href="http://fixture.invalid/next">Synthetic next page</a><h1>Private fixture</h1>`
|
||||
body := `<!doctype html><meta charset="utf-8"><body style="margin:0;background:rgb(255,0,0)"><a style="display:block;width:300px;height:100px;background:blue;color:white" href="http://fixture.invalid/next">Synthetic next page</a><h1>日本語の記事:学舎の園</h1>`
|
||||
if strings.HasPrefix(requests[id], "GET /next ") {
|
||||
body = `<!doctype html><body style="margin:0;background:rgb(0,255,0)"><h1>Second synthetic page</h1>`
|
||||
}
|
||||
@@ -96,11 +123,26 @@ func TestFeedGeckoChromium(t *testing.T) {
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "synthetic", Title: "Gecko synthetic fixture", URL: "http://fixture.invalid/first", Body: "No external network access."}}, "", "", "", false); e != nil {
|
||||
articleURL := "http://fixture.invalid/first"
|
||||
if publicHTTPS {
|
||||
articleURL = "https://example.com/"
|
||||
}
|
||||
if e = s.store.CompleteFeed(context.Background(), claim, []store.FeedItem{{ID: "synthetic", Title: "Gecko synthetic fixture", URL: articleURL, Body: "Synthetic test fixture."}}, "", "", "", false); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
handler := s.handler()
|
||||
web := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if publicHTTPS && r.URL.Path == "/static/js/feed-gecko.js" {
|
||||
js, err := os.ReadFile("static/js/feed-gecko.js")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
http.Error(w, "fixture missing", 500)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/javascript")
|
||||
io.WriteString(w, strings.Replace(string(js), "engine=new Gecko(", "globalThis.fixtureEngine=engine=new Gecko(", 1))
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Origin") == "http://"+r.Host {
|
||||
r.Header.Set("Origin", "http://localhost")
|
||||
}
|
||||
@@ -111,7 +153,14 @@ func TestFeedGeckoChromium(t *testing.T) {
|
||||
defer web.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 150*time.Second)
|
||||
defer cancel()
|
||||
out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/feed-gecko-browser.mjs", web.URL+"/v/feeds", t.TempDir(), "/tmp/pageswiki-feed-gecko.png").CombinedOutput()
|
||||
mode := "synthetic"
|
||||
if failure {
|
||||
mode = "startup-failure"
|
||||
}
|
||||
if publicHTTPS {
|
||||
mode = "public-https"
|
||||
}
|
||||
out, e := exec.CommandContext(ctx, "node", "internal/server/testdata/feed-gecko-browser.mjs", web.URL+"/v/feeds", t.TempDir(), "/tmp/pageswiki-feed-gecko.png", mode).CombinedOutput()
|
||||
if e != nil {
|
||||
t.Fatalf("browser: %v\n%s", e, out)
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) {
|
||||
t.Error("credential/header leaked", h)
|
||||
}
|
||||
}
|
||||
if r.URL.Path != "/assets/v0.0.1/gecko.js" {
|
||||
if r.URL.Path != "/assets/v0.0.1/gecko.js" && r.URL.Path != "/distribution/v0.0.1/manifest.json" {
|
||||
t.Error("path", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("Set-Cookie", "forbidden=1")
|
||||
@@ -133,6 +133,18 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) {
|
||||
if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 {
|
||||
t.Fatal("disabled shell", w.Code)
|
||||
}
|
||||
if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w = feedRequestTest(s, token, "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code != 200 {
|
||||
t.Fatal("sources lost after disabling extensions", w.Code)
|
||||
}
|
||||
if w = feedRequestTest(s, "", "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code == 200 {
|
||||
t.Fatal("anonymous source download")
|
||||
}
|
||||
if err = s.setSystemExtensionEnabled(domain, "organizer.feeds", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.setSystemExtensionEnabled(domain, "viewer.gecko", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -142,6 +154,9 @@ func TestFeedGeckoAuthorizationAndHeaders(t *testing.T) {
|
||||
if w = feedRequestTest(s, token, "GET", "browser/", nil); w.Code != 404 {
|
||||
t.Fatal("readonly browser", w.Code)
|
||||
}
|
||||
if w = feedRequestTest(s, token, "GET", "browser/distribution/v0.0.1/manifest.json", nil); w.Code != 200 {
|
||||
t.Fatal("readonly recipient lost source access", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeedGeckoWebsocketRevocation(t *testing.T) {
|
||||
|
||||
@@ -531,6 +531,9 @@ func TestFeedsChromium(t *testing.T) {
|
||||
t.Skip("opt-in Chromium")
|
||||
}
|
||||
s, token, domain, actor, source := feedFixture(t)
|
||||
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prefs, err := s.store.TaskPreferences(context.Background(), domain, actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -20,11 +20,11 @@ import (
|
||||
"golang.org/x/net/html/atom"
|
||||
)
|
||||
|
||||
const htmlProjectionVersion = "controlled-v2"
|
||||
const htmlProjectionVersion = "controlled-v3"
|
||||
const maxProjectionInput = 1 << 20
|
||||
const maxProjectionOutput = 4 << 20
|
||||
|
||||
var projectionMacroRE = regexp.MustCompile(`\{\{(?:children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`)
|
||||
var projectionMacroRE = regexp.MustCompile(`\{\{(?:csv:[^}]*|children[^}]*|(?:city-breadcrumbs|spot-pages|quartier|street|spot|nearby|city)(?:\s+[^}]*)?)\}\}`)
|
||||
var languageClassRE = regexp.MustCompile(`^language-[a-zA-Z0-9_+-]{1,40}$`)
|
||||
|
||||
func projectionPolicy(generated bool) *bluemonday.Policy {
|
||||
@@ -199,6 +199,8 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b
|
||||
}
|
||||
var output strings.Builder
|
||||
macros, count := 0, 0
|
||||
csvCount := 0
|
||||
csvResults := map[string]string{}
|
||||
var emit func(*html.Node, int, bool) bool
|
||||
emit = func(n *html.Node, depth int, literal bool) bool {
|
||||
count++
|
||||
@@ -226,6 +228,14 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b
|
||||
// be reparsed as a nested children request on its raw text.
|
||||
if isAnonymousProjection(r) {
|
||||
expanded = s.expandPublicMacro(r, macro, path)
|
||||
} else if strings.HasPrefix(macro, "{{csv:") {
|
||||
if cached, ok := csvResults[macro]; ok {
|
||||
expanded = cached
|
||||
} else if csvCount < 4 {
|
||||
csvCount++
|
||||
expanded = s.expandCSVMacro(r, macro)
|
||||
csvResults[macro] = expanded
|
||||
}
|
||||
} else if strings.HasPrefix(macro, "{{children") {
|
||||
expanded = s.expandChildrenMacros(macro, path)
|
||||
} else {
|
||||
@@ -236,6 +246,9 @@ func (s *Server) safeHTMLProjection(r *http.Request, body, path string, expand b
|
||||
// promote decoded entities into trusted generated elements.
|
||||
output.WriteString(stdhtml.EscapeString(macro))
|
||||
} else {
|
||||
if output.Len()+len(expanded) > maxProjectionOutput {
|
||||
return false
|
||||
}
|
||||
output.WriteString(expanded)
|
||||
}
|
||||
at = loc[1]
|
||||
|
||||
@@ -36,8 +36,9 @@ func (c *smtpLimitedConn) Read(buffer []byte) (int, error) {
|
||||
}
|
||||
|
||||
type magicLinkChallenge struct {
|
||||
Return string `json:"return"`
|
||||
Email string `json:"email"`
|
||||
Return string `json:"return"`
|
||||
Email string `json:"email"`
|
||||
Remember bool `json:"remember,omitempty"`
|
||||
}
|
||||
|
||||
func smtpMailbox(value string) (*mail.Address, error) {
|
||||
|
||||
+4
@@ -4,6 +4,10 @@
|
||||
if (phase === 'login') {
|
||||
const form = document.querySelector('form[action="/_/auth/login"]')
|
||||
assert(form, 'Login form missing')
|
||||
const remember = form.elements.remember
|
||||
assert(remember && !remember.checked, 'Trusted login must be opt-in')
|
||||
remember.checked = true
|
||||
remember.dispatchEvent(new Event('change', { bubbles: true }))
|
||||
form.elements.username.value = 'admin'
|
||||
form.elements.password.value = 'a long test password'
|
||||
sessionStorage.setItem('auth-flow-phase', 'authenticated')
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
(async()=>{
|
||||
const wait=async fn=>{for(let i=0;i<200;i++){if(fn())return;await new Promise(r=>setTimeout(r,30));}throw Error('Timeout '+fn);};
|
||||
const assert=(ok,msg)=>{if(!ok)throw Error(msg);};
|
||||
await wait(()=>document.querySelector('.browser-upload-queue'));
|
||||
const drop=(target,files)=>{const transfer=new DataTransfer();for(const [name,body]of files)transfer.items.add(new File([body],name,{type:'text/plain'}));const event=new DragEvent('drop',{bubbles:true,cancelable:true,dataTransfer:transfer});target.dispatchEvent(event);assert(event.defaultPrevented,'File drop did not prevent navigation');};
|
||||
const main=document.querySelector('.browser-main');
|
||||
drop(main,[['日本語 #1.txt','new file'],['existing.txt','must not replace'],['cancel.txt','not sent']]);
|
||||
const queued=[...document.querySelectorAll('.browser-upload-queue li')];queued[2].querySelector('button').click();
|
||||
drop(document.querySelector('.browser-row[data-path="/drop/sub"]'),[['nested.txt','nested']]);
|
||||
await wait(()=>document.querySelectorAll('.browser-upload-queue [data-state=done]').length===2&&document.querySelectorAll('.browser-upload-queue [data-state=failed]').length===1);
|
||||
await wait(()=>document.querySelector('.browser-row[data-path="/drop/日本語 #1.txt"]'));
|
||||
assert(document.querySelectorAll('.browser-upload-queue [data-state=cancelled]').length===1,'Cancellation missing');
|
||||
const card=document.querySelector('.browser-upload-queue'),rect=card.getBoundingClientRect();assert(getComputedStyle(card).position==='fixed'&&rect.right<=innerWidth&&rect.bottom<=innerHeight,'Queue not floating within viewport');
|
||||
const toggle=card.querySelector('[aria-controls]');toggle.click();assert(getComputedStyle(card.querySelector('ol')).display==='none','Queue collapse failed');toggle.click();
|
||||
assert(document.querySelectorAll('.browser-upload-queue progress[value="100"]').length===2,'Completion progress missing');
|
||||
document.body.dataset.vimReady='true';
|
||||
})().catch(e=>{const p=document.createElement('pre');p.id='error';p.textContent=e.stack;document.body.append(p);});
|
||||
+30
-4
@@ -1,7 +1,7 @@
|
||||
import {spawn} from 'node:child_process';
|
||||
import {writeFile} from 'node:fs/promises';
|
||||
import {setTimeout as delay} from 'node:timers/promises';
|
||||
const [url,profile,screenshot]=process.argv.slice(2);
|
||||
const [url,profile,screenshot,mode]=process.argv.slice(2);
|
||||
const browser=spawn('chromium',['--headless','--no-sandbox','--disable-gpu','--disable-background-networking','--no-proxy-server','--remote-debugging-port=0',`--user-data-dir=${profile}`,'about:blank']);
|
||||
process.on('SIGTERM',()=>{browser.kill();process.exit(1);});
|
||||
let socket,logs=[];
|
||||
@@ -17,24 +17,50 @@ try {
|
||||
const wait=async(expression,seconds=15)=>{for(let n=0;n<seconds*5;n++){if(await evaluate(expression))return;await delay(200);}if(screenshot){const {data}=await call('Page.captureScreenshot',{format:'png'});await writeFile(screenshot,Buffer.from(data,'base64'));}throw Error('Timeout: '+expression+'\n'+logs.slice(-30).join('\n'));};
|
||||
await call('Runtime.enable');await call('Log.enable');await call('Page.enable');
|
||||
// Only synthetic fixture diagnostics; production does not log article URLs.
|
||||
await call('Page.addScriptToEvaluateOnNewDocument',{source:`if(location.pathname.endsWith('/browser/'))import('/_/api/v1/extension/organizer.feeds/browser/assets/v0.0.1/gecko.js').then(({Gecko})=>{const init=Gecko.prototype.init;Gecko.prototype.init=function(){this.opts.printErr=console.warn;this.opts.print=console.log;return init.call(this);};});`});
|
||||
await call('Page.addScriptToEvaluateOnNewDocument',{source:`if(location.pathname.endsWith('/browser/'))import('/_/api/v1/extension/organizer.feeds/browser/assets/v0.0.1/gecko.js').then(({Gecko})=>{const init=Gecko.prototype.init;Gecko.prototype.init=function(){window.fixtureEngine=this;this.opts.printErr=console.warn;this.opts.print=console.log;return init.call(this);};});`});
|
||||
await call('Emulation.setDeviceMetricsOverride',{width:1280,height:850,deviceScaleFactor:1,mobile:false});await call('Page.navigate',{url});
|
||||
await wait(`document.querySelector('.feed-row')`);
|
||||
if(!await evaluate('crossOriginIsolated'))throw Error('Parent not isolated');
|
||||
await evaluate(`window.geckoReady=false;addEventListener('message',e=>{if(e.data?.type==='pageswiki-gecko'&&e.data.ready)geckoReady=true;});document.querySelector('.feed-row').click()`);
|
||||
await wait(`document.querySelector('.feed-original')`);await evaluate(`document.querySelector('.feed-original').click()`);
|
||||
await wait(`document.querySelector('.feed-external iframe')?.contentWindow.crossOriginIsolated`);
|
||||
if(mode==='startup-failure'){
|
||||
await wait(`document.querySelector('.feed-external [role=alert]')&&!document.querySelector('.feed-external iframe')`);
|
||||
if(!await evaluate(`document.querySelector('.feed-external a[target=_blank]').href==='http://fixture.invalid/first'`))throw Error('Failure lost original fallback');
|
||||
await evaluate(`document.querySelector('.feed-external').close()`);
|
||||
console.log('Gecko startup failure discarded iframe and retained original fallback');
|
||||
}else{
|
||||
await wait(`geckoReady || document.querySelector('.feed-external iframe')?.contentDocument?.querySelector('#status')?.textContent.includes('Viewer failed')`,100);
|
||||
if(!await evaluate('geckoReady'))throw Error('Gecko startup failed\n'+logs.slice(-8).join('\n'));
|
||||
if(mode==='public-https'){
|
||||
const engine=`document.querySelector('.feed-external iframe').contentWindow.fixtureEngine`;
|
||||
await wait(`(async()=>/documentation examples/i.test(String(await ${engine}.evalChrome('document.body.textContent'))))()`,30);
|
||||
await evaluate(`${engine}.load('https://expired.badssl.com/')`);
|
||||
await wait(`(async()=>/^about:(neterror|certerror)/.test(String(await ${engine}.evalChrome('String(document.documentURI)'))))()`,30);
|
||||
const location=await evaluate(`${engine}.evalChrome('String(document.documentURI)')`);
|
||||
if(!String(location).startsWith('about:neterror')&&!String(location).startsWith('about:certerror'))throw Error('Expired certificate was not rejected: '+location);
|
||||
await wait(`document.querySelector('.feed-external iframe').contentDocument.querySelector('canvas').hidden && document.querySelector('.feed-external iframe').contentDocument.querySelector('#status').textContent.length>20`);
|
||||
await evaluate(`document.querySelector('.feed-external').close()`);
|
||||
console.log('Public HTTPS example.com rendered; expired.badssl.com certificate rejected; closed');
|
||||
}else{
|
||||
const pixel=`(()=>{const c=document.querySelector('.feed-external iframe').contentDocument.querySelector('canvas');return Array.from(c.getContext('2d').getImageData(400,200,1,1).data)})()`;
|
||||
await wait(`${pixel}[0]===255 && ${pixel}[1]===0`);
|
||||
await evaluate(`document.querySelector('.feed-external button[title*="Ctrl+F"]').click();document.querySelector('.feed-gecko-find input').value='学舎の園';document.querySelector('.feed-gecko-find').requestSubmit()`);
|
||||
await wait(`(async()=>String(await document.querySelector('.feed-external iframe').contentWindow.fixtureEngine.evalChrome('String(window.getSelection())'))==='学舎の園')()`);
|
||||
await wait(`getComputedStyle(document.querySelector('.feed-gecko-find')).display==='flex'`);
|
||||
await evaluate(`document.querySelector('.feed-gecko-find input').value='missing-synthetic-word';document.querySelector('.feed-gecko-find').requestSubmit()`);
|
||||
await wait(`document.querySelector('.feed-gecko-find output').textContent.length>0`);
|
||||
await evaluate(`document.querySelector('.feed-gecko-find button:last-child').click()`);
|
||||
await wait(`getComputedStyle(document.querySelector('.feed-gecko-find')).display==='none'`);
|
||||
const rect=await evaluate(`(()=>{const r=document.querySelector('.feed-external iframe').getBoundingClientRect();return {x:r.x+100,y:r.y+50}})()`);
|
||||
await call('Input.dispatchMouseEvent',{type:'mousePressed',...rect,button:'left',buttons:1,clickCount:1});await call('Input.dispatchMouseEvent',{type:'mouseReleased',...rect,button:'left',buttons:0,clickCount:1});
|
||||
await wait(`${pixel}[0]===0 && ${pixel}[1]===255`);
|
||||
await wait(`document.querySelector('.feed-external a').href==='http://fixture.invalid/next'`);
|
||||
await evaluate(`document.querySelector('.feed-external button').click()`);
|
||||
await evaluate(`[...document.querySelectorAll('.feed-external-bar button')].find(b=>b.textContent==='←').click()`);
|
||||
await wait(`${pixel}[0]===255 && ${pixel}[1]===0`);
|
||||
if(screenshot){const {data}=await call('Page.captureScreenshot',{format:'png'});await writeFile(screenshot,Buffer.from(data,'base64'));}
|
||||
await evaluate(`document.querySelector('.feed-external').close()`);await wait(`!document.querySelector('.feed-external')`);
|
||||
console.log('Real Gecko: isolation, Wisp article render, click navigation, viewer-local back and teardown passed');
|
||||
console.log('Real Gecko: isolation, Japanese find/missing match, Wisp article render, click navigation, viewer-local back and teardown passed');
|
||||
}
|
||||
}
|
||||
}finally{socket?.close();browser.kill();await new Promise(resolve=>browser.once('exit',resolve));await delay(300);}
|
||||
Vendored
+15
-2
@@ -9,6 +9,7 @@
|
||||
w.fetch=async(...args)=>{pendingRequests++;try{const response=await fetch(...args);await response.clone().arrayBuffer();return response;}finally{pendingRequests--;}};
|
||||
for(const key of Object.keys(w.localStorage))if(key.startsWith('pageswiki.feed-folders.v1:'))w.localStorage.removeItem(key);
|
||||
await wait(()=>d.querySelectorAll('.feed-row').length===100);
|
||||
const published=d.querySelector('.feed-published-time');assert(/^\d{2}:\d{2}$/.test(published.textContent),'Publication time missing');assert(published.title.includes('2026')&&published.title.includes('JST'),'Publication timezone/full tooltip missing');assert(w.getComputedStyle(published).display!=='none','Publication time hidden on mobile');assert(d.querySelector('.feed-row').children.length===5,'Publication time column missing');
|
||||
assert(d.documentElement.scrollWidth<=w.innerWidth,'Viewport overflow');
|
||||
assert(d.title.startsWith('RSS ·'),'RSS title');assert(d.getElementById('feed-status').textContent==='','Unrequested status copy');assert(w.getComputedStyle(d.getElementById('feed-status')).display==='none','Empty status takes space');
|
||||
for(const a of d.querySelectorAll('.organizer-nav a')){assert(a.getAttribute('aria-label'),'Unnamed navigation');if(w.innerWidth<=700)assert(w.getComputedStyle(a.querySelector('span')).display==='none','Mobile tab label visible');}
|
||||
@@ -62,6 +63,7 @@
|
||||
tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('[data-feed="feed/1"]'));
|
||||
tech().querySelector('[data-feed="feed/1"]').click();await wait(()=>d.querySelector('.feed-row')?.textContent.includes('Catalog first'));
|
||||
const globalSearch=d.querySelector('#feed-toolbar input');globalSearch.value='Catalog';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220);
|
||||
folderKey('H');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name'));
|
||||
folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
|
||||
assert(globalSearch.value==='Catalog'&&!d.querySelector('button[data-feed][aria-current="true"]'),'Folder navigation lost search or kept subscription filter');
|
||||
d.querySelector('.feed-row').click();await wait(()=>d.querySelector('#feed-reader .feed-body'));
|
||||
@@ -71,14 +73,25 @@
|
||||
d.querySelector('.feed-capture').close();await wait(()=>!d.querySelector('.feed-capture'));
|
||||
folderKey('J');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));
|
||||
assert(!d.querySelector('.feed-layout').classList.contains('reading')&&!d.querySelector('#feed-reader .feed-body'),'Folder navigation left old article open');
|
||||
catalogFind.value='Other';catalogFind.dispatchEvent(new w.Event('input',{bubbles:true}));assert(!activeFolder(),'Catalog filter setup');
|
||||
const currentCatalogFind=d.querySelector('.feed-catalog input');currentCatalogFind.value='Other';currentCatalogFind.dispatchEvent(new w.Event('input',{bubbles:true}));assert(!activeFolder(),'Catalog filter setup');
|
||||
folderKey('K');await wait(()=>activeFolder()===folderNames()[1]&&d.querySelector('.feed-row')?.textContent.includes('Catalog second'));
|
||||
assert(d.querySelector('.feed-catalog input').value==='','Folder navigation left selected folder hidden by catalog filter');
|
||||
globalSearch.value='';globalSearch.dispatchEvent(new w.Event('input',{bubbles:true}));await delay(220);
|
||||
// Rapid repeats must settle on the latest folder rather than an older request.
|
||||
folderKey('J');folderKey('J');folderKey('K');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name')&&d.querySelector('.feed-row')?.textContent.includes('Catalog first'));await delay(100);
|
||||
assert(d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first'),'Stale folder request won');
|
||||
tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false');
|
||||
// Horizontal hierarchy navigation, with unchanged article filters and bounded siblings.
|
||||
folderKey('L');await wait(()=>tech().querySelector('button[data-feed="feed/1"][aria-current="true"]'));
|
||||
assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='true','L did not expand folder');
|
||||
folderKey('L');folderKey('J');folderKey('K');await delay(80);
|
||||
assert(tech().querySelector('button[data-feed="feed/1"][aria-current="true"]'),'Leaf keys escaped the current folder');
|
||||
folderKey('H');await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name'));
|
||||
folderKey('H');await wait(()=>d.querySelector('.feed-tree-all[aria-current="true"]'));
|
||||
assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','H did not collapse folder');
|
||||
folderKey('H');await delay(80);assert(d.querySelector('.feed-tree-all[aria-current="true"]'),'H escaped catalog root');
|
||||
folderKey('L');await wait(()=>activeFolder()===folderNames()[0]);
|
||||
folderKey('L');await delay(80);assert(activeFolder()===folderNames()[0]&&!folderNames()[0].closest('.feed-folder').querySelector('button[data-feed]'),'L fabricated an empty-folder child');
|
||||
tech().querySelector('.feed-folder-name').click();await wait(()=>activeFolder()===tech().querySelector('.feed-folder-name'));
|
||||
tech().querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===1&&d.querySelector('.feed-row').textContent.includes('Catalog first'));
|
||||
assert(tech().querySelector('.feed-folder-toggle').getAttribute('aria-expanded')==='false','Selecting folder expands it unexpectedly');tech().querySelector('.feed-folder-toggle').click();await wait(()=>tech().querySelector('button[data-feed="feed/1"]'));
|
||||
const empty=folder('user/-/label/Empty/Folder');assert(empty.querySelector('.feed-folder-name span').textContent==='Empty/Folder','Slash label split');empty.querySelector('.feed-folder-toggle').click();assert(!empty.querySelector('button[data-feed]'),'Empty folder fabricated a feed');empty.querySelector('.feed-folder-name').click();await wait(()=>d.querySelectorAll('.feed-row').length===0);
|
||||
|
||||
+14
-2
@@ -132,7 +132,7 @@ window.addEventListener('load', async () => {
|
||||
await wait(()=>$('status').textContent.includes('ありません'));
|
||||
// Deletion shares the archive footer, not a UUID-search workflow.
|
||||
$('new').click();await wait(()=>document.querySelector('#tasks-form [name=title]'));
|
||||
if(!$('delete').hidden)throw Error('Unsaved task must not offer deletion');$('close').click();
|
||||
if(!$('delete').hidden)throw Error('Unsaved task must not offer deletion');if(!$('page-open').hidden||$('page-open').hasAttribute('href'))throw Error('New task leaked a related page');$('close').click();
|
||||
$('quick-title').value='Disposable deletion fixture';$('capture').requestSubmit();
|
||||
await wait(()=>$('list').querySelector('[data-action=detail]'));$('list').querySelector('[data-action=detail]').click();
|
||||
await wait(()=>!$('delete').hidden);
|
||||
@@ -142,7 +142,19 @@ window.addEventListener('load', async () => {
|
||||
if(!$('dialog').open)throw Error('Cancelled deletion closed the editor');
|
||||
const deletionAPI='/_/api/v1/extension/organizer.tasks/items/'+deletionID;
|
||||
const deletionItem=(await fetch(deletionAPI).then(r=>r.json())).item;
|
||||
const changed=await fetch(deletionAPI,{method:'PUT',headers:{'Content-Type':'application/json','If-Match':'"'+deletionItem.version+'"'},body:JSON.stringify({...deletionItem,title:'Updated deletion fixture'})});
|
||||
if(!$('page-open').hidden)throw Error('Unlinked task offered related page');
|
||||
// The footer link uses the saved, authorized path, never unsaved input.
|
||||
value('page_path','/projects/browser');$('form').requestSubmit();await wait(()=>!$('dialog').open);
|
||||
$('list').querySelector('[data-action=detail]').click();await wait(()=>!$('page-open').hidden);
|
||||
const link=$('page-open');if(link.parentElement!==$('save').parentElement||link.pathname!=='/projects/browser'||link.target!=='_blank'||!link.rel.includes('noopener'))throw Error('Unsafe or misplaced related page navigation');
|
||||
value('title','Unsaved title kept');if(link.hidden||!$('dialog').open)throw Error('Editing task hid the saved page navigation');
|
||||
if(innerWidth<761&&link.getBoundingClientRect().height<44)throw Error('Related page touch target too small');
|
||||
document.querySelector('.task-editor-advanced').open=true;
|
||||
value('page_path','//external.invalid/unsaved');if(!link.hidden||link.hasAttribute('href'))throw Error('Unsaved path exposed a link');
|
||||
value('page_path','/projects/browser');if(link.hidden)throw Error('Restored path did not restore related link');
|
||||
window.confirm=()=>true;$('close').click();$('list').querySelector('[data-action=detail]').click();await wait(()=>!$('page-open').hidden);
|
||||
const freshDeletionItem=(await fetch(deletionAPI).then(r=>r.json())).item;
|
||||
const changed=await fetch(deletionAPI,{method:'PUT',headers:{'Content-Type':'application/json','If-Match':'"'+freshDeletionItem.version+'"'},body:JSON.stringify({...freshDeletionItem,title:'Updated deletion fixture'})});
|
||||
if(!changed.ok)throw Error('Deletion conflict fixture failed');
|
||||
window.confirm=()=>true;$('delete').click();await wait(()=>$('error').textContent.includes('別の画面'));
|
||||
if(!$('dialog').open||$('delete').disabled)throw Error('Deletion failure did not keep a usable editor');
|
||||
|
||||
@@ -39,6 +39,7 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
defer r.MultipartForm.RemoveAll()
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, "file is required", http.StatusBadRequest)
|
||||
@@ -83,6 +84,11 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
objectUUID, err := s.store.ResolvePathInDomain(domainUUID, targetPath)
|
||||
createOnly := r.Header.Get("If-None-Match") == "*"
|
||||
if err == nil && createOnly {
|
||||
http.Error(w, "file already exists", http.StatusPreconditionFailed)
|
||||
return
|
||||
}
|
||||
if err == nil && mode == "enforcing" {
|
||||
// upload and write-once grants never overwrite an existing object.
|
||||
if s.store.Authorize(store.AuthorizationRequest{DomainUUID: domainUUID, PrincipalUUID: principal.UUID, Capability: "write", TargetType: "path", TargetID: targetPath}) != nil {
|
||||
@@ -92,6 +98,13 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
objectUUID, err = s.store.CreatePageAtPathInDomain(domainUUID, targetPath)
|
||||
if err != nil && createOnly {
|
||||
// A concurrent creator wins; never turn this into an overwrite.
|
||||
if _, resolveErr := s.store.ResolvePathInDomain(domainUUID, targetPath); resolveErr == nil {
|
||||
http.Error(w, "file already exists", http.StatusPreconditionFailed)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
@@ -113,17 +126,23 @@ func (s *Server) handleUpload(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.TrimSpace(language) == "" {
|
||||
language = "und"
|
||||
}
|
||||
if _, err := s.store.SaveRevisionContent(
|
||||
revision, err := s.store.SaveRevisionContent(
|
||||
objectUUID,
|
||||
payload,
|
||||
"web upload: "+originalName,
|
||||
language,
|
||||
contentType,
|
||||
originalName,
|
||||
); err != nil {
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if strings.Contains(r.Header.Get("Accept"), "application/json") {
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
writeEventJSON(w, http.StatusCreated, map[string]string{"path": targetPath, "object_uuid": objectUUID, "revision_uuid": revision.UUID})
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, targetPath, http.StatusSeeOther)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -85,6 +86,51 @@ func TestWebUploadCreatesAndRevisesSpecifiedPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebUploadCreateOnlyJSON(t *testing.T) {
|
||||
s, token, domain := configFixture(t)
|
||||
upload := func(payload string, authenticated bool) *httptest.ResponseRecorder {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
writer.WriteField("path", "/uploads/日本語 #1.txt")
|
||||
file, _ := writer.CreateFormFile("file", "日本語 #1.txt")
|
||||
file.Write([]byte(payload))
|
||||
writer.Close()
|
||||
r := httptest.NewRequest("POST", "http://localhost/v/upload", &body)
|
||||
r.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
r.Header.Set("Accept", "application/json")
|
||||
r.Header.Set("If-None-Match", "*")
|
||||
if authenticated {
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
s.handleUpload(w, r)
|
||||
return w
|
||||
}
|
||||
if w := upload("denied", false); w.Code != 403 {
|
||||
t.Fatalf("unauthorized: %d", w.Code)
|
||||
}
|
||||
w := upload("original", true)
|
||||
var result map[string]string
|
||||
if w.Code != 201 || json.Unmarshal(w.Body.Bytes(), &result) != nil || result["path"] != "/uploads/日本語 #1.txt" || result["revision_uuid"] == "" {
|
||||
t.Fatalf("JSON upload %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w = upload("overwrite", true); w.Code != 412 {
|
||||
t.Fatalf("duplicate: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
object, err := s.store.ResolvePathInDomain(domain, result["path"])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
revisions, err := s.store.ListRevisions(object)
|
||||
if err != nil || len(revisions) != 1 {
|
||||
t.Fatalf("unexpected revisions %d %v", len(revisions), err)
|
||||
}
|
||||
rev, err := s.store.GetRevision(revisions[0].UUID)
|
||||
if err != nil || rev.Body != "original" {
|
||||
t.Fatal("overwritten", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustServerDomainUUID(t *testing.T, st *store.Store) string {
|
||||
t.Helper()
|
||||
domain, err := st.GetDefaultDomain()
|
||||
|
||||
@@ -164,6 +164,10 @@ func (s *Store) CreateBrowserAuthSession(domainUUID, principalUUID string, mfaLe
|
||||
}
|
||||
|
||||
func (s *Store) createTypedAuthSession(domainUUID, principalUUID string, mfaLevel int, prefix string) (string, error) {
|
||||
return s.createTypedAuthSessionForLifetime(domainUUID, principalUUID, mfaLevel, prefix, s.AuthSessionLifetime(domainUUID))
|
||||
}
|
||||
|
||||
func (s *Store) createTypedAuthSessionForLifetime(domainUUID, principalUUID string, mfaLevel int, prefix string, lifetime time.Duration) (string, error) {
|
||||
raw, _, err := authToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -177,7 +181,7 @@ func (s *Store) createTypedAuthSession(domainUUID, principalUUID string, mfaLeve
|
||||
result, err := s.DB.Exec(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level)
|
||||
SELECT ?,?,p.uuid,m.domain_uuid,?,?,?,? FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid
|
||||
WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.domain_uuid=? AND m.state='active'`,
|
||||
uuid.NewString(), hash, now, now+s.AuthSessionLifetime(domainUUID).Milliseconds(), now, mfaLevel, principalUUID, domainUUID)
|
||||
uuid.NewString(), hash, now, now+lifetime.Milliseconds(), now, mfaLevel, principalUUID, domainUUID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ var cliAuthMigrations = []db.SQLMigration{{Version: 1, Name: "cli_oidc_handoff",
|
||||
schemaUnique("auth_cli_requests", "id"), schemaUnique("auth_cli_requests", "device_hash"),
|
||||
schemaIndex("auth_cli_requests", "auth_cli_domain_created", "domain_uuid,created_ms"),
|
||||
schemaIndex("auth_cli_requests", "auth_cli_expiry", "expires_ms"),
|
||||
}}}
|
||||
}}, {Version: 2, Name: "cli_trusted_session_choice", Statements: []string{
|
||||
`ALTER TABLE auth_cli_requests ADD COLUMN remember INTEGER NOT NULL DEFAULT 0 CHECK(remember IN (0,1))`,
|
||||
}, Assertions: []string{schemaColumns("auth_cli_requests", "remember")}}}
|
||||
|
||||
var (
|
||||
ErrCLIPending = errors.New("authorization_pending")
|
||||
@@ -39,11 +41,16 @@ var (
|
||||
type CLIAuthRequest struct {
|
||||
ID, Origin, State, BrowserHash, PrincipalUUID string
|
||||
ExpiresMS int64
|
||||
Remember bool
|
||||
}
|
||||
|
||||
// Admission is one write statement; concurrent unauthenticated requests cannot
|
||||
// race the caps. Requests are retained for ten minutes for issuance accounting.
|
||||
func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge string) (id, device, code string, err error) {
|
||||
return s.StartCLIAuthForLogin(ctx, domain, origin, challenge, false)
|
||||
}
|
||||
|
||||
func (s *Store) StartCLIAuthForLogin(ctx context.Context, domain, origin, challenge string, remember bool) (id, device, code string, err error) {
|
||||
if s.ReadOnly {
|
||||
return "", "", "", ErrReadOnly
|
||||
}
|
||||
@@ -66,10 +73,10 @@ func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge stri
|
||||
return
|
||||
}
|
||||
var result sql.Result
|
||||
result, err = s.DB.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms)
|
||||
SELECT ?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM auth_cli_requests)<4096
|
||||
result, err = s.DB.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms,remember)
|
||||
SELECT ?,?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM auth_cli_requests)<4096
|
||||
AND (SELECT count(*) FROM auth_cli_requests WHERE domain_uuid=? AND created_ms>?)<64`,
|
||||
id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, domain, now-300000)
|
||||
id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, remember, domain, now-300000)
|
||||
if err == nil {
|
||||
if n, e := result.RowsAffected(); e != nil {
|
||||
err = e
|
||||
@@ -82,7 +89,7 @@ func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge stri
|
||||
|
||||
func (s *Store) CLIAuthRequest(ctx context.Context, domain, origin, id string) (*CLIAuthRequest, error) {
|
||||
var grant CLIAuthRequest
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms FROM auth_cli_requests WHERE id=? AND domain_uuid=? AND origin=? AND expires_ms>?`, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS)
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms,remember FROM auth_cli_requests WHERE id=? AND domain_uuid=? AND origin=? AND expires_ms>?`, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS, &grant.Remember)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrCLIExpired
|
||||
}
|
||||
@@ -168,15 +175,17 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
lifetime := s.AuthSessionLifetime(domain).Milliseconds()
|
||||
trustedLifetime := s.LoginSessionLifetime(domain, true).Milliseconds()
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var id, state, browser string
|
||||
var remember bool
|
||||
err = tx.QueryRowContext(ctx, `UPDATE auth_cli_requests SET next_poll_ms=?,state=CASE WHEN state='approved' THEN 'consumed' ELSE state END
|
||||
WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND next_poll_ms<=? AND state IN ('pending','approved','denied')
|
||||
RETURNING id,state,browser_hash`, now+5000, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now, now).Scan(&id, &state, &browser)
|
||||
RETURNING id,state,browser_hash,remember`, now+5000, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now, now).Scan(&id, &state, &browser, &remember)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
var exists int
|
||||
e := tx.QueryRowContext(ctx, `SELECT count(*) FROM auth_cli_requests WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND state IN ('pending','approved','denied')`, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now).Scan(&exists)
|
||||
@@ -204,6 +213,9 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
return
|
||||
}
|
||||
var raw string
|
||||
if remember {
|
||||
lifetime = trustedLifetime
|
||||
}
|
||||
raw, _, err = authToken()
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -8,10 +8,68 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
const cliTestOrigin = "https://wiki.example"
|
||||
|
||||
func TestCLIAuthTrustedMigrationPreservesExistingRequests(t *testing.T) {
|
||||
s := newSchemaTestStore(t)
|
||||
ctx := context.Background()
|
||||
if err := db.ApplySQLiteMigrations(ctx, s.DB, "cli-auth", cliAuthMigrations[:1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms) VALUES('old','domain','https://wiki.example','device','challenge','code',1,2)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := db.ApplySQLiteMigrations(ctx, s.DB, "cli-auth", cliAuthMigrations); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var remember int
|
||||
var challenge string
|
||||
if err := s.DB.QueryRow(`SELECT remember,challenge FROM auth_cli_requests WHERE id='old'`).Scan(&remember, &challenge); err != nil || remember != 0 || challenge != "challenge" {
|
||||
t.Fatalf("old grant changed: %d %q %v", remember, challenge, err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`UPDATE auth_cli_requests SET remember=2`); err == nil {
|
||||
t.Fatal("invalid remember flag accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTOTPLoginBindsTrustedChoice(t *testing.T) {
|
||||
for _, remember := range []bool{false, true} {
|
||||
s, p, _ := cliAuthFixture(t)
|
||||
secret, err := s.BeginTOTPEnrollment(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, err := totpCode(secret, time.Now().Unix()/30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.ConfirmTOTPEnrollment(p, code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Synthetic fixture: permit this time step once for login after enrollment.
|
||||
if _, err = s.DB.Exec(`UPDATE auth_credentials SET parameters_json='{"last_counter":0}' WHERE principal_uuid=? AND kind='totp'`, p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
challenge, err := s.CreateTOTPLoginChallengeForLogin("domain", p, remember)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, choice, err := s.CompleteTOTPLoginChallengeForLogin("domain", challenge, code)
|
||||
if err != nil || got != p || choice != remember {
|
||||
t.Fatalf("bound choice: %q %v %v", got, choice, err)
|
||||
}
|
||||
if _, _, err = s.CompleteTOTPLoginChallengeForLogin("domain", challenge, code); err == nil {
|
||||
t.Fatal("challenge replay accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cliAuthFixture(t *testing.T) (*Store, string, string) {
|
||||
t.Helper()
|
||||
s := newSchemaTestStore(t)
|
||||
|
||||
@@ -30,3 +30,37 @@ func (s *Store) SetAuthSessionLifetimeMinutes(domain string, minutes int) error
|
||||
func (s *Store) AuthSessionLifetime(domain string) time.Duration {
|
||||
return time.Duration(s.AuthSessionLifetimeMinutes(domain)) * time.Minute
|
||||
}
|
||||
|
||||
// Opt-in persistent sessions have a separate, administrator-controlled ceiling.
|
||||
// Zero disables the option. Existing sessions retain their original expiry.
|
||||
func (s *Store) TrustedSessionDays(domain string) int {
|
||||
raw, err := s.GetConfigInDomain(domain, "auth_trusted_session_days")
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
if raw == "" {
|
||||
return 30
|
||||
}
|
||||
days, err := strconv.Atoi(raw)
|
||||
if err != nil || days < 0 || days > 30 {
|
||||
return 0
|
||||
}
|
||||
return days
|
||||
}
|
||||
|
||||
func (s *Store) LoginSessionLifetime(domain string, remember bool) time.Duration {
|
||||
if days := s.TrustedSessionDays(domain); remember && days > 0 {
|
||||
return time.Duration(days) * 24 * time.Hour
|
||||
}
|
||||
return s.AuthSessionLifetime(domain)
|
||||
}
|
||||
|
||||
func (s *Store) CreateLoginSession(domain, principal string, mfa int, browser, remember bool) (string, time.Duration, error) {
|
||||
lifetime := s.LoginSessionLifetime(domain, remember)
|
||||
prefix := "pw_api_"
|
||||
if browser {
|
||||
prefix = "pw_browser_"
|
||||
}
|
||||
token, err := s.createTypedAuthSessionForLifetime(domain, principal, mfa, prefix, lifetime)
|
||||
return token, lifetime, err
|
||||
}
|
||||
@@ -176,36 +176,51 @@ func TOTPURI(secret, account, issuer string) string {
|
||||
}
|
||||
|
||||
func (s *Store) CreateTOTPLoginChallenge(domainUUID, principalUUID string) (string, error) {
|
||||
return s.CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID, false)
|
||||
}
|
||||
|
||||
func (s *Store) CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID string, remember bool) (string, error) {
|
||||
token, hash, err := authToken()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
payload, _ := json.Marshal(map[string]string{"principal_uuid": principalUUID})
|
||||
payload, _ := json.Marshal(map[string]any{"principal_uuid": principalUUID, "remember": remember})
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now)
|
||||
return token, err
|
||||
}
|
||||
func (s *Store) CompleteTOTPLoginChallenge(domainUUID, token, code string) (string, error) {
|
||||
id, _, err := s.CompleteTOTPLoginChallengeForLogin(domainUUID, token, code)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code string) (string, bool, error) {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
hash := base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
var challengeUUID, principalUUID string
|
||||
var challengeUUID, principalUUID, payload string
|
||||
var expires int64
|
||||
var attempts int
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts)
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload)
|
||||
if err != nil || expires < time.Now().UnixMilli() || attempts >= 5 {
|
||||
return "", ErrAccessDenied
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
var options struct {
|
||||
Remember bool `json:"remember"`
|
||||
}
|
||||
if json.Unmarshal([]byte(payload), &options) != nil {
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
if err = s.VerifyTOTP(principalUUID, code); err != nil {
|
||||
_, _ = s.DB.Exec(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN ? ELSE consumed_at_ms END WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
return "", err
|
||||
return "", false, err
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=? WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", false, err
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
return "", ErrAccessDenied
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
return principalUUID, nil
|
||||
return principalUUID, options.Remember, nil
|
||||
}
|
||||
@@ -81,7 +81,11 @@ func minInt64(a, b int64) int64 {
|
||||
}
|
||||
|
||||
func (s *Store) GetRevisionMetadata(revisionUUID string) (*Revision, error) {
|
||||
row := s.DB.QueryRow(`SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE uuid=?`, revisionUUID)
|
||||
return s.GetRevisionMetadataContext(context.Background(), revisionUUID)
|
||||
}
|
||||
|
||||
func (s *Store) GetRevisionMetadataContext(ctx context.Context, revisionUUID string) (*Revision, error) {
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE uuid=?`, revisionUUID)
|
||||
return scanRevision(row)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
@@ -69,6 +70,10 @@ func (s *Store) ResolvePathInDomain(
|
||||
domainUUID string,
|
||||
path string,
|
||||
) (string, error) {
|
||||
return s.ResolvePathInDomainContext(context.Background(), domainUUID, path)
|
||||
}
|
||||
|
||||
func (s *Store) ResolvePathInDomainContext(ctx context.Context, domainUUID, path string) (string, error) {
|
||||
path = normalizePath(path)
|
||||
if err := s.ensureLocalDomain(domainUUID); err != nil {
|
||||
return "", err
|
||||
@@ -76,7 +81,7 @@ func (s *Store) ResolvePathInDomain(
|
||||
|
||||
var objectUUID string
|
||||
|
||||
err := s.DB.QueryRow(`
|
||||
err := s.DB.QueryRowContext(ctx, `
|
||||
SELECT object_uuid
|
||||
FROM paths
|
||||
WHERE domain_uuid = ?
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
@@ -324,6 +325,10 @@ func (s *Store) GetRevision(
|
||||
}
|
||||
|
||||
func (s *Store) GetLatestRevisionMetadataForLanguages(objectUUID string, rawLanguages []string) (*Revision, error) {
|
||||
return s.GetLatestRevisionMetadataForLanguagesContext(context.Background(), objectUUID, rawLanguages)
|
||||
}
|
||||
|
||||
func (s *Store) GetLatestRevisionMetadataForLanguagesContext(ctx context.Context, objectUUID string, rawLanguages []string) (*Revision, error) {
|
||||
candidates := make([]string, 0, len(rawLanguages)+1)
|
||||
seen := map[string]bool{}
|
||||
for _, rawLanguage := range rawLanguages {
|
||||
@@ -336,7 +341,7 @@ func (s *Store) GetLatestRevisionMetadataForLanguages(objectUUID string, rawLang
|
||||
candidates = append(candidates, "und")
|
||||
}
|
||||
for _, lang := range candidates {
|
||||
row := s.DB.QueryRow(`SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE object_uuid=? AND COALESCE(NULLIF(lang,''),'und')=? ORDER BY COALESCE(created_at_ms,created_at*1000) DESC,rowid DESC LIMIT 1`, objectUUID, lang)
|
||||
row := s.DB.QueryRowContext(ctx, `SELECT uuid,object_uuid,COALESCE(document_hash,''),COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),'',COALESCE(message,''),COALESCE(created_at,0),COALESCE(created_at_ms,COALESCE(created_at,0)*1000) FROM revisions WHERE object_uuid=? AND COALESCE(NULLIF(lang,''),'und')=? ORDER BY COALESCE(created_at_ms,created_at*1000) DESC,rowid DESC LIMIT 1`, objectUUID, lang)
|
||||
revision, err := scanRevision(row)
|
||||
if err == nil {
|
||||
return revision, nil
|
||||
|
||||
Reference in new issue
Block a user