fix: honor advisory message size contract
CI / test (push) Successful in 11s
CI / test (pull_request) Successful in 11s

This commit is contained in:
m
2026-08-16 20:22:27 +03:00
parent d0255558e2
commit 59f5b4ce57
4 changed files with 17 additions and 9 deletions
+4 -3
View File
@@ -18,8 +18,9 @@ import (
)
const (
maxRequestBytes = 8 << 10
maxResponseBytes = 1 << 20
maxRequestBytes = 8 << 10
maxAdvisoryRequestBytes = 20 << 10
maxResponseBytes = 1 << 20
)
//go:embed static
@@ -113,7 +114,7 @@ type browserAdvisoryRequest struct {
func (g *gateway) handleAdvisory(w http.ResponseWriter, r *http.Request) {
var browserRequest browserAdvisoryRequest
if err := decodeStrictJSON(r.Body, maxRequestBytes, &browserRequest); err != nil || browserRequest.Message == "" || len(browserRequest.Message) > 16*1024 || !validID(browserRequest.SessionID) || !validID(browserRequest.WorkspaceID) {
if err := decodeStrictJSON(r.Body, maxAdvisoryRequestBytes, &browserRequest); err != nil || browserRequest.Message == "" || len(browserRequest.Message) > 16*1024 || !validID(browserRequest.SessionID) || !validID(browserRequest.WorkspaceID) {
http.Error(w, "invalid advisory request", http.StatusBadRequest)
return
}
+10 -3
View File
@@ -8,6 +8,8 @@ import (
"net/http/httptest"
"strings"
"testing"
"git.cin.su/m/Assistant/contracts/cognitive"
)
func TestGatewayForwardsOnlyCreatorAllowlist(t *testing.T) {
@@ -251,6 +253,7 @@ func TestAdvisoryRequiresSessionBeforeUpstreamCalls(t *testing.T) {
}
func TestAdvisoryAssemblesTrustedIdentityAndScope(t *testing.T) {
longMessage := strings.Repeat("a", 16*1024)
core := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer bounded-session" {
t.Fatal("missing bounded session")
@@ -272,8 +275,12 @@ func TestAdvisoryAssemblesTrustedIdentityAndScope(t *testing.T) {
cognitive := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
text := string(body)
if !strings.Contains(text, `"ActorID":"creator-actor"`) || !strings.Contains(text, `"PrincipalID":"creator-principal"`) || !strings.Contains(text, `"ScopeID":"trusted-scope"`) || !strings.Contains(text, `"Message":"help"`) || strings.Contains(text, "attacker") {
t.Fatalf("untrusted cognitive request: %s", text)
var trusted cognitive.AdvisoryRequest
if err := json.Unmarshal(body, &trusted); err != nil {
t.Fatal(err)
}
if trusted.ActorID != "creator-actor" || trusted.PrincipalID != "creator-principal" || trusted.ScopeID != "trusted-scope" || trusted.Message != longMessage || strings.Contains(text, "attacker") {
t.Fatal("cognitive request was not assembled from trusted context")
}
var request map[string]any
_ = json.Unmarshal(body, &request)
@@ -284,7 +291,7 @@ func TestAdvisoryAssemblesTrustedIdentityAndScope(t *testing.T) {
if err != nil {
t.Fatal(err)
}
request := httptest.NewRequest(http.MethodPost, "/api/advisory", strings.NewReader(`{"message":"help","session_id":"focus-session","workspace_id":"home"}`))
request := httptest.NewRequest(http.MethodPost, "/api/advisory", strings.NewReader(fmt.Sprintf(`{"message":%q,"session_id":"focus-session","workspace_id":"home"}`, longMessage)))
request.Header.Set("Authorization", "Bearer bounded-session")
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()