Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[Bug]: request-rewrite ReplacePrefixMatch treats "/api" as a prefix of "/api2/...", and drops a slash when the operation path ends with "/"

Aperta Adatta ai principianti
#3,910 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

L’issue indica policies/request-rewrite/requestrewrite.go e fornisce due test di regressione per applyPathRewrite. Inizia leggendo quella funzione ed esegui il comando go test indicato in policies/request-rewrite. Il lavoro è completato quando /api lascia /api2/users invariato e /api/ riscrive /api/users in /v2/users; aggiungi test per entrambi i casi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Severity/Major
Description

applyPathRewrite for ReplacePrefixMatch (wso2/gateway-controllers, policies/request-rewrite/requestrewrite.go) decides whether to rewrite with a raw string prefix check:

if !strings.HasPrefix(currentPath, operationPath) {
    return currentPath
}
remainder := strings.TrimPrefix(currentPath, operationPath)
return cfg.ReplacePrefixMatch + remainder

That is not path-segment aware. Measured failures on main (4cf9570):

operationPath currentPath ReplacePrefixMatch Actual result Expected
/api /api2/users /v2 /v22/users unchanged /api2/users
/api /api/users /v2 /v2/users /v2/users (OK)
/api/ /api/users /v2 /v2users /v2/users

So a short resource prefix silently rewrites longer sibling paths (/api vs /api2), and an operation path that ends with / concatenates without keeping a path separator. Envoy and most gateways treat prefix rewrite as a path-prefix match that ends on a / or end-of-path boundary.

Steps to Reproduce

Under policies/request-rewrite on 4cf9570:

func TestReplacePrefixMatchOverlappingPath(t *testing.T) {
	cfg := &pathRewrite{Type: "REPLACEPREFIXMATCH", ReplacePrefixMatch: "/v2"}
	got := applyPathRewrite("/api", "/api2/users", cfg)
	if got != "/api2/users" {
		t.Fatalf("prefix /api must not match /api2/users; got %q", got)
	}
}

func TestReplacePrefixMatchTrailingSlashOnOperation(t *testing.T) {
	cfg := &pathRewrite{Type: "REPLACEPREFIXMATCH", ReplacePrefixMatch: "/v2"}
	got := applyPathRewrite("/api/", "/api/users", cfg)
	if got != "/v2/users" {
		t.Fatalf("got %q want /v2/users", got)
	}
}

go test -run 'TestReplacePrefixMatchOverlapping|TestReplacePrefixMatchTrailing' -v .

Severity Level of the Issue

Severity/Major (Important functionality is broken. Should be prioritized. Doesn't need immediate attention)

Environment Details (with versions)
  • wso2/gateway-controllers main 4cf9570 (request-rewrite policy; sdk/core as in that module's go.mod)
  • Go 1.26.x, Linux x86_64
Related Issues

#1297 covers invalid regex / unsupported type silent no-op and query reordering. It does not mention prefix-boundary or trailing-slash concatenation. No other open issue found for ReplacePrefixMatch overlapping paths.

Possible Solution
  1. Match only when currentPath == operationPath or currentPath continues with / after the operation prefix (after normalizing a trailing / on the operation path).
  2. When joining ReplacePrefixMatch and the remainder, ensure exactly one / between them.
  3. Add the two regression tests above.
op := strings.TrimSuffix(operationPath, "/")
if currentPath != op && !strings.HasPrefix(currentPath, op+"/") {
    return currentPath
}
remainder := strings.TrimPrefix(currentPath, op)
repl := strings.TrimSuffix(cfg.ReplacePrefixMatch, "/")
if remainder == "" {
    return repl
}
return repl + remainder

I'm happy to send a PR.

Lingua principale
Go
Stelle
79
Fork
130
Merge medio
2g 51m
PR unite (30g)
173

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di wso2/api-platform

Tutte le issue di wso2/api-platform

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.