@@ -4,6 +4,7 @@ export interface Env {
44
55// Restrict this to your official deployment origin
66const ALLOWED_ORIGIN = "https://salamlang.workers.dev" ;
7+ const MAX_PAYLOAD_SIZE = 5 * 1024 * 1024 ; // 5MB
78
89export default {
910 async fetch (
@@ -59,7 +60,7 @@ export default {
5960 // SECURITY: Protect edge runtime memory limits from infinite stream attacks (5MB Threshold)
6061 const contentLengthHeader = request . headers . get ( "content-length" ) ;
6162 const contentLength = contentLengthHeader ? parseInt ( contentLengthHeader , 10 ) : 0 ;
62- if ( isNaN ( contentLength ) || contentLength > 5 * 1024 * 1024 ) {
63+ if ( isNaN ( contentLength ) || contentLength < 0 || contentLength > MAX_PAYLOAD_SIZE ) {
6364 return new Response ( "Payload Too Large" , {
6465 status : 413 ,
6566 headers : getCorsHeaders ( ) ,
@@ -80,16 +81,15 @@ export default {
8081 const decoder = new TextDecoder ( "utf-8" ) ;
8182 const encoder = new TextEncoder ( ) ;
8283
83- const MAX_PAYLOAD_SIZE = 5 * 1024 * 1024 ;
8484 let totalBytes = 0 ;
8585
8686 const { readable, writable } = new TransformStream ( {
8787 transform ( chunk : Uint8Array , controller ) {
88- totalBytes += chunk . byteLength ;
89- if ( totalBytes > MAX_PAYLOAD_SIZE ) {
88+ if ( totalBytes + chunk . byteLength > MAX_PAYLOAD_SIZE ) {
9089 controller . error ( new Error ( "Payload Too Large" ) ) ;
9190 return ;
9291 }
92+ totalBytes += chunk . byteLength ;
9393 // stream: true stores fragments of split multi-byte UTF-8 sequences safely until the next chunk arrives
9494 const text = decoder . decode ( chunk , { stream : true } ) ;
9595
0 commit comments