def remove_control_and_redundant_space(s: str) -> str: """Substitute multiple concatenated control and space characters into single whitespace.""" res = '' for ch in s: # Convert Unicode control and space characters into simple whitespaces ss = ch if not stringprep.in_table_c11_c12( ch) and not stringprep.in_table_c21_c22(ch) else ' ' res += ss # Remove redundant spaces return ' '.join(res.split())
def _process(self, value): for code in force_text(value): # TODO: Is this long enough? if stringprep.in_table_c12(code) or stringprep.in_table_c21_c22(code) or \ stringprep.in_table_c3(code) or stringprep.in_table_c4(code) or \ stringprep.in_table_c5(code) or stringprep.in_table_c6(code) or \ stringprep.in_table_c7(code) or stringprep.in_table_c8(code) or \ stringprep.in_table_c9(code): self.invalid = False if stringprep.in_table_d1(code): self.r_and_al_cat = True if stringprep.in_table_d2(code): self.l_cat = True
def resource_validator(name): """Check the *name* of a resource for some really bad characters that shouldn't be used anywhere in RestAuth. This filters names containing a slash ("/") or colon (":") and those starting with '.'. It also filters control characters etc., including those from unicode. .. deprecated:: 0.7.0 This method is deprecated in favour of the RestAuthCommon.strprep. This method will be removed in 0.7.1. :param str name: The name to validate :returns: False if the name contains any invalid characters, True otherwise. :rtype: bool """ warnings.warn( 'This method is deprecated, use RestAuthCommon.strprep.stringcheck() instead.', DeprecationWarning) if PY2 and isinstance(name, str): # pragma: py2 name = name.decode('utf-8') # filter various dangerous characters for enc_char in name: if stringprep.in_table_c12( enc_char): # C.1.2 Non-ASCII space characters return False if stringprep.in_table_c21_c22(enc_char): # C.2 Control characters return False if stringprep.in_table_c3(enc_char): # C.3 Private use return False if stringprep.in_table_c4(enc_char): # C.4 Non-character code points return False if stringprep.in_table_c5(enc_char): # C.5 Surrogate codes return False if stringprep.in_table_c6( enc_char): # C.6 Inappropriate for plain text return False if stringprep.in_table_c7( enc_char): # C.7 Inappropriate for canonical representation return False if stringprep.in_table_c8( enc_char): # C.8 Change display properties or are deprecated return False if stringprep.in_table_c9(enc_char): # C.9 Tagging characters return False return True
def canonicaliseUsername(username, ignoreSpaces = False, throws = True): # Read stringprep documentation for the meaning of the tables chars = list(username) for c, char in enumerate(chars): if stringprep.in_table_a1(char): if throws: raise ValueError else: chars[c] = u"" elif stringprep.in_table_b1(char): chars[c] = u"" else: chars[c] = stringprep.map_table_b2(char) chars = list(stringprep.unicodedata.normalize("NFKC", u"".join(chars))) for c, char in enumerate(chars): if ((not ignoreSpaces and stringprep.in_table_c11_c12(char)) or stringprep.in_table_c21_c22(char) or stringprep.in_table_c3(char) or stringprep.in_table_c4(char) or stringprep.in_table_c5(char) or stringprep.in_table_c6(char) or stringprep.in_table_c7(char) or stringprep.in_table_c8(char) or stringprep.in_table_c9(char) or stringprep.unicodedata.category(char) in ("Ps", "Pe", "Pi", "Pf", "Po")): if throws: raise ValueError else: chars[c] = u"" chars = u"".join(chars) if throws: RandAL = map(stringprep.in_table_d1, chars) for c in RandAL: if c: if filter(stringprep.in_table_d2, chars): raise ValueError if not RandAL[0] or not RandAL[-1]: raise ValueError return chars
def sals_stringprep(string): """ TODO: implements bidirectional checks https://tools.ietf.org/html/rfc3454#section-6 """ new_string = [] for x in string: if stringprep.in_table_c12(x): new_string.append(' ') elif stringprep.in_table_b1(x): continue elif stringprep.in_table_c12(x) or stringprep.in_table_c21_c22(x) or stringprep.in_table_c3(x)\ or stringprep.in_table_c4(x) or stringprep.in_table_c5(x) or stringprep.in_table_c6(x)\ or stringprep.in_table_c7(x) or stringprep.in_table_c8(x) or stringprep.in_table_c9(x): raise PDFGenericError("Invalid input character in password.") else: new_string.append(x) return unicodedata.normalize('NFKC', "".join(c for c in new_string))
def resource_validator(name): """Check the *name* of a resource for some really bad characters that shouldn't be used anywhere in RestAuth. This filters names containing a slash ("/") or colon (":") and those starting with '.'. It also filters control characters etc., including those from unicode. .. deprecated:: 0.7.0 This method is deprecated in favour of the RestAuthCommon.strprep. This method will be removed in 0.7.1. :param str name: The name to validate :returns: False if the name contains any invalid characters, True otherwise. :rtype: bool """ warnings.warn('This method is deprecated, use RestAuthCommon.strprep.stringcheck() instead.', DeprecationWarning) if PY2 and isinstance(name, str): # pragma: py2 name = name.decode('utf-8') # filter various dangerous characters for enc_char in name: if stringprep.in_table_c12(enc_char): # C.1.2 Non-ASCII space characters return False if stringprep.in_table_c21_c22(enc_char): # C.2 Control characters return False if stringprep.in_table_c3(enc_char): # C.3 Private use return False if stringprep.in_table_c4(enc_char): # C.4 Non-character code points return False if stringprep.in_table_c5(enc_char): # C.5 Surrogate codes return False if stringprep.in_table_c6(enc_char): # C.6 Inappropriate for plain text return False if stringprep.in_table_c7(enc_char): # C.7 Inappropriate for canonical representation return False if stringprep.in_table_c8(enc_char): # C.8 Change display properties or are deprecated return False if stringprep.in_table_c9(enc_char): # C.9 Tagging characters return False return True
def prohibited_output_profile(string): """RFC4013 Prohibited output profile implementation.""" # Implements: # RFC4013, 2.3. Prohibited Output. # This profile specifies the following characters as prohibited input: # - Non-ASCII space characters [StringPrep, C.1.2] # - ASCII control characters [StringPrep, C.2.1] # - Non-ASCII control characters [StringPrep, C.2.2] # - Private Use characters [StringPrep, C.3] # - Non-character code points [StringPrep, C.4] # - Surrogate code points [StringPrep, C.5] # - Inappropriate for plain text characters [StringPrep, C.6] # - Inappropriate for canonical representation characters [StringPrep, C.7] # - Change display properties or deprecated characters [StringPrep, C.8] # - Tagging characters [StringPrep, C.9] # RFC4013, 2.4. Bidirectional Characters. # RFC4013, 2.5. Unassigned Code Points. # Determine how to handle bidirectional characters (RFC3454): if is_ral_string(string): # If a string contains any RandALCat characters, # The string MUST NOT contain any LCat character: is_prohibited_bidi_ch = in_table_d2 bidi_table = 'D.2' else: # Forbid RandALCat characters in LCat string: is_prohibited_bidi_ch = in_table_d1 bidi_table = 'D.1' RFC = 'RFC4013' for c in string: # RFC4013 2.3. Prohibited Output: if in_table_c12(c): raise ValueError('%s: prohibited non-ASCII space characters ' 'that cannot be replaced (C.1.2).' % RFC) if in_table_c21_c22(c): raise ValueError('%s: prohibited control characters (C.2.1).' % RFC) if in_table_c3(c): raise ValueError('%s: prohibited private Use characters (C.3).' % RFC) if in_table_c4(c): raise ValueError( '%s: prohibited non-character code points (C.4).' % RFC) if in_table_c5(c): raise ValueError('%s: prohibited surrogate code points (C.5).' % RFC) if in_table_c6(c): raise ValueError('%s: prohibited inappropriate for plain text ' 'characters (C.6).' % RFC) if in_table_c7(c): raise ValueError('%s: prohibited inappropriate for canonical ' 'representation characters (C.7).' % RFC) if in_table_c8(c): raise ValueError('%s: prohibited change display properties / ' 'deprecated characters (C.8).' % RFC) if in_table_c9(c): raise ValueError('%s: prohibited tagging characters (C.9).' % RFC) # RFC4013, 2.4. Bidirectional Characters: if is_prohibited_bidi_ch(c): raise ValueError('%s: prohibited bidi characters (%s).' % (RFC, bidi_table)) # RFC4013, 2.5. Unassigned Code Points: if in_table_a1(c): raise ValueError('%s: prohibited unassigned code points (A.1).' % RFC)
def update_event(self, inp=-1): self.set_output_val(0, stringprep.in_table_c21_c22(self.input(0)))