-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加ParseConfig,通过增加maxNestingDepth参数避免StackOverflowError问题,修复CVE-2022-…
…45688漏洞
- Loading branch information
Showing
5 changed files
with
170 additions
and
6 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
88 changes: 88 additions & 0 deletions
88
hutool-json/src/main/java/cn/hutool/json/xml/ParseConfig.java
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,88 @@ | ||
/* | ||
* Copyright (c) 2024. looly([email protected]) | ||
* Hutool is licensed under Mulan PSL v2. | ||
* You can use this software according to the terms and conditions of the Mulan PSL v2. | ||
* You may obtain a copy of Mulan PSL v2 at: | ||
* https://license.coscl.org.cn/MulanPSL2 | ||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, | ||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, | ||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. | ||
* See the Mulan PSL v2 for more details. | ||
*/ | ||
|
||
package cn.hutool.json.xml; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* XML解析为JSON的可选选项<br> | ||
* 参考:https://github.com/stleary/JSON-java/blob/master/src/main/java/org/json/ParserConfiguration.java | ||
* | ||
* @author AylwardJ, Looly | ||
*/ | ||
public class ParseConfig implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* 默认最大嵌套深度 | ||
*/ | ||
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512; | ||
|
||
/** | ||
* 创建ParseConfig | ||
* | ||
* @return ParseConfig | ||
*/ | ||
public static ParseConfig of() { | ||
return new ParseConfig(); | ||
} | ||
|
||
/** | ||
* 是否保持值为String类型,如果为{@code false},则尝试转换为对应类型(numeric, boolean, string) | ||
*/ | ||
private boolean keepStrings; | ||
/** | ||
* 最大嵌套深度,用于解析时限制解析层级,当大于这个层级时抛出异常,-1表示无限制 | ||
*/ | ||
private int maxNestingDepth = -1; | ||
|
||
/** | ||
* 是否保持值为String类型,如果为{@code false},则尝试转换为对应类型(numeric, boolean, string) | ||
* | ||
* @return 是否保持值为String类型 | ||
*/ | ||
public boolean isKeepStrings() { | ||
return keepStrings; | ||
} | ||
|
||
/** | ||
* 设置是否保持值为String类型,如果为{@code false},则尝试转换为对应类型(numeric, boolean, string) | ||
* | ||
* @param keepStrings 是否保持值为String类型 | ||
* @return this | ||
*/ | ||
public ParseConfig setKeepStrings(final boolean keepStrings) { | ||
this.keepStrings = keepStrings; | ||
return this; | ||
} | ||
|
||
/** | ||
* 获取最大嵌套深度,用于解析时限制解析层级,当大于这个层级时抛出异常,-1表示无限制 | ||
* | ||
* @return 最大嵌套深度 | ||
*/ | ||
public int getMaxNestingDepth() { | ||
return maxNestingDepth; | ||
} | ||
|
||
/** | ||
* 设置最大嵌套深度,用于解析时限制解析层级,当大于这个层级时抛出异常,-1表示无限制 | ||
* | ||
* @param maxNestingDepth 最大嵌套深度 | ||
* @return this | ||
*/ | ||
public ParseConfig setMaxNestingDepth(final int maxNestingDepth) { | ||
this.maxNestingDepth = maxNestingDepth; | ||
return this; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
hutool-json/src/test/java/cn/hutool/json/xml/Issue2748Test.java
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,19 @@ | ||
package cn.hutool.json.xml; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import cn.hutool.json.JSONException; | ||
import cn.hutool.json.XML; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class Issue2748Test { | ||
|
||
@Test | ||
public void toJSONObjectTest() { | ||
final String s = StrUtil.repeat("<a>", 600); | ||
|
||
Assert.assertThrows(JSONException.class, () -> { | ||
XML.toJSONObject(s, ParseConfig.of().setMaxNestingDepth(512)); | ||
}); | ||
} | ||
} |