Rename and clarify the `body.collect(upTo:)` API which gets used in an insecure way all the time
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 25/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- swift
- Ambito
- backend-api-design
Direzione di ricerca
Inizia individuando l’API body.collect(upTo:) e la relativa documentazione, quindi esamina i punti in cui viene attualmente chiamata e i test esistenti. Usa gli obiettivi e gli esempi dell’issue per definire un’API a memoria limitata facilmente individuabile, un percorso unsafe esplicito e una documentazione che spieghi quando è preferibile lo streaming; il lavoro è completo quando il design è concordato e coperto dai test.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
We see code similar to this a lot
let expectedBytes = response.headers.first(name: "content-length").flatMap(Int.init) ?? 1024 * 1024
return Response(status: response.status, buffer: try await response.body.collect(upTo: expectedBytes))
The above code doesn't make any sense because it essentially says
Read as many bytes as you want into memory
This of course can lead to very bad scenarios because the content-length is controlled by the attacker. Furthermore, it's a lot of code to spell the ~equivalent of
return Response(status: response.status, buffer: try await response.body.collect(upTo: Int.max))
And just to be clear, we do not have to check content-length here. The content length is correct, or else NIO would've spotted that and sent an error instead (like illegal EOF condition if it ended prematurely or so).
But let's get to fixing this:
Goals:
- Reasonable (fixed-sized limit) code should look good and be discoverable without reading much documentation
- Unreasonable code should be possible but awkward using some word that conveys the danger of what's happening
- Not overcomplicated
Ideas:
- Put a reasonable default limit (e.g. 4MB) on there. So
body.collect()would be safe - Make it clear in the docs that streaming should always be preferred. For example to save something to disk or proxy it,
collect(...)should not be used. Only for JSON/protobuf/image/... decoding that needs to happen in memory we should use it - Instead of
Intuse a new type that can be constructed with integer literals or (and thanks to @hamzahrmalik for the idea) using some.init(watchOutWhatYourAreDoingIsDangerous: Int)
Maybe
body.collectIntoMemory()-- safe: up to 4 MBbody.collectIntoMemory(maximumSize: .megabytes(16))-- safe: up to 16 MBbody.collectIntoMemory(maximumSize: .bytes(contentLength))-- compiler error: expected typeBodySize, actual typeIntbody.collectIntoMemory(maximumSize: .bytes(BodySize(dangerousCalculatedSizeInBytes: contentLength))-- unsafe but compiles
- Lingua principale
- Swift
- Stelle
- 1.1k
- Fork
- 156
- Merge medio
- 1h 54m
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di swift-server/async-http-client
-
RequestBag keeps the request body after an early response with status >= 300Forse già presa @beshkenadze l’ha presa oggi. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
swift-server/async-http-client#941 ·
I maintainer di solito rispondono entro 1 giorno
-
HTTP/2 stream stays half-closed after an early response with status >= 300Forse già presa @beshkenadze l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
swift-server/async-http-client#940 ·
I maintainer di solito rispondono entro 1 giorno
-
Transaction keeps the request body after an early response with status >= 300Forse già presa @beshkenadze l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
swift-server/async-http-client#939 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
swift-server/async-http-client#938 ·
I maintainer di solito rispondono entro 1 giorno
-
Invalid state: idleAperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
swift-server/async-http-client#927 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di swift-server/async-http-client
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
foxglove/mcap#1907 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue help wanted translation
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
I maintainer di solito rispondono entro 1 giorno
-
TestURLRequest.test_methodNormalization fails on macOS 27Forse già presa @dfabulich l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
skiptools/skip-foundation#139 ·
-
accessibility help wanted macos
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
dorlugasigal/annotator#42 ·
I maintainer di solito rispondono entro 1 giorno