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 (
"bytes"
"context"
"encoding/json"
"errors"
"io"
@@ -9,6 +10,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strings"
"sync"
"time"
@@ -22,6 +24,8 @@ type apiGuard struct {
notes map[noteHandle]exportedNote
}
type exportSourceKey struct{}
func main() {
upstream := &url.URL{Scheme: "http", Host: "go-server:8080"}
server := &http.Server{
@@ -72,8 +76,9 @@ func (g *apiGuard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
break
}
if normalizePackage(w, r) {
g.proxy.ServeHTTP(w, r)
if source := g.normalizePackage(w, r); source != "" {
ctx := context.WithValue(r.Context(), exportSourceKey{}, source)
g.proxy.ServeHTTP(w, r.WithContext(ctx))
}
return
case "/api/slices":
@@ -134,7 +139,22 @@ func (g *apiGuard) requireTripOwner(w http.ResponseWriter, r *http.Request, trip
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
defer r.Body.Close()
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, maxPackageRequest))
@@ -145,24 +165,58 @@ func normalizePackage(w http.ResponseWriter, r *http.Request) bool {
} else {
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 tripID string
if json.Unmarshal(body, &fields) != nil || len(fields) != 1 ||
json.Unmarshal(fields["trip_id"], &tripID) != nil || tripID == "" {
writeError(w, http.StatusBadRequest, "provide a trip_id to export")
return false
if json.Unmarshal(body, &fields) == nil && len(fields) == 1 &&
json.Unmarshal(fields["trip_id"], &tripID) == nil && 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)
}
body, _ = json.Marshal(map[string]string{"trip_id": tripID})
r.Body = io.NopCloser(bytes.NewReader(body))
r.ContentLength = int64(len(body))
r.TransferEncoding = nil
r.Trailer = nil
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) {
+3 -3
View File
@@ -38,13 +38,13 @@ func (g *apiGuard) rememberExport(response *http.Response) error {
response.Body = io.NopCloser(bytes.NewReader(body))
var result struct {
TripID string `json:"trip_id"`
Slot *uint64 `json:"note_slot"`
Generation uint64 `json:"note_generation"`
Size int `json:"note_size"`
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 {
return nil
}
@@ -67,7 +67,7 @@ func (g *apiGuard) rememberExport(response *http.Response) error {
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
}