Skip to content

Commit a0f04e7

Browse files
Fix: Copilot suggested fixes
- handler.go: MeConsentApprove now routes ErrUpstreamResponseTooLarge through writeProxyError, so it returns the proper 502 UPSTREAM_RESPONSE_TOO_LARGE instead of 400 INVALID_PAYLOAD. - service.go: proxy header filtering now strips Cookie and Authorization.
1 parent a8efad3 commit a0f04e7

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

portal/backend/internal/proxy/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (h *Handler) MeConsentApprove(w http.ResponseWriter, r *http.Request) {
267267

268268
payload, trustedClientID, err := h.buildApprovalUpdatePayload(r, baseResp.Body, selections, userID)
269269
if err != nil {
270-
if errors.Is(err, ErrUpstreamTimeout) || errors.Is(err, ErrUpstreamUnavailable) {
270+
if errors.Is(err, ErrUpstreamTimeout) || errors.Is(err, ErrUpstreamUnavailable) || errors.Is(err, ErrUpstreamResponseTooLarge) {
271271
h.writeProxyError(w, err)
272272
return
273273
}

portal/backend/internal/proxy/service.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ func (s *Service) skipHeader(name string, connectionHeaders map[string]struct{})
321321
if strings.EqualFold(canonical, "Org-Id") || strings.EqualFold(canonical, "TPP-Client-Id") {
322322
return true
323323
}
324+
if strings.EqualFold(canonical, "Cookie") || strings.EqualFold(canonical, "Authorization") {
325+
return true
326+
}
324327
if isForwardingHeader(canonical) {
325328
return true
326329
}

portal/backend/tests/unit/proxy_service_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,39 @@ func TestForwardStripsClientForwardingHeaders(t *testing.T) {
315315
}
316316
}
317317

318+
func TestForwardStripsClientCredentialHeaders(t *testing.T) {
319+
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
320+
if got := r.Header.Get("Cookie"); got != "" {
321+
t.Fatalf("expected Cookie to be stripped, got %q", got)
322+
}
323+
if got := r.Header.Get("Authorization"); got != "" {
324+
t.Fatalf("expected Authorization to be stripped, got %q", got)
325+
}
326+
w.WriteHeader(http.StatusOK)
327+
}))
328+
defer upstream.Close()
329+
330+
svc, err := proxy.NewService(config.ProxyConfig{
331+
OpenFGCAPIURL: upstream.URL,
332+
OpenFGCAPITimeout: 2 * time.Second,
333+
MaxRequestBytes: 1024,
334+
MaxResponseBytes: 1024,
335+
AllowedPassthrough: []string{"GET"},
336+
})
337+
if err != nil {
338+
t.Fatalf("failed to construct service: %v", err)
339+
}
340+
341+
req := httptest.NewRequest(http.MethodGet, "http://bff.local/api/consents", nil)
342+
req.Header.Set("Cookie", "sid=secret")
343+
req.Header.Set("Authorization", "Bearer secret")
344+
345+
rr := httptest.NewRecorder()
346+
if err := svc.Forward(rr, req, http.MethodGet, "/api/v1/consents", nil, nil); err != nil {
347+
t.Fatalf("unexpected forward error: %v", err)
348+
}
349+
}
350+
318351
func TestForwardGeneratesCorrelationIDWhenMissing(t *testing.T) {
319352
var gotCorrelationID string
320353
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)