Skip to content

Commit

Permalink
prototype pollution fix
Browse files Browse the repository at this point in the history
  • Loading branch information
d3v53c committed Dec 8, 2020
1 parent 1e37fbd commit b253011
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/grpc-js/src/make-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ export interface ServiceClientConstructor {
service: ServiceDefinition;
}

/**
* Returns true, if given key is included in the blacklisted
* keys.
* @param key key for check, string.
*/
function isPrototypePolluted(key: string): Boolean {
return ['__proto__', 'prototype', 'constructor'].includes(key);
}

/**
* Creates a constructor for a client with the given methods, as specified in
* the methods argument. The resulting class will have an instance method for
Expand Down Expand Up @@ -122,7 +131,7 @@ export function makeClientConstructor(
}

Object.keys(methods).forEach((name) => {
if (name === '__proto__') {
if (isPrototypePolluted(name)) {
return;
}
const attrs = methods[name];
Expand Down Expand Up @@ -155,7 +164,7 @@ export function makeClientConstructor(
ServiceClientImpl.prototype[name] = methodFunc;
// Associate all provided attributes with the method
Object.assign(ServiceClientImpl.prototype[name], attrs);
if (attrs.originalName && attrs.originalName !== '__proto__') {
if (attrs.originalName && !isPrototypePolluted(attrs.originalName)) {
ServiceClientImpl.prototype[attrs.originalName] =
ServiceClientImpl.prototype[name];
}
Expand Down Expand Up @@ -204,7 +213,7 @@ export function loadPackageDefinition(
if (Object.prototype.hasOwnProperty.call(packageDef, serviceFqn)) {
const service = packageDef[serviceFqn];
const nameComponents = serviceFqn.split('.');
if (nameComponents.some(comp => comp === '__proto__')) {
if (nameComponents.some((comp: string) => isPrototypePolluted(comp))) {
continue;
}
const serviceName = nameComponents[nameComponents.length - 1];
Expand Down

0 comments on commit b253011

Please sign in to comment.