package server import ( "encoding/json" "github.com/tdurieux/anonymous_github/backend/internal/anonymize" "github.com/tdurieux/anonymous_github/backend/internal/blobcache" "github.com/tdurieux/anonymous_github/backend/internal/filetype" "github.com/tdurieux/anonymous_github/backend/internal/security" "html" "io" "net/http" "net/url" "path" "strconv" "strings" ) var pageIndexPriority = []string{"index.html", "index.htm", "index.md", "index.txt", "index.org", "index.1st", "index", "readme.md", "readme.txt", "readme.org", "readme.1st", "readme"} func (s *Server) webview(q *request) (any, error) { q.W.Header().Set("Content-Security-Policy", "sandbox allow-scripts allow-popups allow-forms allow-modals") q.W.Header().Set("Cache-Control", "private, no-cache, must-revalidate") r, e := q.resource("repo", false) if e != nil { return nil, e } if e = s.checkResource(q.ctx(), "repo", r); e != nil { return nil, e } if _, e = s.authorizeContent(q.ctx(), r); e != nil { return nil, e } opt := obj(r["options"]) page := obj(opt["pageSource"]) if !boolean(opt["page"]) || len(page) == 0 { return nil, fail("page_not_activated", 400) } if str(page["branch"]) != str(obj(r["source"])["branch"]) { return nil, fail("page_not_supported_on_different_branch", 400) } root := strings.TrimPrefix(str(page["path"]), "/") if root == "." { root = "" } if root != "" { if _, e = security.RelativePath(root); e != nil { return nil, fail("invalid_path", 400) } } requested := strings.TrimSuffix(q.Params["path"], "/") if requested != "" { if _, e = security.RelativePath(requested); e != nil { return nil, fail("invalid_path", 400) } } requested = path.Join(root, requested) if requested == "." { requested = "" } selected, e := s.indexedWebFile(q.ctx(), r, requested) if e != nil { return nil, e } children := map[string]M{} // File requests need only one indexed entry. Directory/index rendering keeps // the existing listing behavior, including collisions among child paths. if selected == nil || selected["size"] == nil { selected = nil engine, e := s.engine(r, "repo") if e != nil { return nil, e } rows, e := s.Store.list(q.ctx(), "files", M{"repoId": r["repoId"], "treeGeneration": activeGeneration(r)}, 100000, nil) if e != nil { return nil, e } for _, row := range rows { anonymous, e := engine.Path(q.ctx(), path.Join(str(row["path"]), str(row["name"]))) if e != nil { return nil, e } if anonymous == requested { if selected != nil { return nil, fail("ambiguous_anonymized_path", 409) } selected = row } parent := path.Dir(anonymous) if parent == "." { parent = "" } if parent == requested { if children[path.Base(anonymous)] != nil { return nil, fail("ambiguous_anonymized_path", 409) } children[path.Base(anonymous)] = row } } } if selected == nil || selected["size"] == nil { if !strings.Contains(q.R.Header.Get("Accept"), "text/html") { return nil, fail("file_not_found", 404) } for _, wanted := range pageIndexPriority { for name, row := range children { if strings.EqualFold(name, wanted) && row["size"] != nil { selected = row break } } if selected != nil && selected["size"] != nil { break } } if selected == nil || selected["size"] == nil { var b strings.Builder b.WriteString("