0

I am able to inject javascript:alert `1` or javascript:alert(1) into the href field of the rails app and it is getting executed as JavaScript. Html encoding is already implemented but it doesnt encode brackets and back quotes.

How to solve this? What is the best method to prevent XSS in rails?

2

You should always validate user input.

For example, since you're expecting an URL, you could check that the submitted string starts with http:// or https://.

submitted_url.start_with?('http://', 'https://')
  • That sounds like a hack right? Is there a module in Rails to output encode these kind of strings? – Anonymous Platypus Jul 31 at 9:28
  • 1
    @AnonymousPlatypus: I'm not a Ruby developer so I'm not sure if there's a better way to do this in Ruby / Rails, but the main idea still stands: you need to validate that the submitted URL is a valid URL! Encoding the output isn't enough. – Benoit Esnard Jul 31 at 10:10
  • The fix is deployed as you have mentioned. Thanks :) – Anonymous Platypus Jul 31 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.