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

Add JS string escape helper #847

Open
scottwhudson opened this issue Feb 8, 2025 · 3 comments
Open

Add JS string escape helper #847

scottwhudson opened this issue Feb 8, 2025 · 3 comments
Assignees

Comments

@scottwhudson
Copy link

scottwhudson commented Feb 8, 2025

Hey Joel 👋,

Thanks for the Phlex library, it feels like what HAML and view components could've been.

We're using it alongside alpine.js and overall very happy with the experience. One issue we're running into is the need to escape strings prior to JS evaluation and our current approach feels slightly cumbersome. I'd love to open a PR to add a JS string escape helper that leverages json_escape and raw under the hood to ensure that we're not exposing ourselves to XSS attacks.

Are you open to a contribution like this?

@joeldrapper
Copy link
Collaborator

Hey, that sounds like a good idea. Do you have an example of the approach you have in mind? Not the implementation but the interface. What would it look like with alpine?

@joeldrapper
Copy link
Collaborator

I don’t think we can find automatically escape JSON strings like we can with HTML, so I guess the interface would be just adding a json_escape helper.

def json_escape(string)
  ERB::Util.json_escape(string)
end

@joeldrapper joeldrapper added this to the 2.1 milestone Feb 12, 2025
@scottwhudson
Copy link
Author

This is my feeling as well. We don't have a particularly elegant way of dynamically applying the JSON escape logic in the templates automagically without tying the implementation logic to the alpine-specific data attributes. I'll whip up a PR for this.

@joeldrapper joeldrapper removed this from the 2.1 milestone Feb 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants