This is an automated email from the ASF dual-hosted git repository.
yangjie01 pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.0 by this push:
new fcf8dda2094a [SPARK-52381][CORE][4.0] JsonProtocol: Only accept
subclasses of SparkListenerEvent
fcf8dda2094a is described below
commit fcf8dda2094a17c5a1331f1be15cf345c45a426e
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Jun 30 10:55:38 2025 +0800
[SPARK-52381][CORE][4.0] 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 #51312 from pjfanning/SPARK-52381-br4.0.
Authored-by: PJ Fanning <[email protected]>
Signed-off-by: yangjie01 <[email protected]>
---
.../scala/org/apache/spark/util/JsonProtocol.scala | 10 ++++++--
.../org/apache/spark/util/JsonProtocolSuite.scala | 30 ++++++++++++++++++++++
2 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/core/src/main/scala/org/apache/spark/util/JsonProtocol.scala
b/core/src/main/scala/org/apache/spark/util/JsonProtocol.scala
index d5d7c449f23d..7b99714d7676 100644
--- a/core/src/main/scala/org/apache/spark/util/JsonProtocol.scala
+++ b/core/src/main/scala/org/apache/spark/util/JsonProtocol.scala
@@ -957,8 +957,14 @@ private[spark] object JsonProtocol extends JsonUtils {
case `stageExecutorMetrics` => stageExecutorMetricsFromJson(json)
case `blockUpdate` => blockUpdateFromJson(json)
case `resourceProfileAdded` => resourceProfileAddedFromJson(json)
- case other => mapper.readValue(json.toString, Utils.classForName(other))
- .asInstanceOf[SparkListenerEvent]
+ case other =>
+ val otherClass = Utils.classForName(other)
+ if (classOf[SparkListenerEvent].isAssignableFrom(otherClass)) {
+ mapper.readValue(json.toString, otherClass)
+ .asInstanceOf[SparkListenerEvent]
+ } else {
+ throw new SparkException(s"Unknown event type: $other")
+ }
}
}
diff --git a/core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala
b/core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala
index cfba4c02c944..bd3158910bb6 100644
--- a/core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala
+++ b/core/src/test/scala/org/apache/spark/util/JsonProtocolSuite.scala
@@ -995,6 +995,36 @@ class JsonProtocolSuite extends SparkFunSuite {
// stages that have completed even before the job start event is emitted.
testEvent(jobStart, sparkEventToJsonString(jobStart))
}
+
+ test("SPARK-52381: handle class not found") {
+ val unknownJson =
+ """{
+ | "Event" : "com.example.UnknownEvent",
+ | "foo" : "foo"
+ |}""".stripMargin
+ try {
+ JsonProtocol.sparkEventFromJson(unknownJson)
+ fail("Expected ClassNotFoundException for unknown event type")
+ } catch {
+ case e: ClassNotFoundException =>
+ }
+ }
+
+ test("SPARK-52381: only read classes that extend SparkListenerEvent") {
+ val unknownJson =
+ """{
+ | "Event" : "org.apache.spark.SparkException",
+ | "foo" : "foo"
+ |}""".stripMargin
+ try {
+ JsonProtocol.sparkEventFromJson(unknownJson)
+ fail("Expected SparkException for unknown event type")
+ } catch {
+ case e: SparkException =>
+ assert(e.getMessage.startsWith("Unknown event type"))
+ }
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]