Skip to content

Commit

Permalink
Merge pull request #1565 from kmuto/check-tricky-id-as-path
Browse files Browse the repository at this point in the history
do not allow tricky id used as path; fix #1393
  • Loading branch information
takahashim authored Aug 29, 2020
2 parents 8d6823b + f27b4d3 commit abae279
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
11 changes: 10 additions & 1 deletion lib/review/book/index/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ def initialize(id, number, caption = nil)
alias_method :content, :caption

def path
@path ||= @index.find_path(id)
if @path
return @path
end

if @id =~ /\s/
raise ReVIEW::SyntaxError, "invalid ID character for path: `#{@id}`"
end
@path = @index.find_path(@id)

@path
end
end
end
Expand Down
21 changes: 14 additions & 7 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -665,25 +665,32 @@ def @chapter.image(_id)
assert_equal expected, actual
end

def test_image_with_tricky_id
def test_image_with_tricky_id_kana
def @chapter.image(_id)
item = Book::Index::Item.new('123 あ_;', 1)
item.instance_eval { @path = './images/chap1-123 あ_;.png' }
item = Book::Index::Item.new('123あいう', 1)
item.instance_eval { @path = './images/123あいう.png' }
item
end

actual = compile_block("//image[123 あ_;][sample photo]{\n//}\n")
@chapter.instance_eval { @name = 'ch01' }
actual = compile_block("//image[123あいう][sample photo]{\n//}\nimg: @<img>{123あいう}\n")
expected = <<-EOS
<div id="id_123-_E3_81_82___3B" class="image">
<img src="images/chap1-123 あ_;.png" alt="sample photo" />
<div id="id_123_E3_81_82_E3_81_84_E3_81_86" class="image">
<img src="images/123あいう.png" alt="sample photo" />
<p class="caption">
図1.1: sample photo
</p>
</div>
<p>img: <span class="imgref"><a href="./ch01.html#id_123_E3_81_82_E3_81_84_E3_81_86">図1.1</a></span></p>
EOS
assert_equal expected, actual
end

def test_image_with_tricky_id_space
assert_raise(ReVIEW::SyntaxError) do
_result = compile_block("//image[123 abc][sample photo]{\n//}\n")
end
end

def test_indepimage
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1)
Expand Down

0 comments on commit abae279

Please sign in to comment.