Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5371e2a
limit total array size
mattwittwer Apr 28, 2025
087ad05
Add customizable http input size limit
mattwittwer May 2, 2025
a96f0e1
remove debug statements
mattwittwer May 2, 2025
efb4e94
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer May 12, 2025
d5a44c8
update error message and include size test
mattwittwer May 12, 2025
33d4a63
remove max_input_size check from generate handler
mattwittwer May 12, 2025
a271aed
update sagemaker and vertex_ai endpoints
mattwittwer May 19, 2025
35e1cb6
created new tests for max http input size
mattwittwer May 22, 2025
97a32fb
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer May 22, 2025
7fdbbba
remove unused tests
mattwittwer May 22, 2025
45d4d36
add parameter tests
mattwittwer May 23, 2025
e825e2c
revert changes to vertex ai and sagemaker endpoints
mattwittwer May 23, 2025
a595139
fix constructor
mattwittwer May 23, 2025
403654a
spelling fix
mattwittwer May 23, 2025
f2882a7
whitespace fix
mattwittwer May 29, 2025
27ead96
Merge branch 'main' into mwittwer/shape_array_vulnerability
mattwittwer May 30, 2025
37c1c73
improve test case readability
mattwittwer May 31, 2025
64008c4
update test for int values
mattwittwer Jun 2, 2025
eafc2e6
update input to take values as MB
mattwittwer Jun 2, 2025
cdee641
update comment
mattwittwer Jun 2, 2025
730f963
restore byte size input values
mattwittwer Jun 2, 2025
66407f4
Merge remote-tracking branch 'origin/main' into mwittwer/shape_array_…
mattwittwer Jun 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
365 changes: 365 additions & 0 deletions qa/L0_http/http_input_size_limit_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,365 @@
#!/usr/bin/python
# Copyright 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import sys

sys.path.append("../common")

import unittest

import numpy as np
import requests
import test_util as tu

# Constants for size calculations
# Each FP32 value is 4 bytes, so we need to divide target byte sizes by 4 to get element counts
BYTES_PER_FP32 = 4
MB = 2**20 # 1 MB = 1,048,576 bytes
DEFAULT_LIMIT_BYTES = 64 * MB # 64MB default limit
INCREASED_LIMIT_BYTES = 128 * MB # 128MB increased limit

# Calculate element counts for size limits
DEFAULT_LIMIT_ELEMENTS = DEFAULT_LIMIT_BYTES // BYTES_PER_FP32 # 16,777,216 elements
INCREASED_LIMIT_ELEMENTS = (
INCREASED_LIMIT_BYTES // BYTES_PER_FP32
) # 33,554,432 elements

# Small offsets to go just over/under the limits
OFFSET_ELEMENTS = 32


class InferSizeLimitTest(tu.TestResultCollector):
def _get_infer_url(self, model_name):
return "http://localhost:8000/v2/models/{}/infer".format(model_name)

def test_default_limit_rejection_raw_binary(self):
"""Test raw binary inputs with default limit"""
model = "onnx_zero_1_float32"

# Test case 1: Input just over the 64MB limit (should fail)
# (2^24 + 32) elements * 4 bytes = 64MB + 128 bytes = 67,108,992 bytes
large_input = np.ones(
DEFAULT_LIMIT_ELEMENTS + OFFSET_ELEMENTS, dtype=np.float32
)
input_bytes = large_input.tobytes()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests might be a bit easier to maintain if you added something like this to these test cases:

# Constant at top
MB = 2**20

# ...
# at each "just over" or "just under" test case

assert input_bytes.size() > 64*MB
# ...
assert input_bytes.size() < 64*MB
# ...
assert input_bytes.size() > 128*MB
# ...
assert input_bytes.size() < 128*MB

Right now, it's a bit hard to read something like 224 (* 4 bytes per element) + 32 > 226, etc. because of the implicit 4 bytes per element adding 2 to the exponent for comparison

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Added in a test case for each input to validate the input size is larger/smaller than the limit as expected for each test:
https://github.com/triton-inference-server/server/blob/mwittwer/shape_array_vulnerability/qa/L0_http/http_input_size_limit_test.py#L67

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added a section for constants to be defined in one location. This reduces the number of separately defined values used in the tests:
https://github.com/triton-inference-server/server/blob/mwittwer/shape_array_vulnerability/qa/L0_http/http_input_size_limit_test.py#L38-L50

assert len(input_bytes) > 64 * MB # Verify we're actually over the 64MB limit

headers = {"Inference-Header-Content-Length": "0"}
response = requests.post(
self._get_infer_url(model), data=input_bytes, headers=headers
)

# Should fail with 400 bad request with default limit
self.assertEqual(
400,
response.status_code,
"Expected error code for oversized request, got: {}".format(
response.status_code
),
)

# Verify error message contains size limit info
error_msg = response.content.decode()
self.assertIn(
"exceeds the maximum allowed value",
error_msg,
"Expected error message about exceeding max input size",
)

# Test case 2: Input just under the 64MB limit (should succeed)
# (2^24 - 32) elements * 4 bytes = 64MB - 128 bytes = 67,108,736 bytes
small_input = np.ones(
DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS, dtype=np.float32
)
input_bytes = small_input.tobytes()
assert len(input_bytes) < 64 * MB # Verify we're actually under the 64MB limit

response = requests.post(
self._get_infer_url(model), data=input_bytes, headers=headers
)

# Should succeed with 200 OK
self.assertEqual(
200,
response.status_code,
"Expected success code for request within size limit, got: {}".format(
response.status_code
),
)

# Verify output matches our input (identity model)
header_size = int(response.headers["Inference-Header-Content-Length"])
output_data = response.content[header_size:]

# Convert output bytes back to numpy array for comparison
output_array = np.frombuffer(output_data, dtype=np.float32)
self.assertTrue(
np.array_equal(output_array, small_input),
"Response data does not match input data",
)

def test_default_limit_rejection_json(self):
"""Test JSON inputs with default limit"""
model = "onnx_zero_1_float32"

# Test case 1: Input just over the 64MB limit (should fail)
# (2^24 + 32) elements * 4 bytes = 64MB + 128 bytes = 67,108,992 bytes
shape_size = DEFAULT_LIMIT_ELEMENTS + OFFSET_ELEMENTS

payload = {
"inputs": [
{
"name": "INPUT0",
"datatype": "FP32",
"shape": [1, shape_size],
"data": [1.0] * shape_size,
}
]
}
assert (
shape_size * BYTES_PER_FP32 > 64 * MB
) # Verify we're actually over the 64MB limit

headers = {"Content-Type": "application/json"}
response = requests.post(
self._get_infer_url(model), headers=headers, json=payload
)

# Should fail with 400 bad request with default limit
self.assertEqual(
400,
response.status_code,
"Expected error code for oversized JSON request, got: {}".format(
response.status_code
),
)

# Verify error message contains size limit info
error_msg = response.content.decode()
self.assertIn(
"exceeds the maximum allowed value",
error_msg,
"Expected error message about exceeding max input size",
)

# Test case 2: Input just under the 64MB limit (should succeed)
# (2^24 - 32) elements * 4 bytes = 64MB - 128 bytes = 67,108,736 bytes
shape_size = DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS

payload = {
"inputs": [
{
"name": "INPUT0",
"datatype": "FP32",
"shape": [1, shape_size],
"data": [1.0] * shape_size,
}
]
}
assert (
shape_size * BYTES_PER_FP32 < 64 * MB
) # Verify we're actually under the 64MB limit

response = requests.post(
self._get_infer_url(model), headers=headers, json=payload
)

# Should succeed with 200 OK
self.assertEqual(
200,
response.status_code,
"Expected success code for JSON request within size limit, got: {}".format(
response.status_code
),
)

# Verify we got a valid response
result = response.json()
self.assertIn("outputs", result, "Response missing outputs field")
self.assertEqual(1, len(result["outputs"]), "Expected 1 output")
self.assertEqual(
shape_size,
result["outputs"][0]["shape"][1],
f"Expected shape {[1, shape_size]}, got {result['outputs'][0]['shape']}",
)

def test_large_input_raw_binary(self):
"""Test raw binary input larger with custom limit set"""
model = "onnx_zero_1_float32"

# Test case 1: Input just over the 128MB configured limit (should fail)
# (2^25 + 32) elements * 4 bytes = 128MB + 128 bytes = 134,217,856 bytes
large_input = np.ones(
INCREASED_LIMIT_ELEMENTS + OFFSET_ELEMENTS, dtype=np.float32
)
input_bytes = large_input.tobytes()
assert len(input_bytes) > 128 * MB # Verify we're actually over the 128MB limit

headers = {"Inference-Header-Content-Length": "0"}
response = requests.post(
self._get_infer_url(model), data=input_bytes, headers=headers
)

# Should fail with 400 bad request with our increased limit
self.assertEqual(
400,
response.status_code,
"Expected error code for oversized request, got: {}".format(
response.status_code
),
)

# Verify error message contains size limit info
error_msg = response.content.decode()
self.assertIn(
"exceeds the maximum allowed value",
error_msg,
"Expected error message about exceeding max input size",
)

# Test case 2: Input just under the 128MB configured limit (should succeed)
# (2^25 - 32) elements * 4 bytes = 128MB - 128 bytes = 134,217,600 bytes
small_input = np.ones(
INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS, dtype=np.float32
)
input_bytes = small_input.tobytes()
assert (
len(input_bytes) < 128 * MB
) # Verify we're actually under the 128MB limit

response = requests.post(
self._get_infer_url(model), data=input_bytes, headers=headers
)

# Should succeed with 200 OK
self.assertEqual(
200,
response.status_code,
"Expected success code for request within increased limit, got: {}".format(
response.status_code
),
)

# Verify output matches our input (identity model)
header_size = int(response.headers["Inference-Header-Content-Length"])
output_data = response.content[header_size:]

# Convert output bytes back to numpy array for comparison
output_array = np.frombuffer(output_data, dtype=np.float32)
self.assertTrue(
np.array_equal(output_array, small_input),
"Response data does not match input data",
)

def test_large_input_json(self):
"""Test JSON input larger with custom limit set"""
model = "onnx_zero_1_float32"

# Test case 1: Input just over the 128MB configured limit (should fail)
# (2^25 + 32) elements * 4 bytes = 128MB + 128 bytes = 134,217,856 bytes
shape_size = INCREASED_LIMIT_ELEMENTS + OFFSET_ELEMENTS

payload = {
"inputs": [
{
"name": "INPUT0",
"datatype": "FP32",
"shape": [1, shape_size],
"data": [1.0] * shape_size,
}
]
}
assert (
shape_size * BYTES_PER_FP32 > 128 * MB
) # Verify we're actually over the 128MB limit

headers = {"Content-Type": "application/json"}
response = requests.post(
self._get_infer_url(model), headers=headers, json=payload
)

# Should fail with 400 bad request with our increased limit
self.assertEqual(
400,
response.status_code,
"Expected error code for oversized JSON request, got: {}".format(
response.status_code
),
)

# Verify error message contains size limit info
error_msg = response.content.decode()
self.assertIn(
"exceeds the maximum allowed value",
error_msg,
"Expected error message about exceeding max input size",
)

# Test case 2: Input just under the 128MB configured limit (should succeed)
# (2^25 - 32) elements * 4 bytes = 128MB - 128 bytes = 134,217,600 bytes
shape_size = INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS

payload = {
"inputs": [
{
"name": "INPUT0",
"datatype": "FP32",
"shape": [1, shape_size],
"data": [1.0] * shape_size,
}
]
}
assert (
shape_size * BYTES_PER_FP32 < 128 * MB
) # Verify we're actually under the 128MB limit

response = requests.post(
self._get_infer_url(model), headers=headers, json=payload
)

# Should succeed with 200 OK
self.assertEqual(
200,
response.status_code,
"Expected success code for request within increased limit, got: {}".format(
response.status_code
),
)

# Verify we got a valid response
result = response.json()
self.assertIn("outputs", result, "Response missing outputs field")
self.assertEqual(1, len(result["outputs"]), "Expected 1 output")
self.assertEqual(
shape_size,
result["outputs"][0]["shape"][1],
f"Expected shape {[1, shape_size]}, got {result['outputs'][0]['shape']}",
)


if __name__ == "__main__":
unittest.main()
Loading
Loading