|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | +# contributors may be used to endorse or promote products derived |
| 14 | +# from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +import socket |
| 29 | +import unittest |
| 30 | + |
| 31 | + |
| 32 | +class HTTPRequestManyChunksTest(unittest.TestCase): |
| 33 | + def setUp(self): |
| 34 | + self._model_name = "simple" |
| 35 | + self._local_host = "localhost" |
| 36 | + self._http_port = 8000 |
| 37 | + self._malicious_chunk_count = ( |
| 38 | + 1000000 # large enough to cause a stack overflow if using alloca() |
| 39 | + ) |
| 40 | + self._parse_error = ( |
| 41 | + "failed to parse the request JSON buffer: Invalid value. at 0" |
| 42 | + ) |
| 43 | + |
| 44 | + def send_chunked_request( |
| 45 | + self, header: str, chunk_count: int, expected_response: str |
| 46 | + ): |
| 47 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 48 | + header = ( |
| 49 | + f"{header}" |
| 50 | + f"Host: {self._local_host}:{self._http_port}\r\n" |
| 51 | + f"Content-Type: application/octet-stream\r\n" |
| 52 | + f"Transfer-Encoding: chunked\r\n" |
| 53 | + f"Connection: close\r\n" |
| 54 | + f"\r\n" |
| 55 | + ) |
| 56 | + try: |
| 57 | + s.connect((self._local_host, self._http_port)) |
| 58 | + # HTTP request with chunked encoding |
| 59 | + s.sendall((header.encode())) |
| 60 | + |
| 61 | + # Send chunked payload |
| 62 | + for _ in range(chunk_count): |
| 63 | + s.send(b"1\r\nA\r\n") |
| 64 | + # End chunked encoding |
| 65 | + s.sendall(b"0\r\n\r\n") |
| 66 | + |
| 67 | + # Receive response |
| 68 | + response = b"" |
| 69 | + while True: |
| 70 | + try: |
| 71 | + chunk = s.recv(4096) |
| 72 | + if not chunk: |
| 73 | + break |
| 74 | + response += chunk |
| 75 | + except socket.timeout: |
| 76 | + break |
| 77 | + self.assertIn(expected_response, response.decode()) |
| 78 | + except Exception as e: |
| 79 | + raise (e) |
| 80 | + finally: |
| 81 | + s.close() |
| 82 | + |
| 83 | + def test_infer(self): |
| 84 | + request_header = ( |
| 85 | + f"POST /v2/models/{self._model_name}/infer HTTP/1.1\r\n" |
| 86 | + f"Inference-Header-Content-Length: 0\r\n" |
| 87 | + ) |
| 88 | + |
| 89 | + self.send_chunked_request( |
| 90 | + request_header, |
| 91 | + self._malicious_chunk_count, |
| 92 | + "Raw request must only have 1 input (found 1) to be deduced but got 2 inputs in 'simple' model configuration", |
| 93 | + ) |
| 94 | + |
| 95 | + def test_registry_index(self): |
| 96 | + request_header = f"POST /v2/repository/index HTTP/1.1\r\n" |
| 97 | + |
| 98 | + self.send_chunked_request( |
| 99 | + request_header, self._malicious_chunk_count, self._parse_error |
| 100 | + ) |
| 101 | + |
| 102 | + def test_model_control(self): |
| 103 | + load_request_header = ( |
| 104 | + f"POST /v2/repository/models/{self._model_name}/load HTTP/1.1\r\n" |
| 105 | + ) |
| 106 | + unload_request_header = load_request_header.replace("/load", "/unload") |
| 107 | + |
| 108 | + self.send_chunked_request( |
| 109 | + load_request_header, self._malicious_chunk_count, self._parse_error |
| 110 | + ) |
| 111 | + self.send_chunked_request( |
| 112 | + unload_request_header, self._malicious_chunk_count, self._parse_error |
| 113 | + ) |
| 114 | + |
| 115 | + def test_trace(self): |
| 116 | + request_header = ( |
| 117 | + f"POST /v2/models/{self._model_name}/trace/setting HTTP/1.1\r\n" |
| 118 | + ) |
| 119 | + |
| 120 | + self.send_chunked_request( |
| 121 | + request_header, self._malicious_chunk_count, self._parse_error |
| 122 | + ) |
| 123 | + |
| 124 | + def test_logging(self): |
| 125 | + request_header = f"POST /v2/logging HTTP/1.1\r\n" |
| 126 | + |
| 127 | + self.send_chunked_request( |
| 128 | + request_header, self._malicious_chunk_count, self._parse_error |
| 129 | + ) |
| 130 | + |
| 131 | + def test_system_shm_register(self): |
| 132 | + request_header = f"POST /v2/systemsharedmemory/region/test_system_shm_register/register HTTP/1.1\r\n" |
| 133 | + |
| 134 | + self.send_chunked_request( |
| 135 | + request_header, self._malicious_chunk_count, self._parse_error |
| 136 | + ) |
| 137 | + |
| 138 | + def test_cuda_shm_register(self): |
| 139 | + request_header = f"POST /v2/cudasharedmemory/region/test_cuda_shm_register/register HTTP/1.1\r\n" |
| 140 | + |
| 141 | + self.send_chunked_request( |
| 142 | + request_header, self._malicious_chunk_count, self._parse_error |
| 143 | + ) |
| 144 | + |
| 145 | + def test_generate(self): |
| 146 | + request_header = f"POST /v2/models/{self._model_name}/generate HTTP/1.1\r\n" |
| 147 | + self.send_chunked_request( |
| 148 | + request_header, self._malicious_chunk_count, self._parse_error |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + unittest.main() |
0 commit comments