Skip to content

Commit da81352

Browse files
Jens-Gclaude
andcommitted
THRIFT-6037: Harden JavaScript protocol negative sizes
Client: js Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e6039a1 commit da81352

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎lib/js/src/thrift.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,9 @@ Thrift.Protocol.prototype = {
12351235
r.ktype = Thrift.Protocol.RType[first];
12361236
r.vtype = Thrift.Protocol.RType[map.shift()];
12371237
r.size = map.shift();
1238-
1238+
if (r.size < 0) {
1239+
throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, 'Negative size');
1240+
}
12391241
this.rpos.push(this.rstack.length);
12401242
this.rstack.push(map.shift());
12411243

@@ -1263,7 +1265,9 @@ Thrift.Protocol.prototype = {
12631265
var r = {};
12641266
r.etype = Thrift.Protocol.RType[list.shift()];
12651267
r.size = list.shift();
1266-
1268+
if (r.size < 0) {
1269+
throw new Thrift.TProtocolException(Thrift.TProtocolExceptionType.NEGATIVE_SIZE, 'Negative size');
1270+
}
12671271
this.rpos.push(this.rstack.length);
12681272
this.rstack.push(list.shift());
12691273

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
QUnit.module('Protocol negative size rejection');
21+
22+
function isNegativeSizeError(e) {
23+
return e instanceof Thrift.TProtocolException &&
24+
e.type === Thrift.TProtocolExceptionType.NEGATIVE_SIZE;
25+
}
26+
27+
QUnit.test('readMapBegin rejects negative size', function(assert) {
28+
var transport = new Thrift.Transport('/service');
29+
var protocol = new Thrift.Protocol(transport);
30+
// rstack entry: [ktype, vtype, size, content]
31+
protocol.rstack = [['i32', 'string', -1, {}]];
32+
protocol.rpos = [];
33+
34+
assert.throws(
35+
function() { protocol.readMapBegin(); },
36+
isNegativeSizeError,
37+
'readMapBegin throws NEGATIVE_SIZE for size=-1'
38+
);
39+
});
40+
41+
QUnit.test('readListBegin rejects negative size', function(assert) {
42+
var transport = new Thrift.Transport('/service');
43+
var protocol = new Thrift.Protocol(transport);
44+
// rstack entry: [etype, size, ...content]
45+
protocol.rstack = [['string', -1]];
46+
protocol.rpos = [];
47+
48+
assert.throws(
49+
function() { protocol.readListBegin(); },
50+
isNegativeSizeError,
51+
'readListBegin throws NEGATIVE_SIZE for size=-1'
52+
);
53+
});

0 commit comments

Comments
 (0)