Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

`NIOHTTPServer` does not enforce response body framing against `Content-Length`

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

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
swift
Área
backend

Línea de trabajo

Start at the NIOHTTPServer.ResponseSender.Writer entry points write(buffer:) and finish(trailer:), using the ShortBodyHandler reproduction with Content-Length set to 2. Check the existing Vapor tests mentioned in the issue and verify that writing too many bytes or finishing too early raises an error and aborts the response instead of completing it.

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

Descripción

kind/bug size/M status/needs-design

This was picked up by some tests in Vapor. ResponseSender.Writer accepts any number of body bytes regardless of the Content-Length the handler declared on the response. finish(buffer:finalElement:) just completes the response without checking, resulting in the potential for under-writing and over-writing with no errors raised in the handler. Under-writing is annoying, over-writing is problem with keep-alive connections as it was treat the additional data as the start of the next response.

Repro
struct ShortBodyHandler: HTTPServerRequestHandler {
    typealias RequestContext = NIOHTTPServer.RequestContext
    typealias Reader = NIOHTTPServer.Reader
    typealias ResponseSender = NIOHTTPServer.ResponseSender

    static let payload = "a"   // or "abcdefgh"

    func handle(
        request: HTTPRequest,
        requestContext: consuming NIOHTTPServer.RequestContext,
        reader: consuming sending NIOHTTPServer.Reader,
        responseSender: consuming sending NIOHTTPServer.ResponseSender
    ) async throws {
        var fields = HTTPFields()
        // Limit the content length to 2 which we'd expect to cause issues with over or under writing.
        fields[.contentLength] = "2"
        var writer = try await responseSender.send(HTTPResponse(status: .ok, headerFields: fields))
        var body = UniqueArray<UInt8>()
        body.append(copying: Array(Self.payload.utf8)[...])
        try await writer.write(buffer: &body)
        try await writer.finish(trailer: nil) // ...and declare the body complete
    }
}
Actual

Both responses return successfully with no error:

payload = "a"         ->  "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\na"
payload = "abcdefgh"  ->  "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nabcdefgh"

In the over-write case six bytes follow the declared body.

Expected

write(buffer:) should fail once the accumulated body would exceed the declared Content-Length and finish(buffer:finalElement:) should fail if fewer bytes were written than expected, aborting the response rather than completing it

Impact

In Vapor (where Content-Length is set but the caller on streaming response bodies) an under-writing handler produced a response that AsyncHTTPClient timed out on:

threw after 30.008566208 seconds: stream ended at an unexpected time

This is correct but should error way quicker. I added a guard in Vapor so this fails pretty instantly but it feels like this should be the server's responsibility rather than every implementation having to implement it. There's no workaround for the over-write case

Lenguaje dominante
Swift
Estrellas
53
Forks
16
Merge medio
3 d 14 h
PR fusionados (30 d)
11

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.

Más de swift-server/swift-http-server

Todos los issues de swift-server/swift-http-server

Issues similares

Más issues de Swift

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.