-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 json text 전체를 clean 시키면 json 구조가 망가질 수 있기 때문에 String 필드만 필터링 처리
- Loading branch information
Showing
5 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/herbaccara/xss/jackson/StringStdDeserializer.kt
This file contains 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,14 @@ | ||
package herbaccara.xss.jackson | ||
|
||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer | ||
import org.jsoup.Jsoup | ||
import org.jsoup.safety.Safelist | ||
|
||
class StringStdDeserializer(private val safelist: Safelist) : StdDeserializer<String>(String::class.java) { | ||
|
||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): String? { | ||
return p.text?.let { Jsoup.clean(it, safelist) } | ||
} | ||
} |
This file contains 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,43 @@ | ||
package herbaccara.xss | ||
|
||
import com.fasterxml.jackson.databind.module.SimpleModule | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import herbaccara.xss.jackson.StringStdDeserializer | ||
import org.jsoup.safety.Safelist | ||
import org.junit.jupiter.api.Test | ||
|
||
class JsonTest { | ||
|
||
@Test | ||
fun test() { | ||
val objectMapper = jacksonObjectMapper().apply { | ||
findAndRegisterModules() | ||
registerModule( | ||
SimpleModule().apply { | ||
addDeserializer(String::class.java, StringStdDeserializer(Safelist.none())) | ||
} | ||
) | ||
} | ||
|
||
// val json = """ | ||
// ["<script>1</scritp>", "<img>2"] | ||
// """.trimIndent() | ||
|
||
val json = """ | ||
{ | ||
"foo" : "<script>1</scritp>", | ||
"bar" : "<img>2", | ||
"asd" : null | ||
} | ||
""".trimIndent() | ||
|
||
// val clean = Jsoup.clean(json, Safelist.none()) | ||
// println(clean) | ||
// | ||
// objectMapper.readTree(json) | ||
|
||
val readValue = objectMapper.readValue<Any>(json) | ||
println(readValue) | ||
} | ||
} |