I need to allow a user to pass unsafe text into an iframe URL:
<iframe src="https://example.com?foo=INPUT"></iframe>
Inside the iFrame render code, I have this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
window.myVar = '<%= cleaned %>';
</script>
</body>
</html>
I'm using ruby, but that's not specific to the question.
The important parts are:
I am explicitly defining the
'
single quotes within the iFrame HTML myself. The ERB<%= %>
execution context is within the quotes.The only cleaning I am doing in ruby is the following regex:
cleaned = value.gsub(/[`'"\\]+/, '')
I realize having double quotes and backticks in there is also not needed, but I wanted that false feeling of extra comfort 👹. With that said, obviously backticks in iFrame HTML instead of single quotes would require extra escaping to prevent interpolation, but I will never use backticks in the iFrame HTML. It would either be single or double quotes only.
Is this safe from XSS? I added \
to the regex so they can't escape the last quote, breaking the JS.