summary refs log tree commit diff
path: root/synapse/util/stringutils.py
diff options
context:
space:
mode:
authorDavid Robertson <davidr@element.io>2022-05-18 11:28:14 +0100
committerGitHub <noreply@github.com>2022-05-18 11:28:14 +0100
commitd4713d3e335b21d12284ddd8ebd00e38abcfd521 (patch)
treeca02237e04ac9f1139a52e36c7c169e82d7b8cdf /synapse/util/stringutils.py
parentMake handling of federation Authorization header (more) compliant with RFC723... (diff)
downloadsynapse-d4713d3e335b21d12284ddd8ebd00e38abcfd521.tar.xz
Discard null-containing strings before updating the user directory (#12762)
Diffstat (limited to 'synapse/util/stringutils.py')
-rw-r--r--synapse/util/stringutils.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/synapse/util/stringutils.py b/synapse/util/stringutils.py
index b26546aecd..27a363d7e5 100644
--- a/synapse/util/stringutils.py
+++ b/synapse/util/stringutils.py
@@ -16,7 +16,7 @@ import itertools
 import re
 import secrets
 import string
-from typing import Iterable, Optional, Tuple
+from typing import Any, Iterable, Optional, Tuple
 
 from netaddr import valid_ipv6
 
@@ -247,3 +247,11 @@ def base62_encode(num: int, minwidth: int = 1) -> str:
     # pad to minimum width
     pad = "0" * (minwidth - len(res))
     return pad + res
+
+
+def non_null_str_or_none(val: Any) -> Optional[str]:
+    """Check that the arg is a string containing no null (U+0000) codepoints.
+
+    If so, returns the given string unmodified; otherwise, returns None.
+    """
+    return val if isinstance(val, str) and "\u0000" not in val else None