Skip to content

Commit cdcf5a3

Browse files
committed
refactor: 优化金属头像生成参数处理
- 重构参数处理逻辑,提高代码可读性和维护性 - 增加参数安全过滤,防止潜在的注入攻击 - 优化参数拼接,提高效率
1 parent ec24fb0 commit cdcf5a3

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/main/java/org/b3log/symphony/processor/ChatroomProcessor.java

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,14 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
228228
}
229229
});
230230
public void genMetal(final RequestContext context) {
231-
Set<String> params = context.getRequest().getParameterNames();
232-
String paramString = "";
233-
List<String> paramList = new ArrayList<>();
234-
paramList.add("ver");
235-
paramList.add("scale");
236-
paramList.add("txt");
237-
paramList.add("url");
238-
paramList.add("backcolor");
239-
paramList.add("fontcolor");
240-
for (String param : params) {
241-
if (paramList.contains(param)) {
242-
paramString += param + "=" + context.getRequest().getParameter(param) + "&";
243-
}
244-
}
245-
paramString = "?" + paramString.substring(0, paramString.length() - 1);
231+
String ver = safeParam(context.param("ver"), "ver");
232+
String scale = safeParam(context.param("scale"), "scale");
233+
String txt = safeParam(context.param("txt"), "txt");
234+
String url = safeParam(context.param("url"), "url");
235+
String backcolor = safeParam(context.param("backcolor"), "backcolor");
236+
String fontcolor = safeParam(context.param("fontcolor"), "fontcolor");
237+
String paramString = "?ver=" + ver + "&scale=" + scale + "&txt=" + txt + "&url=" + url + "&backcolor=" + backcolor + "&fontcolor=" + fontcolor;
238+
246239
String body = "";
247240
if (!metalCache.containsKey(paramString)) {
248241
String genUrl = Symphonys.get("gen.metal.url") + paramString;
@@ -263,6 +256,32 @@ public void genMetal(final RequestContext context) {
263256
context.getResponse().sendBytes(body.getBytes());
264257
}
265258

259+
public static String safeParam(String value, String type) {
260+
if (value == null) return "";
261+
262+
if ("ver".equals(type) || "scale".equals(type)) {
263+
return value.replaceAll("[^\\d.]", "")
264+
.replaceAll("\\.{2,}", ".")
265+
.replaceAll("^[^\\d]+", "")
266+
.replaceAll("(\\..*)\\.", "$1");
267+
} else if ("txt".equals(type)) {
268+
return value.replaceAll("[^\\u4e00-\\u9fa5a-zA-Z0-9\\s,。!?;:“”‘’()【】《》…—~-]", "");
269+
} else if ("url".equals(type)) {
270+
String filtered = value.replaceAll("[^a-zA-Z0-9\\-._~:/?#@!$&'()*+,;=%]", "");
271+
return filtered.startsWith("https://file.fishpi.cn") ? filtered : "";
272+
} else if ("fontcolor".equals(type)) {
273+
return value.replaceAll("[^0-9a-fA-F]", "")
274+
.substring(0, Math.min(6, value.length()))
275+
.toLowerCase();
276+
} else if ("backcolor".equals(type)) {
277+
return value.replaceAll("[^0-9a-fA-F,]", "")
278+
.substring(0, Math.min(13, value.length()))
279+
.toLowerCase();
280+
}
281+
return value;
282+
}
283+
284+
266285
public void nodePush(final RequestContext context) {
267286
final JSONObject requestJSONObject = context.requestJSON();
268287
String adminKey = requestJSONObject.optString("adminKey");

0 commit comments

Comments
 (0)