Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ERROR_PAYLOAD_TOO_LARGE = Buffer.from(JSON.stringify({ error: 'Payload too

// API endpoints
function isValidModel(model) {
return typeof model === 'string' && model.trim() !== '';
return typeof model === 'string' && model.length > 0 && model.length < 1000 && model.trim() !== '';
}

function isValidMessagesArray(messages) {
Expand Down Expand Up @@ -154,8 +154,8 @@ app.use((err, req, res, next) => {
});

const computationCache = new Map();
let lastIterations = null;
let lastResult = null;
let lastIterations = undefined;
let lastResult = undefined;

/**
* Performs a heavy mathematical computation.
Expand Down
12 changes: 12 additions & 0 deletions tests/api_robustness.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ test('JSON error handler safely skips without crashing if headers are already se
// The fact that it gets here without an UncaughtException (ERR_HTTP_HEADERS_SENT) means it passed
assert.ok(true);
});

test('POST /v1/chat/completions handles extremely large model names gracefully', async () => {
const largeModel = 'a'.repeat(10000);
const res = await request(app)
.post('/v1/chat/completions')
.send({
model: largeModel,
messages: [{ role: 'user', content: 'test' }]
});
assert.strictEqual(res.status, 400);
assert.strictEqual(res.body.error, 'Missing or invalid model');
});
5 changes: 5 additions & 0 deletions tests/heavy_computation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ test('heavyComputation implements LRU eviction', () => {
// but we can at least assert that it didn't crash.
assert.ok(time1 < 1.0, 'Refreshed item 1 should be near-instant');
});

test('heavyComputation handles null correctly', () => {
const result = heavyComputation(null);
assert.strictEqual(result, 0);
});
Loading