-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup: "lookup" should also respect dangerous properties
- properties not configurable anymore - add more harmful properties to the list (push, pop, splice...)
- Loading branch information
Showing
8 changed files
with
94 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
lib/handlebars/internal/newObjectWithoutPrototypeProperties.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Create an object without Object.prototype methods like __defineGetter__, constructor, | ||
* __defineSetter__ and __proto__. | ||
* | ||
* Those methods should not be accessed from template code, because that can lead to | ||
* security leaks. This method should be used to create internal objects that. | ||
* | ||
* @private | ||
* @param {...object} sourceObjects | ||
* @returns {object} | ||
*/ | ||
export function newObjectWithoutPrototypeProperties(...sourceObjects) { | ||
let result = Object.create(null); | ||
sourceObjects.forEach(sourceObject => { | ||
if (sourceObject != null) { | ||
Object.keys(sourceObject).forEach(key => { | ||
result[key] = sourceObject[key]; | ||
}); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import {newObjectWithoutPrototypeProperties} from './newObjectWithoutPrototypeProperties'; | ||
|
||
const dangerousProperties = newObjectWithoutPrototypeProperties(); | ||
|
||
getFunctionPropertiesOf(Object.prototype).forEach(propertyName => { | ||
dangerousProperties[propertyName] = true; | ||
}); | ||
getFunctionPropertiesOf(Array.prototype).forEach(propertyName => { | ||
dangerousProperties[propertyName] = true; | ||
}); | ||
getFunctionPropertiesOf(Function.prototype).forEach(propertyName => { | ||
dangerousProperties[propertyName] = true; | ||
}); | ||
getFunctionPropertiesOf(String.prototype).forEach(propertyName => { | ||
dangerousProperties[propertyName] = true; | ||
}); | ||
|
||
// eslint-disable-next-line no-proto | ||
dangerousProperties.__proto__ = true; | ||
dangerousProperties.__defineGetter__ = true; | ||
dangerousProperties.__defineSetter__ = true; | ||
|
||
// Following properties are not _that_ dangerous | ||
delete dangerousProperties.toString; | ||
delete dangerousProperties.includes; | ||
delete dangerousProperties.slice; | ||
delete dangerousProperties.isPrototypeOf; | ||
delete dangerousProperties.propertyIsEnumerable; | ||
delete dangerousProperties.indexOf; | ||
delete dangerousProperties.keys; | ||
delete dangerousProperties.lastIndexOf; | ||
|
||
function getFunctionPropertiesOf(obj) { | ||
return Object.getOwnPropertyNames(obj) | ||
.filter(propertyName => { | ||
try { | ||
return typeof obj[propertyName] === 'function'; | ||
} catch (error) { | ||
// TypeError happens here when accessing 'caller', 'callee', 'arguments' on Function | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Checks if a property can be harmful and should only processed when it is enumerable on its parent. | ||
* | ||
* This is necessary because of various "arbitrary-code-execution" issues that Handlebars has faced in the past. | ||
* | ||
* @param propertyName | ||
* @returns {boolean} | ||
*/ | ||
export function propertyMustBeEnumerable(propertyName) { | ||
return dangerousProperties[propertyName]; | ||
} | ||
|
||
export function getAllDangerousProperties() { | ||
return dangerousProperties; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters