فهرست منبع

Fixes #23827: Strip Unicode line breaks in header_safe (#23843)

The header_safe filter removed only ASCII control characters, but the
webhook header parser also splits lines on NEL, U+2028 and U+2029. The
filter now keeps only printable ASCII and Latin-1 characters, without
any control characters.
Martin Hauser 15 ساعت پیش
والد
کامیت
46d048d559

+ 1 - 1
docs/integrations/webhooks.md

@@ -39,7 +39,7 @@ The following data is available as context for Jinja2 templates:
 
 ### Sanitizing Header Values
 
-When rendering the `additional_headers` field, a `header_safe` filter is made available for sanitizing a value for safe inclusion in a raw HTTP header. It strips newlines and other control characters from the rendered value, preventing HTTP header (CR/LF) injection.
+When rendering the `additional_headers` field, a `header_safe` filter is made available for sanitizing a value for safe inclusion in a raw HTTP header. It strips line breaks, other control characters, and any character outside the Latin-1 character set (such as `€` or emoji) from the rendered value, preventing HTTP header injection.
 
 Whenever a header value incorporates data which may be influenced by other users (such as an object's attributes), pass it through this filter to avoid smuggling of additional headers. For example:
 

+ 1 - 1
docs/models/extras/webhook.md

@@ -58,7 +58,7 @@ The content type to indicate in the outgoing HTTP request header. See [this list
 Any additional header to include with the outgoing HTTP request. These should be defined in the format `Name: Value`, with each header on a separate line. Jinja2 templating is supported for this field.
 
 !!! warning "Sanitize interpolated header values"
-    When interpolating data which may be influenced by other users (such as object attributes) into a header value, apply the `header_safe` filter to guard against HTTP header (CR/LF) injection. This filter strips newlines and other control characters which could otherwise be used to smuggle additional headers into the request. For example:
+    When interpolating data which may be influenced by other users (such as object attributes) into a header value, apply the `header_safe` filter to guard against HTTP header injection. This filter strips line breaks and other control characters which could otherwise be used to smuggle additional headers into the request, as well as any character outside the Latin-1 character set (such as `€` or emoji). For example:
 
     ```
     X-Object-Name: {{ data.name | header_safe }}

+ 13 - 0
netbox/extras/tests/test_event_rules.py

@@ -2046,3 +2046,16 @@ class WebhookRenderHeadersTest(TestCase):
         self.assertEqual(list(headers.keys()), ['X-Object'])
         self.assertNotIn('X-Injected', headers)
         self.assertEqual(headers['X-Object'], 'legitX-Injected: evil')
+
+    @tag('regression')  # Ref: #23827
+    def test_render_headers_with_unicode_line_separators(self):
+        """Unicode line separators in a `header_safe` value must not split the rendered headers."""
+        webhook = Webhook(
+            name='Webhook 1',
+            payload_url='http://localhost:9000/',
+            additional_headers='X-Object: {{ data.name | header_safe }}\nX-Static: enabled',
+        )
+        for separator in ('\x85', '\N{LINE SEPARATOR}', '\N{PARAGRAPH SEPARATOR}'):
+            with self.subTest(separator=separator):
+                headers = webhook.render_headers({'data': {'name': f'foo{separator}bar'}})
+                self.assertEqual(headers, {'X-Object': 'foobar', 'X-Static': 'enabled'})

+ 11 - 1
netbox/extras/tests/test_models.py

@@ -1335,10 +1335,20 @@ class SanitizeHTTPHeaderFilterTestCase(TestCase):
         self.assertEqual(sanitize_http_header('legit\r\nX-Injected: evil'), 'legitX-Injected: evil')
 
     def test_strips_control_characters(self):
-        self.assertEqual(sanitize_http_header('foo\x00\x1f\x7fbar'), 'foobar')
+        self.assertEqual(sanitize_http_header('foo\x00\x0b\x0c\x1c\x1d\x1e\x1f\x7f\x80\x85\x9fbar'), 'foobar')
+
+    def test_strips_non_latin1_characters(self):
+        value = 'foo\N{LATIN CAPITAL LIGATURE OE}\N{LINE SEPARATOR}\N{PARAGRAPH SEPARATOR}\N{EURO SIGN}\U0001f600bar'
+        self.assertEqual(sanitize_http_header(value), 'foobar')
 
     def test_preserves_normal_value(self):
         self.assertEqual(sanitize_http_header('application/json'), 'application/json')
+        self.assertEqual(sanitize_http_header('München'), 'München')
+
+    def test_preserves_allowed_characters(self):
+        value = ''.join(chr(i) for i in range(0x20, 0x7f))
+        value += ''.join(chr(i) for i in range(0xa0, 0x100))
+        self.assertEqual(sanitize_http_header(value), value)
 
     def test_coerces_non_string(self):
         self.assertEqual(sanitize_http_header(42), '42')

+ 4 - 6
netbox/utilities/jinja2.py

@@ -24,9 +24,8 @@ __all__ = (
     'validate_jinja2_syntax',
 )
 
-# Control characters (C0 range plus DEL) which are invalid in an HTTP header value. Notably, this includes the
-# carriage return and line feed characters used to smuggle additional headers (CR/LF injection).
-HTTP_HEADER_INVALID_CHARS_RE = re.compile(r'[\x00-\x1f\x7f]')
+# HTTP header values are encoded as Latin-1 by the HTTP client. Exclude all control characters.
+HTTP_HEADER_INVALID_CHARS_RE = re.compile(r'[^\x20-\x7e\xa0-\xff]')
 
 # Matches the start of a Jinja2 expression, statement, or comment ({{, {%, {#), to detect whether a
 # template-capable field (e.g. Webhook.payload_url) is being used as a literal value or a template.
@@ -47,9 +46,8 @@ def env_filter(name):
 
 def sanitize_http_header(value):
     """
-    Jinja2 filter which sanitizes a value for safe inclusion in a raw HTTP header by stripping newlines and other
-    control characters. This guards against HTTP header (CR/LF) injection when interpolating untrusted data (e.g.
-    user-controlled object attributes) into a webhook's additional headers.
+    Jinja2 filter which removes control characters and characters outside the Latin-1 character set from HTTP
+    header values. This prevents header injection when interpolating untrusted data into webhook headers.
     """
     return HTTP_HEADER_INVALID_CHARS_RE.sub('', str(value))