Skip to content

Commit

Permalink
Merge pull request #1606 from murgatroid99/grpc_prototype_pollution
Browse files Browse the repository at this point in the history
grpc: Prevent prototype pollution in loadPackageDefinition
  • Loading branch information
murgatroid99 committed Oct 23, 2020
2 parents 96803d4 + 01cbcab commit 23b3795
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/grpc-native-core/index.js
Expand Up @@ -161,6 +161,9 @@ exports.loadPackageDefinition = function loadPackageDefintion(packageDef) {
for (const serviceFqn in packageDef) {
const service = packageDef[serviceFqn];
const nameComponents = serviceFqn.split('.');
if (nameComponents.some(comp => comp === '__proto__')) {
continue;
}
const serviceName = nameComponents[nameComponents.length-1];
let current = result;
for (const packageName of nameComponents.slice(0, -1)) {
Expand Down
5 changes: 4 additions & 1 deletion packages/grpc-native-core/src/client.js
Expand Up @@ -992,6 +992,9 @@ exports.makeClientConstructor = function(methods, serviceName,

Object.keys(methods).forEach(name => {
const attrs = methods[name];
if (name === '__proto__') {
return;
}
if (name.indexOf('$') === 0) {
throw new Error('Method names cannot start with $');
}
Expand All @@ -1011,7 +1014,7 @@ exports.makeClientConstructor = function(methods, serviceName,
ServiceClient.prototype.$method_names[attrs.path] = name;
// Associate all provided attributes with the method
Object.assign(ServiceClient.prototype[name], attrs);
if (attrs.originalName) {
if (attrs.originalName && attrs.originalName !== '__proto__') {
ServiceClient.prototype[attrs.originalName] =
ServiceClient.prototype[name];
}
Expand Down
27 changes: 27 additions & 0 deletions packages/grpc-native-core/test/prototype_pollution_test.js
@@ -0,0 +1,27 @@
/*
* Copyright 2020 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import * as assert from 'assert';

const grpc = require('../');

describe('loadPackageDefinition', () => {
it('Should not allow prototype pollution', () => {
grpc.loadPackageDefinition({'__proto__.polluted': true});
assert.notStrictEqual({}.polluted, true);
});
});

0 comments on commit 23b3795

Please sign in to comment.