Skip to content

Commit a53a9c4

Browse files
pjfanningLuciferYang
authored andcommitted
[SPARK-52381][CORE][3.5] JsonProtocol: Only accept subclasses of SparkListenerEvent
### What changes were proposed in this pull request? JsonProtocol tidy up. Only parse JSON relating to Spark events. https://issues.apache.org/jira/browse/SPARK-52381 ### Why are the changes needed? Tidier code and https://lists.apache.org/thread/9zwkdo85wcdfppgqvbhjly8wdgf595yp ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Unit test ### Was this patch authored or co-authored using generative AI tooling? No Closes #51323 from pjfanning/SPARK-52381-br3.5. Authored-by: PJ Fanning <[email protected]> Signed-off-by: yangjie01 <[email protected]>
1 parent 57da485 commit a53a9c4

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

core/src/main/scala/org/apache/spark/util/JsonProtocol.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,8 +874,14 @@ private[spark] object JsonProtocol extends JsonUtils {
874874
case `stageExecutorMetrics` => stageExecutorMetricsFromJson(json)
875875
case `blockUpdate` => blockUpdateFromJson(json)
876876
case `resourceProfileAdded` => resourceProfileAddedFromJson(json)
877-
case other => mapper.readValue(json.toString, Utils.classForName(other))
878-
.asInstanceOf[SparkListenerEvent]
877+
case other =>
878+
val otherClass = Utils.classForName(other)
879+
if (classOf[SparkListenerEvent].isAssignableFrom(otherClass)) {
880+
mapper.readValue(json.toString, otherClass)
881+
.asInstanceOf[SparkListenerEvent]
882+
} else {
883+
throw new SparkException(s"Unknown event type: $other")
884+
}
879885
}
880886
}
881887

core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,36 @@ class JsonProtocolSuite extends SparkFunSuite {
874874
val jobFailedEvent = JsonProtocol.sparkEventFromJson(exJobFailureNoStackJson)
875875
testEvent(jobFailedEvent, exJobFailureExpectedJson)
876876
}
877+
878+
test("SPARK-52381: handle class not found") {
879+
val unknownJson =
880+
"""{
881+
| "Event" : "com.example.UnknownEvent",
882+
| "foo" : "foo"
883+
|}""".stripMargin
884+
try {
885+
JsonProtocol.sparkEventFromJson(unknownJson)
886+
fail("Expected ClassNotFoundException for unknown event type")
887+
} catch {
888+
case e: ClassNotFoundException =>
889+
}
890+
}
891+
892+
test("SPARK-52381: only read classes that extend SparkListenerEvent") {
893+
val unknownJson =
894+
"""{
895+
| "Event" : "org.apache.spark.SparkException",
896+
| "foo" : "foo"
897+
|}""".stripMargin
898+
try {
899+
JsonProtocol.sparkEventFromJson(unknownJson)
900+
fail("Expected SparkException for unknown event type")
901+
} catch {
902+
case e: SparkException =>
903+
assert(e.getMessage.startsWith("Unknown event type"))
904+
}
905+
}
906+
877907
}
878908

879909

0 commit comments

Comments
 (0)