Skip to content

Commit fbac8f2

Browse files
committed
Convert newpipe/settings/export/PreferencesObjectInputStream to kotlin
1 parent 1eb0bd0 commit fbac8f2

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

app/src/main/java/org/schabi/newpipe/settings/export/PreferencesObjectInputStream.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2026 NewPipe contributors <https://newpipe.net>
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
package org.schabi.newpipe.settings.export
7+
8+
import java.io.IOException
9+
import java.io.InputStream
10+
import java.io.ObjectInputStream
11+
import java.io.ObjectStreamClass
12+
13+
/**
14+
* An [ObjectInputStream] that only allows preferences-related types to be deserialized, to
15+
* prevent injections. The only allowed types are: all primitive types, all boxed primitive types,
16+
* null, strings. HashMap, HashSet and arrays of previously defined types are also allowed. Sources:
17+
* [cmu.edu](https://wiki.sei.cmu.edu/confluence/display/java/SER00-J.+Enable+serialization+compatibility+during+class+evolution) * ,
18+
* [OWASP cheatsheet](https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html#harden-your-own-javaioobjectinputstream) * ,
19+
* [Apache's `ValidatingObjectInputStream`](https://commons.apache.org/proper/commons-io/apidocs/src-html/org/apache/commons/io/serialization/ValidatingObjectInputStream.html#line-118) *
20+
*/
21+
class PreferencesObjectInputStream(stream: InputStream) : ObjectInputStream(stream) {
22+
@Throws(ClassNotFoundException::class, IOException::class)
23+
override fun resolveClass(desc: ObjectStreamClass): Class<*> {
24+
if (desc.name in CLASS_WHITELIST) {
25+
return super.resolveClass(desc)
26+
} else {
27+
throw ClassNotFoundException("Class not allowed: $desc.name")
28+
}
29+
}
30+
31+
companion object {
32+
/**
33+
* Primitive types, strings and other built-in types do not pass through resolveClass() but
34+
* instead have a custom encoding; see
35+
* [
36+
* official docs](https://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html#10152).
37+
*/
38+
private val CLASS_WHITELIST = setOf<String>(
39+
"java.lang.Boolean",
40+
"java.lang.Byte",
41+
"java.lang.Character",
42+
"java.lang.Short",
43+
"java.lang.Integer",
44+
"java.lang.Long",
45+
"java.lang.Float",
46+
"java.lang.Double",
47+
"java.lang.Void",
48+
"java.util.HashMap",
49+
"java.util.HashSet"
50+
)
51+
}
52+
}

0 commit comments

Comments
 (0)