Restore validated Delamain manifest exports

This commit is contained in:
takuya committed 2026-11-29 13:20:29 +03:00
1 parent 527255f5b7
commit 02e1428ff6
2 files changed
+68 -14

No files matched your search

+65 -11
View File
@@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"io" "io"
@@ -9,6 +10,7 @@ import (
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"regexp"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -22,6 +24,8 @@ type apiGuard struct {
notes map[noteHandle]exportedNote notes map[noteHandle]exportedNote
} }
type exportSourceKey struct{}
func main() { func main() {
upstream := &url.URL{Scheme: "http", Host: "go-server:8080"} upstream := &url.URL{Scheme: "http", Host: "go-server:8080"}
server := &http.Server{ server := &http.Server{
@@ -72,8 +76,9 @@ func (g *apiGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
break break
} }
if normalizePackage(w, r) { if source := g.normalizePackage(w, r); source != "" {
g.proxy.ServeHTTP(w, r) ctx := context.WithValue(r.Context(), exportSourceKey{}, source)
g.proxy.ServeHTTP(w, r.WithContext(ctx))
} }
return return
case "/api/slices": case "/api/slices":
@@ -134,7 +139,22 @@ func (g *apiGuard) requireTripOwner(w http.ResponseWriter, r *http.Request, trip
return false return false
} }
func normalizePackage(w http.ResponseWriter, r *http.Request) bool { var (
manifestTripID = regexp.MustCompile(`^[a-fA-F0-9]{8}(?:-[a-fA-F0-9]{4}){3}-[a-fA-F0-9]{12}$`)
manifestVersion = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)
)
type exportManifest struct {
Source string `json:"source"`
Sections []struct {
Segments []struct {
Type string `json:"type"`
Version string `json:"version,omitempty"`
} `json:"segments"`
} `json:"sections"`
}
func (g *apiGuard) normalizePackage(w http.ResponseWriter, r *http.Request) string {
const maxPackageRequest = 1 << 20 const maxPackageRequest = 1 << 20
defer r.Body.Close() defer r.Body.Close()
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxPackageRequest)) body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxPackageRequest))
@@ -145,24 +165,58 @@ func normalizePackage(w http.ResponseWriter, r *http.Request) bool {
} else { } else {
writeError(w, http.StatusBadRequest, "invalid body") writeError(w, http.StatusBadRequest, "invalid body")
} }
return false return ""
} }
// Forward only the trip ID so the backend constructs the export manifest.
var fields map[string]json.RawMessage var fields map[string]json.RawMessage
var tripID string var tripID string
if json.Unmarshal(body, &fields) != nil || len(fields) != 1 || if json.Unmarshal(body, &fields) == nil && len(fields) == 1 &&
json.Unmarshal(fields["trip_id"], &tripID) != nil || tripID == "" { json.Unmarshal(fields["trip_id"], &tripID) == nil && tripID != "" {
writeError(w, http.StatusBadRequest, "provide a trip_id to export")
return false
}
body, _ = json.Marshal(map[string]string{"trip_id": tripID}) body, _ = json.Marshal(map[string]string{"trip_id": tripID})
} else {
manifest, valid := parseManifest(body)
if !valid {
writeError(w, http.StatusBadRequest, "invalid export manifest")
return ""
}
tripID = manifest.Source
if !g.requireTripOwner(w, r, tripID) {
return ""
}
body, _ = json.Marshal(manifest)
}
r.Body = io.NopCloser(bytes.NewReader(body)) r.Body = io.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body)) r.ContentLength = int64(len(body))
r.TransferEncoding = nil r.TransferEncoding = nil
r.Trailer = nil r.Trailer = nil
r.Header.Set("Content-Type", "application/json") r.Header.Set("Content-Type", "application/json")
return true return tripID
}
func parseManifest(body []byte) (exportManifest, bool) {
var manifest exportManifest
decoder := json.NewDecoder(bytes.NewReader(body))
decoder.DisallowUnknownFields()
if decoder.Decode(&manifest) != nil || decoder.Decode(new(any)) != io.EOF ||
!manifestTripID.MatchString(manifest.Source) {
return manifest, false
}
hasRoute := false
for _, section := range manifest.Sections {
if len(section.Segments) == 0 {
return manifest, false
}
for _, segment := range section.Segments {
switch {
case segment.Type == "route" && segment.Version == "":
hasRoute = true
case segment.Type == "note" && manifestVersion.MatchString(segment.Version):
default:
return manifest, false
}
}
}
return manifest, hasRoute
} }
func writeError(w http.ResponseWriter, status int, message string) { func writeError(w http.ResponseWriter, status int, message string) {
+3 -3
View File
@@ -38,13 +38,13 @@ func (g *apiGuard) rememberExport(response *http.Response) error {
response.Body = io.NopCloser(bytes.NewReader(body)) response.Body = io.NopCloser(bytes.NewReader(body))
var result struct { var result struct {
TripID string `json:"trip_id"`
Slot *uint64 `json:"note_slot"` Slot *uint64 `json:"note_slot"`
Generation uint64 `json:"note_generation"` Generation uint64 `json:"note_generation"`
Size int `json:"note_size"` Size int `json:"note_size"`
Data string `json:"data"` Data string `json:"data"`
} }
if json.Unmarshal(body, &result) != nil || result.TripID == "" || result.Slot == nil || tripID, _ := response.Request.Context().Value(exportSourceKey{}).(string)
if json.Unmarshal(body, &result) != nil || !manifestTripID.MatchString(tripID) || result.Slot == nil ||
result.Generation == 0 || result.Size != len(result.Data) || result.Size > 16<<10 { result.Generation == 0 || result.Size != len(result.Data) || result.Size > 16<<10 {
return nil return nil
} }
@@ -67,7 +67,7 @@ func (g *apiGuard) rememberExport(response *http.Response) error {
delete(g.notes, oldestKey) delete(g.notes, oldestKey)
} }
} }
g.notes[key] = exportedNote{tripID: result.TripID, data: result.Data, created: now} g.notes[key] = exportedNote{tripID: tripID, data: result.Data, created: now}
return nil return nil
} }