package server import ( "bytes" "crypto/rand" "math/big" "mime" "net/http" pathpkg "path" "strconv" "strings" "time" "pageswiki/internal/store" ) const defaultHomeWallpaperOpacity = 28 type homeWallpaperView struct { Path string URL string Opacity string Key string } // configuredLoginWallpaper returns only the image selected by the Domain // administrator as the home/login wallpaper. It intentionally does not use // the requesting principal's document permissions: choosing an image as the // login wallpaper makes that one image public on the sign-in surface. func (s *Server) configuredLoginWallpaper(domainUUID string) *store.Revision { configured, _ := s.homeWallpaperConfig(domainUUID) if configured == "" { return nil } if objectUUID, err := s.store.ResolvePathInDomain(domainUUID, configured); err == nil { revision, revisionErr := s.store.GetLatestRevisionForLanguages(objectUUID, []string{s.getLocale(domainUUID), "und"}) if revisionErr == nil && imageContentType(revision.ContentType) { return revision } } entries, err := s.store.ListBrowserEntriesInDomain(domainUUID, configured) if err != nil { return nil } candidates := make([]store.BrowserEntry, 0, len(entries)) for _, entry := range entries { if !entry.IsDir && entry.RevisionUUID != "" && imageContentType(entry.ContentType) { candidates = append(candidates, entry) } } if len(candidates) == 0 { return nil } index := 0 if value, randomErr := rand.Int(rand.Reader, big.NewInt(int64(len(candidates)))); randomErr == nil { index = int(value.Int64()) } revision, err := s.store.GetRevision(candidates[index].RevisionUUID) if err != nil || !imageContentType(revision.ContentType) { return nil } return revision } func (s *Server) handleAuthWallpaper(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet && r.Method != http.MethodHead { w.Header().Set("Allow", "GET, HEAD") http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } revision := s.configuredLoginWallpaper(s.domainUUIDForRequest(r)) if revision == nil { http.NotFound(w, r) return } contentType := revision.ContentType mediaType, _, err := mime.ParseMediaType(contentType) if err != nil || !strings.HasPrefix(strings.ToLower(mediaType), "image/") { http.NotFound(w, r) return } filename := strings.TrimSpace(revision.Filename) if filename == "" { filename = "wallpaper" } w.Header().Set("Cache-Control", "private, no-store") w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filename})) w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("ETag", `"`+revision.DocumentHash+`"`) if strings.EqualFold(mediaType, "image/svg+xml") { w.Header().Set("Content-Security-Policy", "default-src 'none'; sandbox") } http.ServeContent(w, r, filename, time.UnixMilli(revision.CreatedAtMS), bytes.NewReader([]byte(revision.Body))) } func normalizePickerPath(raw string) (string, bool) { raw = strings.TrimSpace(raw) if raw == "" { return "", true } if len(raw) > 4096 || !strings.HasPrefix(raw, "/") || strings.ContainsAny(raw, "\x00\r\n\\") { return "", false } for _, part := range strings.Split(raw, "/") { if part == "." || part == ".." { return "", false } } clean := pathpkg.Clean(raw) if clean == "." || !strings.HasPrefix(clean, "/") { return "", false } return clean, true } func imageContentType(raw string) bool { mediaType, _, err := mime.ParseMediaType(raw) return err == nil && strings.HasPrefix(strings.ToLower(mediaType), "image/") } func (s *Server) homeWallpaperConfig(domainUUID string) (string, int) { path, ok := normalizePickerPath(s.getDomainConfig("home_wallpaper_path", domainUUID)) if !ok { path = "" } opacity := defaultHomeWallpaperOpacity if raw := strings.TrimSpace(s.getDomainConfig("home_wallpaper_opacity", domainUUID)); raw != "" { if parsed, err := strconv.Atoi(raw); err == nil && parsed >= 0 && parsed <= 100 { opacity = parsed } } return path, opacity } func (s *Server) homeWallpaperForRequest(r *http.Request) homeWallpaperView { domainUUID := s.domainUUIDForRequest(r) configured, opacity := s.homeWallpaperConfig(domainUUID) view := homeWallpaperView{Path: configured, Opacity: strconv.FormatFloat(float64(opacity)/100, 'f', 2, 64)} if configured == "" { return view } httpRequest := r if objectUUID, err := s.store.ResolvePathInDomain(domainUUID, configured); err == nil && s.requestCanReadPath(httpRequest, configured) { if revision, err := s.latestRevisionMetadataForRequest(httpRequest, objectUUID); err == nil && imageContentType(revision.ContentType) { view.URL = rawProjectionURL("rev:" + revision.UUID) view.Key = revision.DocumentHash return view } } entries, err := s.store.ListBrowserEntriesInDomain(domainUUID, configured) if err != nil { return view } candidates := make([]store.BrowserEntry, 0, len(entries)) for _, entry := range entries { if !entry.IsDir && imageContentType(entry.ContentType) && s.requestCanReadPath(httpRequest, entry.Path) { candidates = append(candidates, entry) } } if len(candidates) == 0 { return view } index := 0 if value, err := rand.Int(rand.Reader, big.NewInt(int64(len(candidates)))); err == nil { index = int(value.Int64()) } view.URL = rawProjectionURL("rev:" + candidates[index].RevisionUUID) view.Key = candidates[index].DocumentHash return view }