From 961513d2c1780d960598a3016272283b213a401d Mon Sep 17 00:00:00 2001 From: KDreynolds Date: Sat, 6 May 2023 23:45:05 -0600 Subject: [PATCH] added typechecking function --- context.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/context.go b/context.go index 5716318e..1c44f9de 100644 --- a/context.go +++ b/context.go @@ -962,6 +962,16 @@ func (c *Context) JSONP(code int, obj any) { c.Render(code, render.JSON{Data: obj}) return } + + // Add type checking for the callback function name + callbackPattern := `^[\p{L}\p{N}_]+$` // Unicode-aware pattern for alphanumeric characters and underscores + isValidCallback := regexp.MustCompile(callbackPattern).MatchString(callback) + if !isValidCallback { + // Handle the invalid callback function name, e.g., return an error or set a default callback function name + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid callback function name"}) + return + } + c.Render(code, render.JsonpJSON{Callback: callback, Data: obj}) }