fix: project inline binary metadata and protect private markup attributes
This commit is contained in:
@@ -2,11 +2,14 @@ package logging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"git.ipao.vip/rogee/go-sip/internal/media"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -61,6 +64,72 @@ func TestContentFieldClassificationAndEveryLoggingPathAgree(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInlineWAVDataURIsOnlyExposeTypeAndLength(t *testing.T) {
|
||||
pcm := make([]byte, 1600)
|
||||
for i := range pcm {
|
||||
pcm[i] = byte(i%239 + 1)
|
||||
}
|
||||
wav, _, err := media.EncodeMonoWAV(pcm, 2048)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
encoded := base64.StdEncoding.EncodeToString(wav)
|
||||
escaped := url.PathEscape(string(wav))
|
||||
for _, uri := range []string{"data:audio/wav;base64," + encoded, "DATA:AUDIO/WAV;BASE64," + encoded, "data:audio/wav;codec=pcm;base64," + encoded, "data:audio/wav," + escaped} {
|
||||
for _, debug := range []bool{false, true} {
|
||||
out := installTestLogger(t, debug)
|
||||
for _, kind := range []string{"text/html", "text/plain", "application/json", "application/x-www-form-urlencoded"} {
|
||||
body := uri
|
||||
switch kind {
|
||||
case "text/html":
|
||||
body = `<audio src="` + uri + `" controls></audio><source src="` + uri + `"/><task_id>4</task_id><status>ready</status>`
|
||||
case "application/json":
|
||||
body = `{"resource":"` + uri + `","task_id":4,"status":"ready"}`
|
||||
case "application/x-www-form-urlencoded":
|
||||
body = "resource=" + url.QueryEscape(uri) + "&task_id=4&status=ready"
|
||||
}
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
got, err := io.ReadAll(r.Body)
|
||||
if err != nil || string(got) != body {
|
||||
t.Error("wire request changed", err)
|
||||
}
|
||||
w.Header().Set("Content-Type", kind)
|
||||
_, _ = io.WriteString(w, body)
|
||||
}))
|
||||
req, err := http.NewRequest(http.MethodPost, ts.URL, strings.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", kind)
|
||||
response, err := HTTPClient(ts.Client(), "test.inline_audio", "upstream").Do(req)
|
||||
if err != nil {
|
||||
ts.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := io.ReadAll(response.Body)
|
||||
_ = response.Body.Close()
|
||||
ts.Close()
|
||||
if err != nil || string(got) != body {
|
||||
t.Fatal("wire response changed", err)
|
||||
}
|
||||
slog.Error("audio diagnostic", "error", errors.New(body), "detail", body)
|
||||
slog.Error(body)
|
||||
}
|
||||
text := out.String()
|
||||
for _, secret := range []string{encoded, escaped, url.QueryEscape(encoded), url.QueryEscape(escaped)} {
|
||||
if strings.Contains(text, secret) {
|
||||
t.Fatalf("audio leaked, debug=%v", debug)
|
||||
}
|
||||
}
|
||||
for _, keep := range []string{"audio/wav", "1644", "task_id", "ready"} {
|
||||
if !strings.Contains(text, keep) {
|
||||
t.Fatal("metadata or diagnostic lost", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestURLSchemeCaseCannotExposeSignedPartsOnAnyLoggingPath(t *testing.T) {
|
||||
for _, scheme := range []string{"http", "HTTP", "HtTp", "https", "HTTPS", "hTtPs", "ws", "WS", "wS", "wss", "WSS", "wSs"} {
|
||||
signed := scheme + "://storage.example/audio.wav?sv=2025-01-05&se=2026-10-10&sig=private-signature-value"
|
||||
|
||||
Reference in New Issue
Block a user