Class InputSanitizer
comment / reason / userId) before the
engine persists them, per design v4 §7.3.
包私有工具:在引擎写库前清洗与校验用户提交的文本字段(comment /
reason / userId),对应设计 v4 §7.3。
Rules:
comment: strip0x00 — 0x1Fand0x7Fcontrol characters (preserving\n); collapse\r\nand lone\rto\n; max length 2000.reason: same character rules; max length 500.userId: max length 64; must match^[A-Za-z0-9_\-]+$.
Length / character violations throw IllegalActionException with
FlowErrorCode.INPUT_INVALID_TEXT (BASE-FLOW-INPUT-001).
sanitizeComment(String) / sanitizeReason(String) accept
null and return null; validateUserId(String)
rejects null.
This sanitiser intentionally does not escape HTML/JS — that is the rendering layer's responsibility.
本清洗器有意不做 HTML/JS 转义,那是渲染层的职责。
- Since:
- JDK 25, opencode-base-flow V1.0.5
- Author:
- Leon Soo
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMax length for fields embedded in exception messages. — 异常消息嵌入字段长度上限。static final intMax length of a comment field. — 评论字段长度上限。static final intMax length of a reason field. — 理由字段长度上限。static final intMax length of a user id. — 用户 ID 长度上限。static final PatternAllowed characters in a user id. — 用户 ID 允许的字符集。 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringforErrorMessage(String input) Sanitises a string for safe embedding in exception / log messages — strips CRLF, C0 controls, DEL, Unicode line / segment separators (U+0085 / U+2028 / U+2029), bidi / zero-width characters and the BOM, then truncates to 64 characters.static StringsanitizeComment(String input) Sanitises acommentfield: returnsnullfornullinput; normalises line endings to\n; strips control characters other than\n; enforcesMAX_COMMENT_LEN.static StringsanitizeReason(String input) static StringvalidateUserId(String userId)
-
Field Details
-
MAX_COMMENT_LEN
public static final int MAX_COMMENT_LENMax length of a comment field. — 评论字段长度上限。- See Also:
-
MAX_REASON_LEN
public static final int MAX_REASON_LENMax length of a reason field. — 理由字段长度上限。- See Also:
-
MAX_USERID_LEN
public static final int MAX_USERID_LENMax length of a user id. — 用户 ID 长度上限。- See Also:
-
ERROR_FIELD_MAX_LEN
public static final int ERROR_FIELD_MAX_LENMax length for fields embedded in exception messages. — 异常消息嵌入字段长度上限。- See Also:
-
USERID_PATTERN
Allowed characters in a user id. — 用户 ID 允许的字符集。The leading character must be alphanumeric or underscore (no hyphen) to prevent argument-injection downstream (a
-prefix could be misinterpreted as a CLI flag by hosts that pass userIds into external commands without--separators).首字符必须是字母数字或下划线(禁止 hyphen),避免下游把
-foo误判为 CLI flag 引发参数注入。
-
-
Method Details
-
sanitizeComment
Sanitises acommentfield: returnsnullfornullinput; normalises line endings to\n; strips control characters other than\n; enforcesMAX_COMMENT_LEN. 清洗comment:null直接返回null;换行统一为\n;剥离除\n外的控制字符;长度上限MAX_COMMENT_LEN,超长抛IllegalActionException(FlowErrorCode.INPUT_INVALID_TEXT)。- Parameters:
input- raw input — 原始输入- Returns:
- cleaned text, or
nullwhen input isnull— 清洗后的文本(输入为null时返回null) - Throws:
IllegalActionException- when the cleaned text exceedsMAX_COMMENT_LENcharacters- Since:
- JDK 25, opencode-base-flow V1.0.5
-
sanitizeReason
Sanitises areasonfield: same rules assanitizeComment(String)but capped atMAX_REASON_LEN. 清洗reason:规则同sanitizeComment(String),上限MAX_REASON_LEN。- Parameters:
input- raw input — 原始输入- Returns:
- cleaned text, or
nullwhen input isnull— 清洗后的文本(输入为null时返回null) - Throws:
IllegalActionException- when the cleaned text exceedsMAX_REASON_LENcharacters- Since:
- JDK 25, opencode-base-flow V1.0.5
-
forErrorMessage
Sanitises a string for safe embedding in exception / log messages — strips CRLF, C0 controls, DEL, Unicode line / segment separators (U+0085 / U+2028 / U+2029), bidi / zero-width characters and the BOM, then truncates to 64 characters. Never throws and never returnsnull:nullinput is rendered as"<null>"so callers can string-concat without a guard. 用于异常 / 日志消息拼接的脱敏:剥离 CRLF、C0、DEL、行 / 段分隔符以及 bidi / zero-width / BOM,按 64 截断。永不 抛异常 / 永不返回null(null输入呈现为"<null>"), 便于调用方直接字符串拼接而无需空值守卫。The truncation marker
"…"(U+2026, single char) is appended when the input is trimmed so log readers can see the cut. Use this for error-message contexts only — for persisted text fields usesanitizeComment(String)orsanitizeReason(String).截断时追加
"…"(U+2026),便于日志阅读者识别。仅用于 异常消息,持久化场景请用 sanitizeComment / sanitizeReason。- Parameters:
input- raw input (may benull) — 原始输入(允许null)- Returns:
- sanitised + truncated text, never
null— 脱敏 + 截断后的文本 - Since:
- JDK 25, opencode-base-flow V1.0.5
-
validateUserId
-