Skip to content

Commit

Permalink
Remove inline scripts from UI descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rshkv committed Jan 29, 2024
1 parent 2423a45 commit f84c18c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions core/src/main/scala/org/apache/spark/ui/UIUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ private[spark] object UIUtils extends Logging {
* the whole string will rendered as a simple escaped text.
*
* Note: In terms of security, only anchor tags with root relative links are supported. So any
* attempts to embed links outside Spark UI, or other tags like <script> will cause in
* the whole description to be treated as plain text.
* attempts to embed links outside Spark UI, other tags like <script>, or inline scripts
* like `onclick` will cause in the whole description to be treated as plain text.
*
* @param desc the original job or stage description string, which may contain html tags.
* @param basePathUri with which to prepend the relative links; this is used when plainText is
Expand All @@ -571,7 +571,13 @@ private[spark] object UIUtils extends Logging {

// Verify that this has only anchors and span (we are wrapping in span)
val allowedNodeLabels = Set("a", "span", "br")
val illegalNodes = (xml \\ "_").filterNot(node => allowedNodeLabels.contains(node.label))
val allowedAttributes = Set("class", "href")
val illegalNodes =
(xml \\ "_").filterNot { node =>
allowedNodeLabels.contains(node.label) &&
// Verify we only have href attributes
node.attributes.map(_.key).forall(allowedAttributes.contains)
}
if (illegalNodes.nonEmpty) {
throw new IllegalArgumentException(
"Only HTML anchors allowed in job descriptions\n" +
Expand Down
14 changes: 14 additions & 0 deletions core/src/test/scala/org/apache/spark/ui/UIUtilsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ class UIUtilsSuite extends SparkFunSuite {
errorMsg = "Base URL should be prepended to html links",
plainText = false
)

verify(
"""<a onclick="alert('oops');"></a>""",
<span class="description-input">{"""<a onclick="alert('oops');"></a>"""}</span>,
"Non href attributes should make the description be treated as a string instead of HTML",
plainText = false
)

verify(
"""<a onmouseover="alert('oops');"></a>""",
<span class="description-input">{"""<a onmouseover="alert('oops');"></a>"""}</span>,
"Non href attributes should make the description be treated as a string instead of HTML",
plainText = false
)
}

test("makeDescription(plainText = true)") {
Expand Down

0 comments on commit f84c18c

Please sign in to comment.