`NIOHTTPServer` does not enforce response body framing against `Content-Length`
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
NIOHTTPServer.ResponseSender.Writer のエントリポイントである write(buffer:) と finish(trailer:) から始め、Content-Length を 2 に設定した ShortBodyHandler の再現コードを使用してください。issue で言及されている既存の Vapor テストを確認し、バイトを書き込みすぎた場合や早すぎるタイミングで完了した場合に、レスポンスが完了するのではなくエラーが発生して中断されることを検証してください。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Swift
- スター
- 53
- フォーク
- 16
- 平均マージ
- 3日 14時間
- マージ済み PR(30日)
- 11
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
swift-server/swift-http-server のほかの issue
-
Expose the negotiated HTTP version to request handlers対応中かも @dioKaratzas が 47 日前に担当しました。 オープンgood first issue kind/enhancement size/S
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
swift-server/swift-http-server#113 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
swift-server/swift-http-server#137 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
swift-server/swift-http-server#131 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Offer ResponseSender.Writer RawSpan APIs対応中かも @0xTim が 49 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 68/100
swift-server/swift-http-server#123 ·
メンテナーはふだん 1 日以内に返信
-
kind/feature
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
swift-server/swift-http-server#122 ·
メンテナーはふだん 1 日以内に返信
swift-server/swift-http-server の issue をすべて見る
似ている issue
-
builtin-port enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
cmlarsen/writemorebetter#235 ·
メンテナーはふだん 1 日以内に返信
-
bug-pending-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
NativeScript/NativeScript#11525 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
callstack/agent-device#3627 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 1 日以内に返信