Skip to content

Commit 56b63a3

Browse files
authored
Merge pull request lichess-org#4658 from niklasf/whitelist-imgur
only embed images from imgur
2 parents de89b06 + 9c65e36 commit 56b63a3

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

modules/common/src/main/base/RawHtml.scala

+1-3
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,10 @@ final object RawHtml {
159159
last + 1
160160
}
161161

162-
private[this] val imgurRegex = """https?://imgur\.com/(\w+)""".r
163-
private[this] val imgUrlPat = """\.(?:jpg|jpeg|png|gif)$""".r.pattern
162+
private[this] val imgurRegex = """https?://(?:i\.)?imgur\.com/(\w+)(?:\.jpe?g|\.png|\.gif)?""".r
164163

165164
private[this] def imgUrl(url: String): Option[String] = (url match {
166165
case imgurRegex(id) => Some(s"""https://i.imgur.com/$id.jpg""")
167-
case _ if imgUrlPat.matcher(url).find => Some(url)
168166
case _ => None
169167
}) map { img => s"""<img class="embed" src="$img" alt="$url"/>""" }
170168

modules/common/src/test/RawHtmlTest.scala

+3-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ class RawHtmlTest extends Specification {
3535
val url = "http://foo@bar"
3636
addLinks(s"""link to $url here""") must not contain ("""href="http://foo"""")
3737
}
38-
"detect image" in {
38+
"ignore image from untrusted host" in {
3939
val url = "http://zombo.com/pic.jpg"
40-
addLinks(s"""img to $url here""") must_== {
41-
s"""img to <img class="embed" src="$url" alt="$url"/> here"""
42-
}
40+
addLinks(s"""link to $url here""") must_==
41+
s"""link to <a rel="nofollow" href="$url" target="_blank">$url</a> here"""
4342
}
4443
"detect imgur image URL" in {
4544
val url = "https://imgur.com/NXy19Im"

0 commit comments

Comments
 (0)