-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also guard against entity expansion attacks in attribute values
- Loading branch information
Showing
3 changed files
with
158 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { EntityRefEvent } from './parserEvents'; | ||
import { throwErrorWithContext } from './parsingAlgorithms'; | ||
|
||
/* | ||
* Guard against entity expansion attacks by keeping track of the initial input | ||
* length vs. the expanded input length. The latter includes the length of the | ||
* replacement text for each processed entity reference. An attack is likely if | ||
* the ratio between the two exceeds the maximum amplification factor AND the | ||
* expanded input length exceeds a threshold. This approach and defaults are | ||
* taken from libexpat's billion laughs attack protection. | ||
*/ | ||
export default class EntityExpansionGuard { | ||
private readonly _initialInputLength: number; | ||
|
||
private _expandedInputLength: number; | ||
|
||
private readonly _entityExpansionThreshold: number; | ||
|
||
private readonly _entityExpansionMaxAmplification: number; | ||
|
||
private _topLevelEntityRef: EntityRefEvent | null = null; | ||
|
||
private _depth = 0; | ||
|
||
public constructor( | ||
initialInputLength: number, | ||
entityExpansionThreshold: number, | ||
entityExpansionMaxAmplification: number | ||
) { | ||
this._initialInputLength = initialInputLength; | ||
this._expandedInputLength = initialInputLength; | ||
this._entityExpansionThreshold = entityExpansionThreshold; | ||
this._entityExpansionMaxAmplification = entityExpansionMaxAmplification; | ||
} | ||
|
||
public enter(event: EntityRefEvent, replacementTextLength: number): void { | ||
const topLevelEntityRef = this._topLevelEntityRef ?? event; | ||
this._expandedInputLength += replacementTextLength; | ||
if (this._expandedInputLength > this._entityExpansionThreshold) { | ||
const amplification = this._expandedInputLength / this._initialInputLength; | ||
if (amplification > this._entityExpansionMaxAmplification) { | ||
throwErrorWithContext('too much entity expansion', topLevelEntityRef); | ||
} | ||
} | ||
this._topLevelEntityRef = topLevelEntityRef; | ||
this._depth += 1; | ||
} | ||
|
||
public exit(): void { | ||
this._depth -= 1; | ||
if (this._depth === 0) { | ||
this._topLevelEntityRef = null; | ||
} | ||
} | ||
} |
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