← 中文资源

简体中文版本

isprintable 审计不可见字符

零宽空格与 NBSP 在屏幕上完全隐形,却会让两个"相同"的用户名不相等。isprintable() 对控制符、格式符与非 ASCII 空格返回 False,一次推导就能列出可疑码点。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

clean input: clean
zero-width in middle: ['U+200B ZERO WIDTH SPACE']
nbsp instead of space: ['U+00A0 NO-BREAK SPACE']

机制说明

INVISIBLE 映射点名五个嫌疑字符:零宽空格、non-joiner、joiner、BOM 与不换行空格。audit() 列出所有 isprintable() 为 False 的字符,按映射标注或回退为 non-printable。

三条提交依次审计:[email protected] 打印 clean,注入零宽字符的地址报告 U+200B ZERO WIDTH SPACE,含 NBSP 的密码报告 U+00A0 NO-BREAK SPACE——正是让两个“相同”名称不相等的字符。

注意事项

  • isprintable() 覆盖控制符、格式符与非 ASCII 空格三类。
  • 在存储或匹配用户输入之前先跑审计。

核心语句

if not ch.isprintable()

代码示例

# Invisible characters disguise themselves as normal text: zero-width spaces slip
# into usernames, no-break spaces replace real ones from copy-pasted input
INVISIBLE = {
    "\u200b": "ZERO WIDTH SPACE",
    "\u200c": "ZERO WIDTH NON-JOINER",
    "\u200d": "ZERO WIDTH JOINER",
    "\ufeff": "ZERO WIDTH NO-BREAK SPACE (BOM)",
    "\u00a0": "NO-BREAK SPACE",
}

def audit(text: str) -> list[str]:
    # isprintable() is False for control, format, and non-ASCII space characters
    return [
        f"U+{ord(ch):04X} {INVISIBLE.get(ch, 'non-printable')}"
        for ch in text
        if not ch.isprintable()
    ]

submissions = {
    "[email protected]": "clean input",
    "ali\u200b[email protected]": "zero-width in middle",
    "pass\u00a0word": "nbsp instead of space",
}

for text, label in submissions.items():
    findings = audit(text)
    print(f"{label}: {findings if findings else 'clean'}")