@@ -13,7 +13,12 @@ import (
1313 "time"
1414)
1515
16- const randomIDAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
16+ const (
17+ randomIDAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
18+ maxJSONPayloadBytes = 1 << 20
19+ maxRegexInputBytes = 1 << 20
20+ maxRegexPatternSize = 16 << 10
21+ )
1722
1823func builtinAssert (exec * Execution , receiver Value , args []Value , kwargs map [string ]Value , block Value ) (Value , error ) {
1924 if len (args ) == 0 {
@@ -225,7 +230,12 @@ func builtinJSONParse(exec *Execution, receiver Value, args []Value, kwargs map[
225230 return NewNil (), fmt .Errorf ("JSON.parse does not accept blocks" )
226231 }
227232
228- decoder := json .NewDecoder (strings .NewReader (args [0 ].String ()))
233+ raw := args [0 ].String ()
234+ if len (raw ) > maxJSONPayloadBytes {
235+ return NewNil (), fmt .Errorf ("JSON.parse input exceeds limit %d bytes" , maxJSONPayloadBytes )
236+ }
237+
238+ decoder := json .NewDecoder (strings .NewReader (raw ))
229239 decoder .UseNumber ()
230240
231241 var decoded any
@@ -267,6 +277,9 @@ func builtinJSONStringify(exec *Execution, receiver Value, args []Value, kwargs
267277 if err != nil {
268278 return NewNil (), fmt .Errorf ("JSON.stringify failed: %v" , err )
269279 }
280+ if len (payload ) > maxJSONPayloadBytes {
281+ return NewNil (), fmt .Errorf ("JSON.stringify output exceeds limit %d bytes" , maxJSONPayloadBytes )
282+ }
270283 return NewString (string (payload )), nil
271284}
272285
@@ -384,12 +397,20 @@ func builtinRegexMatch(exec *Execution, receiver Value, args []Value, kwargs map
384397 if args [0 ].Kind () != KindString || args [1 ].Kind () != KindString {
385398 return NewNil (), fmt .Errorf ("Regex.match expects string pattern and text" )
386399 }
400+ pattern := args [0 ].String ()
401+ text := args [1 ].String ()
402+ if len (pattern ) > maxRegexPatternSize {
403+ return NewNil (), fmt .Errorf ("Regex.match pattern exceeds limit %d bytes" , maxRegexPatternSize )
404+ }
405+ if len (text ) > maxRegexInputBytes {
406+ return NewNil (), fmt .Errorf ("Regex.match text exceeds limit %d bytes" , maxRegexInputBytes )
407+ }
387408
388- re , err := regexp .Compile (args [ 0 ]. String () )
409+ re , err := regexp .Compile (pattern )
389410 if err != nil {
390411 return NewNil (), fmt .Errorf ("Regex.match invalid regex: %v" , err )
391412 }
392- match := re .FindString (args [ 1 ]. String () )
413+ match := re .FindString (text )
393414 if match == "" {
394415 return NewNil (), nil
395416 }
@@ -426,6 +447,12 @@ func builtinRegexReplaceInternal(args []Value, kwargs map[string]Value, block Va
426447 text := args [0 ].String ()
427448 pattern := args [1 ].String ()
428449 replacement := args [2 ].String ()
450+ if len (pattern ) > maxRegexPatternSize {
451+ return NewNil (), fmt .Errorf ("%s pattern exceeds limit %d bytes" , method , maxRegexPatternSize )
452+ }
453+ if len (text ) > maxRegexInputBytes {
454+ return NewNil (), fmt .Errorf ("%s text exceeds limit %d bytes" , method , maxRegexInputBytes )
455+ }
429456
430457 re , err := regexp .Compile (pattern )
431458 if err != nil {
0 commit comments