Skip to content

Commit 7ec0241

Browse files
authored
Merge pull request increments#82 from increments/fix-iframe-width
Update iframe transformer to fix iframe width
2 parents 9382f16 + 6086fd7 commit 7ec0241

File tree

3 files changed

+85
-18
lines changed

3 files changed

+85
-18
lines changed

lib/qiita/markdown/transformers/filter_iframe.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def initialize(env)
2222
def transform
2323
if name == "iframe"
2424
if URL_WHITE_LIST.include?(node["src"]) || HOST_WHITE_LIST.include?(host_of(node["src"]))
25+
node["width"] = "100%"
2526
node.children.unlink
2627
else
2728
node.unlink
@@ -40,7 +41,10 @@ def node
4041
end
4142

4243
def host_of(url)
43-
Addressable::URI.parse(url).host if url
44+
if url
45+
port = URI.parse(url).port
46+
Addressable::URI.parse(url).host if [443, 80].include? port
47+
end
4448
rescue Addressable::URI::InvalidURIError
4549
nil
4650
end

lib/qiita/markdown/transformers/filter_script.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def node
4343
end
4444

4545
def host_of(url)
46-
Addressable::URI.parse(url).host if url
46+
if url
47+
port = URI.parse(url).port
48+
Addressable::URI.parse(url).host if [443, 80].include? port
49+
end
4750
rescue Addressable::URI::InvalidURIError
4851
nil
4952
end

spec/qiita/markdown/processor_spec.rb

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,10 +1480,18 @@
14801480
MARKDOWN
14811481
end
14821482

1483-
it "does not sanitize embed code" do
1484-
should eq <<-HTML.strip_heredoc
1485-
<iframe width="100" height="100" src="https://www.youtube.com/embed/example"></iframe>
1486-
HTML
1483+
if allowed
1484+
it "does not sanitize embed code" do
1485+
should eq <<-HTML.strip_heredoc
1486+
<iframe width="100" height="100" src="https://www.youtube.com/embed/example"></iframe>
1487+
HTML
1488+
end
1489+
else
1490+
it "forces width attribute on iframe" do
1491+
should eq <<-HTML.strip_heredoc
1492+
<iframe width="100%" height="100" src="https://www.youtube.com/embed/example"></iframe>
1493+
HTML
1494+
end
14871495
end
14881496

14891497
context "when url is privacy enhanced mode" do
@@ -1493,10 +1501,18 @@
14931501
MARKDOWN
14941502
end
14951503

1496-
it "does not sanitize embed code" do
1497-
should eq <<-HTML.strip_heredoc
1498-
<iframe width="100" height="100" src="https://www.youtube-nocookie.com/embed/example"></iframe>
1499-
HTML
1504+
if allowed
1505+
it "does not sanitize embed code" do
1506+
should eq <<-HTML.strip_heredoc
1507+
<iframe width="100" height="100" src="https://www.youtube-nocookie.com/embed/example"></iframe>
1508+
HTML
1509+
end
1510+
else
1511+
it "forces width attribute on iframe" do
1512+
should eq <<-HTML.strip_heredoc
1513+
<iframe width="100%" height="100" src="https://www.youtube-nocookie.com/embed/example"></iframe>
1514+
HTML
1515+
end
15001516
end
15011517
end
15021518
end
@@ -1508,10 +1524,18 @@
15081524
MARKDOWN
15091525
end
15101526

1511-
it "does not sanitize embed code" do
1512-
should eq <<-HTML.strip_heredoc
1513-
<iframe width="100" height="100" src="https://www.slideshare.net/embed/example"></iframe>
1514-
HTML
1527+
if allowed
1528+
it "does not sanitize embed code" do
1529+
should eq <<-HTML.strip_heredoc
1530+
<iframe width="100" height="100" src="https://www.slideshare.net/embed/example"></iframe>
1531+
HTML
1532+
end
1533+
else
1534+
it "forces width attribute on iframe" do
1535+
should eq <<-HTML.strip_heredoc
1536+
<iframe width="100%" height="100" src="https://www.slideshare.net/embed/example"></iframe>
1537+
HTML
1538+
end
15151539
end
15161540
end
15171541

@@ -1522,10 +1546,18 @@
15221546
MARKDOWN
15231547
end
15241548

1525-
it "does not sanitize embed code" do
1526-
should eq <<-HTML.strip_heredoc
1527-
<iframe src="https://docs.google.com/presentation/d/example/embed" frameborder="0" width="482" height="300" allowfullscreen="true"></iframe>
1528-
HTML
1549+
if allowed
1550+
it "does not sanitize embed code" do
1551+
should eq <<-HTML.strip_heredoc
1552+
<iframe src="https://docs.google.com/presentation/d/example/embed" frameborder="0" width="482" height="300" allowfullscreen="true"></iframe>
1553+
HTML
1554+
end
1555+
else
1556+
it "forces width attribute on iframe" do
1557+
should eq <<-HTML.strip_heredoc
1558+
<iframe src="https://docs.google.com/presentation/d/example/embed" frameborder="0" width="100%" height="300" allowfullscreen="true"></iframe>
1559+
HTML
1560+
end
15291561
end
15301562
end
15311563

@@ -1566,6 +1598,34 @@
15661598
HTML
15671599
end
15681600
end
1601+
1602+
context "with embed script code with xss" do
1603+
let(:markdown) do
1604+
<<-MARKDOWN.strip_heredoc
1605+
<script async class="speakerdeck-embed" data-id="example" data-ratio="1.33333333333333" src="javascript://speakerdeck.com/assets/embed.js"></script>
1606+
MARKDOWN
1607+
1608+
it "forces width attribute on iframe" do
1609+
should eq <<-HTML.strip_heredoc
1610+
\n
1611+
HTML
1612+
end
1613+
end
1614+
end
1615+
1616+
context "with embed iframe code with xss" do
1617+
let(:markdown) do
1618+
<<-MARKDOWN.strip_heredoc
1619+
<iframe src="javascript://docs.google.com/presentation/d/example/embed" frameborder="0" width="482" height="300" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
1620+
MARKDOWN
1621+
1622+
it "forces width attribute on iframe" do
1623+
should eq <<-HTML.strip_heredoc
1624+
\n
1625+
HTML
1626+
end
1627+
end
1628+
end
15691629
end
15701630

15711631
context "without script and strict context" do

0 commit comments

Comments
 (0)