Skip to content

Commit

Permalink
Work around new Twitch embedding restriction. They could have looked …
Browse files Browse the repository at this point in the history
…at the referrer too, to be honest. See #42.
  • Loading branch information
stefansundin committed Jul 6, 2020
1 parent 54690f6 commit 35e9711
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
send_file File.join(settings.views, "countdown.html")
end

get "/twitch-embed.html" do
content_type :html
SecureHeaders.use_secure_headers_override(request, :twitch_embed)
send_file File.join(settings.views, "twitch-embed.html")
end

# This route is useful together with this bookmarklet:
# javascript:location='https://rssbox.herokuapp.com/go?q='+encodeURIComponent(location.href);
get "/go" do
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/05-string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def embed_html(request=nil)
# https://www.twitch.tv/gamesdonequick
# https://www.twitch.tv/gamesdonequick/video/76877760 (legacy url)
# https://www.twitch.tv/gamesdonequick/v/76877760 (legacy url)
url = "https://player.twitch.tv/?"
url = "#{request.root_url}/twitch-embed.html?"
url += vod_id ? "video=#{vod_id}" : "channel=#{channel_name}"
url += "&time=#{t}" if t
<<~EOF
Expand Down
12 changes: 12 additions & 0 deletions config/initializers/10-secure_headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@
script_src: %w('unsafe-inline'),
})
end

SecureHeaders::Configuration.override(:twitch_embed) do |config|
config.x_frame_options = SecureHeaders::OPT_OUT
config.csp.merge!({
# "meta" values. these will shape the header, but the values are not included in the header.
report_only: false,
preserve_schemes: true,
# directive values: these values will directly translate into source directives
default_src: %w('none'),
frame_src: %w(https://player.twitch.tv),
})
end
36 changes: 36 additions & 0 deletions views/twitch-embed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<style>
body {
margin: 0;
overflow: hidden;
}
iframe {
min-height: 378px;
}
</style>
</head>
<body>

<noscript>Please enable JavaScript.</noscript>

<script>
let qs = window.location.search;
qs += `&parent=${window.location.hostname}`;
if (document.referrer != "") {
const referrer = new URL(document.referrer);
qs += `&parent=${referrer.hostname}`;
}

const iframe = document.createElement("iframe");
iframe.width = "100%";
iframe.height = "100%";
iframe.frameBorder = "0";
iframe.scrolling = "no";
iframe.allowFullscreen = true;
iframe.src = `https://player.twitch.tv/${qs}`;
document.body.appendChild(iframe);
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions views/twitch.atom.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<author><name><%= video["user_name"].esc %></name></author>
<content type="html">
<%= <<~EOF.esc
<iframe width="620" height="378" src="https://player.twitch.tv/?video=#{video["id"]}" frameborder="0" scrolling="no" allowfullscreen referrerpolicy="no-referrer"></iframe>
<iframe width="620" height="378" src="#{request.root_url}/twitch-embed.html?video=#{video["id"]}" frameborder="0" scrolling="no" allowfullscreen referrerpolicy="no-referrer"></iframe>
<p>
<a href="https://player.twitch.tv/?video=#{video["id"]}" rel="noreferrer">Open embed</a> |
<a href="https://player.twitch.tv/?parent=twitch.tv&video=#{video["id"]}" rel="noreferrer">Open embed</a> |
<a href="#{request.root_url}/twitch/watch?url=#{video["id"]}&open">Open in VLC</a> |
<a href="#{request.root_url}/twitch/watch?url=#{video["id"]}">Open in external program</a> |
<a href="#{request.root_url}/twitch/download?url=#{video["id"]}">Download video</a>
Expand Down

0 comments on commit 35e9711

Please sign in to comment.