package server import ( "archive/zip" "context" "io" "mime" "net/http" "os/exec" "path/filepath" "strings" "time" "pageswiki/internal/store" ) func (s *Server) handleArchiveEntry(w http.ResponseWriter, r *http.Request) { path := r.URL.Query().Get("path") entry := r.URL.Query().Get("entry") metadataOnly := r.URL.Query().Get("metadata") == "1" if path == "" || (!metadataOnly && entry == "") || len(entry) > 4096 || strings.ContainsAny(entry, "\x00\r\n") { http.Error(w, "path and entry are required", http.StatusBadRequest) return } if !s.requestCanReadPath(r, path) { http.Error(w, "forbidden", http.StatusForbidden) return } objectUUID, err := s.store.ResolvePathInDomain(s.domainUUIDForRequest(r), path) if err != nil { http.NotFound(w, r) return } var revision *store.Revision if revisionUUID := r.URL.Query().Get("rev"); revisionUUID != "" { revision, err = s.store.GetRevisionMetadata(revisionUUID) } else { revision, err = s.latestRevisionMetadataForRequest(r, objectUUID) } if err != nil || revision.ObjectUUID != objectUUID { http.NotFound(w, r) return } if metadataOnly { s.serveMetadataProjection(w, r, revision.DocumentHash, "archive", path, revision.UUID) return } if strings.EqualFold(filepath.Ext(revision.Filename), ".zip") { s.serveZIPDocumentEntry(w, r, revision.DocumentHash, entry) return } ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second) defer cancel() lease, err := s.store.MaterializeDocument(ctx, revision.DocumentHash, 1<<30) if err != nil { http.Error(w, "archive source unavailable or exceeds materialization limit", http.StatusServiceUnavailable) return } defer lease.Close() command := exec.CommandContext(ctx, "nice", "-n", "10", "7z", "x", "-so", "-y", "-spd", "-mmt1", "-p-", "--", lease.Path, entry) command.WaitDelay = time.Second stdout, err := command.StdoutPipe() if err != nil || command.Start() != nil { http.Error(w, "cannot extract archive entry", http.StatusUnprocessableEntity) return } w.Header().Set("Content-Type", archiveEntryContentType(entry, nil)) w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(entry)})) w.Header().Set("Cache-Control", "private, max-age=60") w.Header().Set("Vary", "Authorization, Cookie") w.Header().Set("X-Content-Type-Options", "nosniff") n, copyErr := io.Copy(w, io.LimitReader(stdout, 128<<20)) if copyErr != nil || n == 128<<20 { // Never wait for a decoder whose output is no longer being drained. cancel() } waitErr := command.Wait() if copyErr != nil || waitErr != nil { return } } func (s *Server) serveZIPDocumentEntry( w http.ResponseWriter, r *http.Request, hash string, entry string, ) { input, err := s.store.OpenDocumentContext(r.Context(), hash) if err != nil { http.NotFound(w, r) return } defer input.Close() size, err := input.Seek(0, io.SeekEnd) if err != nil { http.Error(w, "cannot determine archive size", http.StatusUnprocessableEntity) return } reader, err := zip.NewReader(input, size) if err != nil { http.Error(w, err.Error(), http.StatusUnprocessableEntity) return } for _, file := range reader.File { if file.Name != entry || file.FileInfo().IsDir() { continue } if file.UncompressedSize64 > 128<<20 { http.Error(w, "archive entry exceeds 128 MiB limit", http.StatusRequestEntityTooLarge) return } source, err := file.Open() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer source.Close() w.Header().Set("Content-Type", archiveEntryContentType(file.Name, nil)) w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": filepath.Base(file.Name)})) w.Header().Set("Cache-Control", "private, max-age=60") w.Header().Set("Vary", "Authorization, Cookie") w.Header().Set("X-Content-Type-Options", "nosniff") _, _ = io.Copy(w, io.LimitReader(source, 128<<20)) return } http.NotFound(w, r) }