fix(audit): preserve complete native message data in session HTML

This commit is contained in:
2026-10-20 20:45:56 +01:00
parent cb57da5dfc
commit d7e99a19d6
2 changed files with 28 additions and 0 deletions
+6
View File
@@ -234,6 +234,12 @@ func renderHTMLMessage(out *bytes.Buffer, index int, message opencode.ExportedMe
for _, part := range message.Parts {
renderHTMLPart(out, part)
}
// The readable projection is not the complete native record: tools can
// carry inline images and provider-specific metadata absent from ChatPart.
// Retain the exact stored message as escaped data, never executable markup.
if len(message.Raw) > 0 {
fmt.Fprintf(out, "<details class=\"session-native-record\"><summary>Complete native message (JSON)</summary><pre>%s</pre></details>\n", html.EscapeString(string(message.Raw)))
}
}
// renderHTMLPart renders one content element, escaping all session text.
+22
View File
@@ -61,6 +61,28 @@ func sessionBody() string {
`{"type":"file","uri":"file:///tmp/x"}]}]}`
}
func TestExportSessionRetainsNativeToolImagesAndUnknownMetadata(t *testing.T) {
dir := t.TempDir()
body := `{"info":{"id":"ses_ok"},"messages":[{"id":"m1","type":"user","content":[{"type":"text","text":"inspect screenshot"}]},{"id":"m2","type":"assistant","providerState":{"custom":"preserved"},"content":[{"type":"tool","name":"read","state":{"status":"completed","content":[{"type":"image","mimeType":"image/png","data":"iVBORfixturebytes"}]}}]}]}`
sessionCLI(t, "ses_ok", body)
result, err := ExportSession(context.Background(), sessionRequest(dir))
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(filepath.Join(dir, result.HTML))
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"session-native-record", "iVBORfixturebytes", "providerState", "preserved"} {
if !strings.Contains(string(data), want) {
t.Fatalf("native evidence dropped %q", want)
}
}
if result.Messages != 2 || result.Truncated {
t.Fatalf("incomplete export: %+v", result)
}
}
func sessionRequest(dir string) SessionExportRequest {
return SessionExportRequest{
SessionID: "ses_ok", Backend: "v2", Directory: dir, ArtifactsDir: dir,