Skip to content

Commit af6c01a

Browse files
fix: input validation DoS and L1 cache correctness
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: shenald-dev <245350826+shenald-dev@users.noreply.github.com>
1 parent 4dbc052 commit af6c01a

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const ERROR_PAYLOAD_TOO_LARGE = Buffer.from(JSON.stringify({ error: 'Payload too
5656

5757
// API endpoints
5858
function isValidModel(model) {
59-
return typeof model === 'string' && model.trim() !== '';
59+
return typeof model === 'string' && model.length > 0 && model.length < 1000 && model.trim() !== '';
6060
}
6161

6262
function isValidMessagesArray(messages) {
@@ -154,8 +154,8 @@ app.use((err, req, res, next) => {
154154
});
155155

156156
const computationCache = new Map();
157-
let lastIterations = null;
158-
let lastResult = null;
157+
let lastIterations = undefined;
158+
let lastResult = undefined;
159159

160160
/**
161161
* Performs a heavy mathematical computation.

tests/api_robustness.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,15 @@ test('JSON error handler safely skips without crashing if headers are already se
5353
// The fact that it gets here without an UncaughtException (ERR_HTTP_HEADERS_SENT) means it passed
5454
assert.ok(true);
5555
});
56+
57+
test('POST /v1/chat/completions handles extremely large model names gracefully', async () => {
58+
const largeModel = 'a'.repeat(10000);
59+
const res = await request(app)
60+
.post('/v1/chat/completions')
61+
.send({
62+
model: largeModel,
63+
messages: [{ role: 'user', content: 'test' }]
64+
});
65+
assert.strictEqual(res.status, 400);
66+
assert.strictEqual(res.body.error, 'Missing or invalid model');
67+
});

tests/heavy_computation.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,8 @@ test('heavyComputation implements LRU eviction', () => {
6161
// but we can at least assert that it didn't crash.
6262
assert.ok(time1 < 1.0, 'Refreshed item 1 should be near-instant');
6363
});
64+
65+
test('heavyComputation handles null correctly', () => {
66+
const result = heavyComputation(null);
67+
assert.strictEqual(result, 0);
68+
});

0 commit comments

Comments
 (0)