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

AO3-6665 Check URL protocols of media src attributes #4713

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions lib/otw_sanitize/media_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,37 @@ class controls crossorigin dir height loop
audio video source track
] + Sanitize::Config::ARCHIVE[:elements],
attributes: {
'audio' => AUDIO_ATTRIBUTES,
'video' => VIDEO_ATTRIBUTES,
'source' => SOURCE_ATTRIBUTES,
'track' => TRACK_ATTRIBUTES
"audio" => AUDIO_ATTRIBUTES,
"video" => VIDEO_ATTRIBUTES,
"source" => SOURCE_ATTRIBUTES,
"track" => TRACK_ATTRIBUTES
},
add_attributes: {
'audio' => {
'controls' => 'controls',
'crossorigin' => 'anonymous',
'preload' => 'metadata'
"audio" => {
"controls" => "controls",
"crossorigin" => "anonymous",
"preload" => "metadata"
},
'video' => {
'controls' => 'controls',
'playsinline' => 'playsinline',
'crossorigin' => 'anonymous',
'preload' => 'metadata'
"video" => {
"controls" => "controls",
"playsinline" => "playsinline",
"crossorigin" => "anonymous",
"preload" => "metadata"
}
},
protocols: {
'audio' => {
'src' => %w[http https]
"audio" => {
"src" => %w[http https]
},
'video' => {
'poster' => %w[http https],
'src' => %w[http https]
"video" => {
"poster" => %w[http https],
"src" => %w[http https]
},
"source" => {
"src" => %w[http https]
},
"track" => {
"src" => %w[http https]
}
}
}.freeze
Expand Down Expand Up @@ -102,6 +108,7 @@ def source_host

def banned_source?
return unless source_host

ArchiveConfig.BANNED_MULTIMEDIA_SRCS.any? do |blocked|
source_host.match(blocked)
end
Expand Down
13 changes: 11 additions & 2 deletions spec/lib/otw_sanitize/media_sanitizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
it "allows source elements" do
html = "
<video controls width='250'>
<source src='example.com/flower.webm' type='video/webm'>
<source src='example.com/flower.mp4' type='video/mp4'>
<source src='http://example.com/flower.webm' type='video/webm'>
<source src='http://example.com/flower.mp4' type='video/mp4'>
Sorry, your browser doesn't support embedded videos.
</video>"
content = Sanitize.fragment(html, config)
Expand Down Expand Up @@ -99,6 +99,15 @@
expect(content).not_to match("javascript")
end

%w[audio video source track].each do |element|
it "removes src on #{element} elements for unsupported protocols" do
html = "<#{element} src='file://flower.mp4'></#{element}>"
content = Sanitize.fragment(html, config)
expect(content).not_to match("src")
expect(content).not_to match("file://")
end
end

context "given a blacklisted source" do
before do
ArchiveConfig.BANNED_MULTIMEDIA_SRCS = ["google.com"]
Expand Down
Loading