Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3577 sec fix #3597

Closed
wants to merge 2 commits into from
Closed

3577 sec fix #3597

wants to merge 2 commits into from

Conversation

KDreynolds
Copy link

Added type checking to JSONP for security reasons.
Added mention in documentation.
Added test function.

@KDreynolds
Copy link
Author

Let me know if this PR needs more work.

// Add type checking for the callback function name
// Use a Unicode-aware pattern for alphanumeric characters and underscores
callbackPattern := `^[\p{L}\p{N}_]+$`
isValidCallback := regexp.MustCompile(callbackPattern).MatchString(callback)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should move the callbackPattern and the regex MustCompile in a package level variable to avoid the cost of rebuilding it everytime.

jsonString, _ := json.Marshal(anyData)

// Respond with a JavaScript callback function call that includes the JSON data
c.Render(code, render.String{Format: "/**/ typeof " + callback + " === 'function' && " + callback + "(%s);", Data: []interface{}{string(jsonString)}})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The render logic should probably be moved to the render package by leveraging the JsonpJSON struct (in particular https://github.com/gin-gonic/gin/blob/master/render/json.go#LL116C13-L116C13).

@KDreynolds
Copy link
Author

outdated.

@KDreynolds KDreynolds closed this Mar 4, 2024
@KDreynolds KDreynolds deleted the 3577-sec-fix branch March 4, 2024 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants