1212logger = logging .getLogger (__name__ )
1313
1414
15+ def _reject_line_breaks (field_name : str , value : str ) -> None :
16+ """
17+ Reject values that would break the one-option-per-line config format.
18+
19+ :param field_name: The name of the configuration field being validated.
20+ :type field_name: str
21+ :param value: The value of the configuration field to validate.
22+ :type value: str
23+ :raises ValueError: If the value contains line break characters or ends with an odd number of backslashes.
24+ """
25+ if "\n " in value or "\r " in value :
26+ err = f"config { field_name } cannot contain line breaks"
27+ raise ValueError (err )
28+
29+ trailing_backslashes = len (value ) - len (value .rstrip ("\\ " ))
30+ if trailing_backslashes % 2 == 1 :
31+ err = f"config { field_name } cannot end with an odd number of backslashes"
32+ raise ValueError (err )
33+
34+
1535@dataclass (frozen = True )
1636class OptionSpec :
1737 """
@@ -176,14 +196,17 @@ def _encode_config(config: Dict[str, Any]) -> Dict[str, str]:
176196 logger .debug ("Encoding LanguageTool config with keys: %s" , list (config .keys ()))
177197 encoded : Dict [str , str ] = {}
178198 for key , value in config .items ():
199+ _reject_line_breaks ("key" , key )
179200 if _is_lang_key (key ) and key .count ("-" ) == 1 : # lang-<code>
180201 logger .debug ("Encoding language option %s=%r" , key , value )
181202 encoded [key ] = str (value )
203+ _reject_line_breaks (key , encoded [key ])
182204 continue
183205 if _is_lang_key (key ) and key .count ("-" ) == 2 : # lang-<code>-dictPath
184206 logger .debug ("Encoding language dictPath %s=%r" , key , value )
185207 _path_validator (value )
186208 encoded [key ] = _path_encoder (value )
209+ _reject_line_breaks (key , encoded [key ])
187210 continue
188211
189212 spec = CONFIG_SCHEMA .get (key )
@@ -197,6 +220,7 @@ def _encode_config(config: Dict[str, Any]) -> Dict[str, str]:
197220 if spec .validator is not None :
198221 spec .validator (value )
199222 encoded [key ] = spec .encoder (value )
223+ _reject_line_breaks (key , encoded [key ])
200224 return encoded
201225
202226
0 commit comments