-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[MINOR][UI] Encode query parameters when generating log page scripts #57165
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
Closed
holdenk
wants to merge
4
commits into
apache:master
from
holdenk:spark-log-viewer-minor-ui-improvement
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
core/src/test/scala/org/apache/spark/deploy/history/LogPageSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.deploy.history | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest | ||
| import org.mockito.Mockito.{mock, when} | ||
|
|
||
| import org.apache.spark.{SparkConf, SparkFunSuite} | ||
|
|
||
| class LogPageSuite extends SparkFunSuite { | ||
|
|
||
| test("render encodes the logType parameter embedded in the inline script") { | ||
| val page = new LogPage(new SparkConf(false)) | ||
| val request = mock(classOf[HttpServletRequest]) | ||
| // A value that would break out of the single-quoted JavaScript string literal, close the | ||
| // script element, or start a new statement if it were emitted into the page verbatim. | ||
| when(request.getParameter("logType")).thenReturn("stdout');alert('xss')</script>") | ||
| val html = page.render(request).mkString | ||
|
|
||
| // The value is URL-encoded, so the quotes, angle brackets and slash that would break out of | ||
| // the inline <script> (or inject extra /log parameters) become inert percent escapes. | ||
| assert(html.contains("logType=stdout%27%29%3Balert%28%27xss%27%29%3C%2Fscript%3E")) | ||
| // The raw, structure-breaking form must never reach the generated query string. (The page | ||
| // title reflects logType too, but there the XML library HTML-escapes it, so it is safe.) | ||
| assert(!html.contains("logType=stdout');")) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ package org.apache.spark.deploy.worker.ui | |
|
|
||
| import java.io.{File, FileWriter} | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest | ||
| import org.mockito.Mockito.{mock, when} | ||
| import org.scalatest.PrivateMethodTester | ||
|
|
||
|
|
@@ -75,6 +76,32 @@ class LogPageSuite extends SparkFunSuite with PrivateMethodTester { | |
| assert(error4.startsWith("Error: invalid log directory")) | ||
| } | ||
|
|
||
| test("render encodes request parameters embedded in the inline script") { | ||
| val webui = mock(classOf[WorkerWebUI]) | ||
| val worker = mock(classOf[Worker]) | ||
| val tmpDir = new File(sys.props("java.io.tmpdir")) | ||
| val workDir = new File(tmpDir, "work-dir") | ||
| workDir.mkdir() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a new test case, shall we use withTempDir { workDir =>
val webui = mock(classOf[WorkerWebUI])
val worker = mock(classOf[Worker])
when(webui.workDir).thenReturn(workDir)
...
} |
||
| when(webui.workDir).thenReturn(workDir) | ||
| when(webui.worker).thenReturn(worker) | ||
| when(worker.conf).thenReturn(new SparkConf()) | ||
| when(worker.activeMasterWebUiUrl).thenReturn("http://master:8080") | ||
| val logPage = new LogPage(webui) | ||
|
|
||
| val request = mock(classOf[HttpServletRequest]) | ||
| // appId is user-controlled; a '&' in it would inject an extra parameter into the /log | ||
| // requests, and the '</script>' would break out of the inline <script> block, if emitted raw. | ||
| when(request.getParameter("appId")).thenReturn("app&byteLength=0</script>") | ||
| when(request.getParameter("executorId")).thenReturn("0") | ||
| when(request.getParameter("logType")).thenReturn("stdout") | ||
| val html = logPage.render(request).mkString | ||
|
|
||
| // The value is percent-encoded, so it stays a single opaque appId value and cannot inject a | ||
| // parameter or close the script element. | ||
| assert(html.contains("appId=app%26byteLength%3D0%3C%2Fscript%3E&executorId=0")) | ||
| assert(!html.contains("app&byteLength=0</script>")) | ||
| } | ||
|
|
||
| /** Write the specified string to the file. */ | ||
| private def write(f: File, s: String): Unit = { | ||
| val writer = new FileWriter(f) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.