You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "moonchen (via GitHub)" <gi...@apache.org> on 2025/10/13 23:49:43 UTC

[PR] [header_rewrite] Fix set-body [trafficserver]

moonchen opened a new pull request, #12560:
URL: https://github.com/apache/trafficserver/pull/12560

   1. set-body with an empty string argument causes heap corruption in The cause is incorrect handling of zero-length strings in MIOBuffer::append_xmalloced.
   2. set-body with variable expansion fails to expand the variable. The cause is a typo in the OperatorSetBody::exec() method.
   
   Add autest for these bugs.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "cmcfarlen (via GitHub)" <gi...@apache.org>.
cmcfarlen commented on PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#issuecomment-3513523630

   Cherry-picked to 10.1.x branch


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen commented on code in PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#discussion_r2429951294


##########
tests/gold_tests/pluginTest/header_rewrite/header_rewrite_set_body.test.py:
##########
@@ -0,0 +1,111 @@
+'''
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+Test.Summary = '''
+Test for empty set-body operator value.
+'''
+
+Test.ContinueOnFail = True
+
+
+class HeaderRewriteSetBodyTest:
+
+    def __init__(self):
+        self.setUpOriginServer()
+        self.setUpTS()
+
+    def setUpOriginServer(self):
+        self.server = Test.MakeOriginServer("server")
+
+        # Response for original transaction
+        request_header = {"headers": "GET /test HTTP/1.1\r\nHost: www.example.com\r\n\r\n", "body": ""}
+
+        response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "body": "ATS should not serve this body"}
+        self.server.addResponse("sessionlog.log", request_header, response_header)
+
+    def setUpTS(self):
+        self.ts = Test.MakeATSProcess("ts")
+        empty_body_rule_file = Test.Disk.File("empty_body_rule.conf", "txt", "")
+        empty_body_rule_file.WriteOn(
+            '''
+                cond %{REMAP_PSEUDO_HOOK}
+                    set-status 200
+
+                cond %{SEND_RESPONSE_HDR_HOOK}
+                    set-body ""
+            ''')
+        self.ts.Disk.remap_config.AddLine(
+            f'map http://www.example.com/emptybody http://127.0.0.1:{self.server.Variables.Port}/test @plugin=header_rewrite.so @pparam={empty_body_rule_file.AbsRunTimePath}'
+        )
+
+        set_body_rule_file = Test.Disk.File("set_body_rule.conf", "txt", "")
+        set_body_rule_file.WriteOn(
+            '''
+                cond %{REMAP_PSEUDO_HOOK}
+                    set-status 200
+
+                cond %{SEND_RESPONSE_HDR_HOOK}
+                    set-body "%{STATUS}"
+            ''')
+        self.ts.Disk.remap_config.AddLine(
+            f'map http://www.example.com/setbody http://127.0.0.1:{self.server.Variables.Port}/test @plugin=header_rewrite.so @pparam={set_body_rule_file.AbsRunTimePath}'
+        )
+
+        # Enable debug logging to help diagnose issues
+        self.ts.Disk.records_config.update(
+            {
+                'proxy.config.diags.debug.enabled': 1,
+                'proxy.config.diags.debug.tags': 'http|header_rewrite',
+            })
+
+    def test_empty_body(self):
+        '''
+        Test that empty set-body doesn't crash the server and properly deletes internal error body
+        '''
+        tr = Test.AddTestRun()
+        tr.MakeCurlCommand(f'-s -v --proxy 127.0.0.1:{self.ts.Variables.port} "http://www.example.com/emptybody"', ts=self.ts)
+        tr.Processes.Default.ReturnCode = 0
+        tr.Processes.Default.StartBefore(self.server)
+        tr.Processes.Default.StartBefore(self.ts)
+        tr.Processes.Default.Streams.stderr.Content = Testers.ContainsExpression("200 OK", "expected 200 response")
+        tr.Processes.Default.Streams.stderr.Content += Testers.ContainsExpression("Content-Length: 0", "expected content-length 0")
+        tr.Processes.Default.Streams.stdout.Content = Testers.ExcludesExpression("should not", "body should be removed")
+
+        tr.StillRunningAfter = self.ts  # Verify server didn't crash
+
+    def test_set_body(self):
+        '''
+        Test that set-body with a variable works correctly
+        '''
+        tr = Test.AddTestRun()
+        tr.MakeCurlCommand(f'-s -v --proxy 127.0.0.1:{self.ts.Variables.port} "http://www.example.com/setbody"', ts=self.ts)
+        tr.Processes.Default.ReturnCode = 0
+        tr.Processes.Default.StartBefore(self.server)
+        tr.Processes.Default.StartBefore(self.ts)
+        tr.Processes.Default.Streams.stderr.Content = Testers.ContainsExpression("200 OK", "expected 200 response")
+        tr.Processes.Default.Streams.stderr.Content += Testers.ContainsExpression("Content-Length: 3", "expected content-length 3")
+        tr.Processes.Default.Streams.stdout.Content = Testers.ContainsExpression("200", "body should be set to 200")

Review Comment:
   added



##########
include/iocore/eventsystem/IOBuffer.h:
##########
@@ -102,8 +102,16 @@ enum AllocType {
 #define BUFFER_SIZE_INDEX_IS_FAST_ALLOCATED(_size_index) (((uint64_t)_size_index) < DEFAULT_BUFFER_SIZES)
 #define BUFFER_SIZE_INDEX_IS_CONSTANT(_size_index)       (_size_index >= DEFAULT_BUFFER_SIZES)
 
-#define BUFFER_SIZE_FOR_XMALLOC(_size)            (-(_size))
-#define BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(_size) (-(_size))
+#define BUFFER_SIZE_FOR_XMALLOC(_size) (-(_size))
+inline int64_t
+BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(int64_t size)
+{
+  // Positive size indices are interpreted as a BUFFER_SIZE_INDEX_*.
+  // Negative size indices are interpreted as a malloc size.
+  // A zero size index is BUFFER_SIZE_INDEX_128, which causes this buffer to be freed incorrectly.
+  ink_release_assert(size > 0 && "Zero-length xmalloc buffer causes heap corruption!");
+  return -size;
+}

Review Comment:
   added



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen commented on PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#issuecomment-3403056964

   [approve ci autest 0]


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen commented on code in PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#discussion_r2430049280


##########
include/iocore/eventsystem/IOBuffer.h:
##########
@@ -102,8 +102,16 @@ enum AllocType {
 #define BUFFER_SIZE_INDEX_IS_FAST_ALLOCATED(_size_index) (((uint64_t)_size_index) < DEFAULT_BUFFER_SIZES)
 #define BUFFER_SIZE_INDEX_IS_CONSTANT(_size_index)       (_size_index >= DEFAULT_BUFFER_SIZES)
 
-#define BUFFER_SIZE_FOR_XMALLOC(_size)            (-(_size))
-#define BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(_size) (-(_size))
+#define BUFFER_SIZE_FOR_XMALLOC(_size) (-(_size))
+inline int64_t
+BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(int64_t size)
+{
+  // Positive size indices are interpreted as a BUFFER_SIZE_INDEX_*.
+  // Negative size indices are interpreted as a malloc size.
+  // A zero size index is BUFFER_SIZE_INDEX_128, which causes this buffer to be freed incorrectly.
+  ink_release_assert(size > 0 && "Zero-length xmalloc buffer causes heap corruption!");
+  return -size;
+}

Review Comment:
   inline is needed here to prevent ODR violation since the definition is in a header file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen commented on PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#issuecomment-3403054630

   approve ci autest


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "bneradt (via GitHub)" <gi...@apache.org>.
bneradt commented on code in PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#discussion_r2429675075


##########
include/iocore/eventsystem/IOBuffer.h:
##########
@@ -102,8 +102,16 @@ enum AllocType {
 #define BUFFER_SIZE_INDEX_IS_FAST_ALLOCATED(_size_index) (((uint64_t)_size_index) < DEFAULT_BUFFER_SIZES)
 #define BUFFER_SIZE_INDEX_IS_CONSTANT(_size_index)       (_size_index >= DEFAULT_BUFFER_SIZES)
 
-#define BUFFER_SIZE_FOR_XMALLOC(_size)            (-(_size))
-#define BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(_size) (-(_size))
+#define BUFFER_SIZE_FOR_XMALLOC(_size) (-(_size))
+inline int64_t
+BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(int64_t size)
+{
+  // Positive size indices are interpreted as a BUFFER_SIZE_INDEX_*.
+  // Negative size indices are interpreted as a malloc size.
+  // A zero size index is BUFFER_SIZE_INDEX_128, which causes this buffer to be freed incorrectly.
+  ink_release_assert(size > 0 && "Zero-length xmalloc buffer causes heap corruption!");
+  return -size;
+}

Review Comment:
   I've generally had the feedback to avoid inline for new functions.
   
   Also, can this be `constexpr`?



##########
tests/gold_tests/pluginTest/header_rewrite/header_rewrite_set_body.test.py:
##########
@@ -0,0 +1,111 @@
+'''
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+Test.Summary = '''
+Test for empty set-body operator value.
+'''
+
+Test.ContinueOnFail = True
+
+
+class HeaderRewriteSetBodyTest:
+
+    def __init__(self):
+        self.setUpOriginServer()
+        self.setUpTS()
+
+    def setUpOriginServer(self):
+        self.server = Test.MakeOriginServer("server")
+
+        # Response for original transaction
+        request_header = {"headers": "GET /test HTTP/1.1\r\nHost: www.example.com\r\n\r\n", "body": ""}
+
+        response_header = {"headers": "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n", "body": "ATS should not serve this body"}
+        self.server.addResponse("sessionlog.log", request_header, response_header)
+
+    def setUpTS(self):
+        self.ts = Test.MakeATSProcess("ts")
+        empty_body_rule_file = Test.Disk.File("empty_body_rule.conf", "txt", "")
+        empty_body_rule_file.WriteOn(
+            '''
+                cond %{REMAP_PSEUDO_HOOK}
+                    set-status 200
+
+                cond %{SEND_RESPONSE_HDR_HOOK}
+                    set-body ""
+            ''')
+        self.ts.Disk.remap_config.AddLine(
+            f'map http://www.example.com/emptybody http://127.0.0.1:{self.server.Variables.Port}/test @plugin=header_rewrite.so @pparam={empty_body_rule_file.AbsRunTimePath}'
+        )
+
+        set_body_rule_file = Test.Disk.File("set_body_rule.conf", "txt", "")
+        set_body_rule_file.WriteOn(
+            '''
+                cond %{REMAP_PSEUDO_HOOK}
+                    set-status 200
+
+                cond %{SEND_RESPONSE_HDR_HOOK}
+                    set-body "%{STATUS}"
+            ''')
+        self.ts.Disk.remap_config.AddLine(
+            f'map http://www.example.com/setbody http://127.0.0.1:{self.server.Variables.Port}/test @plugin=header_rewrite.so @pparam={set_body_rule_file.AbsRunTimePath}'
+        )
+
+        # Enable debug logging to help diagnose issues
+        self.ts.Disk.records_config.update(
+            {
+                'proxy.config.diags.debug.enabled': 1,
+                'proxy.config.diags.debug.tags': 'http|header_rewrite',
+            })
+
+    def test_empty_body(self):
+        '''
+        Test that empty set-body doesn't crash the server and properly deletes internal error body
+        '''
+        tr = Test.AddTestRun()
+        tr.MakeCurlCommand(f'-s -v --proxy 127.0.0.1:{self.ts.Variables.port} "http://www.example.com/emptybody"', ts=self.ts)
+        tr.Processes.Default.ReturnCode = 0
+        tr.Processes.Default.StartBefore(self.server)
+        tr.Processes.Default.StartBefore(self.ts)
+        tr.Processes.Default.Streams.stderr.Content = Testers.ContainsExpression("200 OK", "expected 200 response")
+        tr.Processes.Default.Streams.stderr.Content += Testers.ContainsExpression("Content-Length: 0", "expected content-length 0")
+        tr.Processes.Default.Streams.stdout.Content = Testers.ExcludesExpression("should not", "body should be removed")
+
+        tr.StillRunningAfter = self.ts  # Verify server didn't crash
+
+    def test_set_body(self):
+        '''
+        Test that set-body with a variable works correctly
+        '''
+        tr = Test.AddTestRun()
+        tr.MakeCurlCommand(f'-s -v --proxy 127.0.0.1:{self.ts.Variables.port} "http://www.example.com/setbody"', ts=self.ts)
+        tr.Processes.Default.ReturnCode = 0
+        tr.Processes.Default.StartBefore(self.server)
+        tr.Processes.Default.StartBefore(self.ts)
+        tr.Processes.Default.Streams.stderr.Content = Testers.ContainsExpression("200 OK", "expected 200 response")
+        tr.Processes.Default.Streams.stderr.Content += Testers.ContainsExpression("Content-Length: 3", "expected content-length 3")
+        tr.Processes.Default.Streams.stdout.Content = Testers.ContainsExpression("200", "body should be set to 200")

Review Comment:
   Maybe just to be safe include the ExcludesExpression of `should not` from above?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen commented on code in PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#discussion_r2430049280


##########
include/iocore/eventsystem/IOBuffer.h:
##########
@@ -102,8 +102,16 @@ enum AllocType {
 #define BUFFER_SIZE_INDEX_IS_FAST_ALLOCATED(_size_index) (((uint64_t)_size_index) < DEFAULT_BUFFER_SIZES)
 #define BUFFER_SIZE_INDEX_IS_CONSTANT(_size_index)       (_size_index >= DEFAULT_BUFFER_SIZES)
 
-#define BUFFER_SIZE_FOR_XMALLOC(_size)            (-(_size))
-#define BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(_size) (-(_size))
+#define BUFFER_SIZE_FOR_XMALLOC(_size) (-(_size))
+inline int64_t
+BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(int64_t size)
+{
+  // Positive size indices are interpreted as a BUFFER_SIZE_INDEX_*.
+  // Negative size indices are interpreted as a malloc size.
+  // A zero size index is BUFFER_SIZE_INDEX_128, which causes this buffer to be freed incorrectly.
+  ink_release_assert(size > 0 && "Zero-length xmalloc buffer causes heap corruption!");
+  return -size;
+}

Review Comment:
   inline is needed here to prevent ODR violation since the definition is in a header file.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "cmcfarlen (via GitHub)" <gi...@apache.org>.
cmcfarlen commented on code in PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560#discussion_r2449789527


##########
include/iocore/eventsystem/IOBuffer.h:
##########
@@ -102,8 +102,16 @@ enum AllocType {
 #define BUFFER_SIZE_INDEX_IS_FAST_ALLOCATED(_size_index) (((uint64_t)_size_index) < DEFAULT_BUFFER_SIZES)
 #define BUFFER_SIZE_INDEX_IS_CONSTANT(_size_index)       (_size_index >= DEFAULT_BUFFER_SIZES)
 
-#define BUFFER_SIZE_FOR_XMALLOC(_size)            (-(_size))
-#define BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(_size) (-(_size))
+#define BUFFER_SIZE_FOR_XMALLOC(_size) (-(_size))
+inline int64_t
+BUFFER_SIZE_INDEX_FOR_XMALLOC_SIZE(int64_t size)
+{
+  // Positive size indices are interpreted as a BUFFER_SIZE_INDEX_*.
+  // Negative size indices are interpreted as a malloc size.
+  // A zero size index is BUFFER_SIZE_INDEX_128, which causes this buffer to be freed incorrectly.
+  ink_release_assert(size > 0 && "Zero-length xmalloc buffer causes heap corruption!");
+  return -size;
+}

Review Comment:
   @bneradt why avoid inline?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [header_rewrite] Fix set-body [trafficserver]

Posted by "moonchen (via GitHub)" <gi...@apache.org>.
moonchen merged PR #12560:
URL: https://github.com/apache/trafficserver/pull/12560


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org