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,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を表示できません(権限・形式・サイズ制限)。"
|
||||
}
|
||||
Reference in new issue
Block a user