wasihttp: P2 Transport sends an empty authority on redirects (uses req.Host, not req.URL.Host)

Abierto
#5 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go

Línea de trabajo

Read wasihttp/roundtripper_p2.go around the authority and path construction, then compare it with wasihttp/roundtripper_p3.go and the redirect request construction in net/http/client.go cited in the issue. Check how Go represents the URL host, request URI, and custom Host value. Done means redirects send the URL authority and preserve the encoded path and query, while a differing Host value returns a clear error.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

The P2 Transport in wasihttp sets the outgoing request's authority from req.Host. net/http leaves Host empty on the request it builds for a redirect, so every redirect goes out with an empty authority and fails. On Spin 4.3.0 the second hop comes back as HTTP-request-URI-invalid:

GET http://127.0.0.1:18081/final      -> 200
GET http://127.0.0.1:18081/redirect   (302 to /final)
  ERR: Get "http://127.0.0.1:18081/final": failed to acquire handle to outbound request: {19 <nil>}

The origin never sees the second request. The first hop works only because http.NewRequestWithContext copies u.Host into req.Host (net/http/request.go:931). The redirect request is built in client.go (~655-683) as &Request{..., URL: u, Host: host}, where host is "" unless the caller set a custom Host and the Location is relative.

The relevant lines are in wasihttp/roundtripper_p2.go (same in v0.1.2 and main at d97e278):

outRequest.SetAuthority(witTypes.Some(incomingRequest.Host))      // :60
pathWithQuery := incomingRequest.URL.Path                          // :63
if incomingRequest.URL.RawQuery != "" {
	pathWithQuery = pathWithQuery + "?" + incomingRequest.URL.Query().Encode()  // :65
}

Two smaller problems on the same lines:

  • URL.Path is the decoded path, so /a%2Fb is sent as /a/b.
  • URL.Query().Encode() re-sorts and re-encodes the query (?z=1&a=2 goes out as ?a=2&z=1), which breaks signed URLs.

When req.Host is set to something other than URL.Host, it becomes the authority. The request is then checked against the host's allow-list, and connected, as that other name, not the host in the URL. net/http would send it as a Host header instead, but Host is a forbidden header on wasi:http (setting it fails with header-error 1). So I don't think there's a correct way to honour a differing req.Host here.

The P3 roundtripper already gets the path right (req.URL.RequestURI()), and falls back to URL.Host when Host is empty (roundtripper_p3.go:129-134). It still lets a non-empty req.Host override the authority, though.

Suggested fix (P2, and the precedence in P3):

  • take the authority from req.URL.Host, and return an error if it's empty;
  • return an error if req.Host is set and differs from req.URL.Host (case-insensitive);
  • use req.URL.RequestURI() for the path and query;
  • keep leaving redirects to http.Client, so the host re-checks every hop.

We're carrying exactly this in a vendored copy for a Go sandbox on Spin. A same-host redirect now returns 200, %2F and the query arrive unchanged, and a Host mismatch gives a clear error: https://github.com/mastersingh24/spin-code-sandbox/blob/8d85b45a5a9bb8659e282d616b9c4e49f67dac52/experiments/go-sandbox/guest/wasihttpc/roundtripper_p2.go

Happy to send a PR if this direction looks right.

Repro setup: Spin 4.3.0, go1.27.4, componentize-go 0.5.4, unmodified go.bytecodealliance.org/wasi v0.1.2, allowed_outbound_hosts = ["http://127.0.0.1:18081"], and a local origin where /redirect returns 302 Location: /final.

Lenguaje dominante
Go
Estrellas
2
Forks
3
Merge medio
14 h 12 min
PR fusionados (30 d)
2

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.