File tree 3 files changed +20
-14
lines changed
3 files changed +20
-14
lines changed Original file line number Diff line number Diff line change 1
1
import { resolveComplexPattern } from "./resolver.js" ;
2
2
import FluentResource from "./resource.js" ;
3
+ import { FluentNone } from "./types.js" ;
3
4
4
5
/**
5
6
* Message bundles are single-language stores of translations. They are
@@ -220,8 +221,13 @@ export default class FluentBundle {
220
221
// Resolve a complex pattern.
221
222
if ( Array . isArray ( pattern ) ) {
222
223
let scope = this . _createScope ( args , errors ) ;
223
- let value = resolveComplexPattern ( scope , pattern ) ;
224
- return value . toString ( scope ) ;
224
+ try {
225
+ let value = resolveComplexPattern ( scope , pattern ) ;
226
+ return value . toString ( scope ) ;
227
+ } catch ( err ) {
228
+ scope . errors . push ( err ) ;
229
+ return new FluentNone ( ) . toString ( scope ) ;
230
+ }
225
231
}
226
232
227
233
throw new TypeError ( "Invalid Pattern type" ) ;
Original file line number Diff line number Diff line change @@ -277,17 +277,19 @@ export function resolveComplexPattern(scope, ptn) {
277
277
}
278
278
279
279
if ( part . length > MAX_PLACEABLE_LENGTH ) {
280
- scope . errors . push (
281
- new RangeError (
282
- "Too many characters in placeable " +
283
- `(${ part . length } , max allowed is ${ MAX_PLACEABLE_LENGTH } )`
284
- )
280
+ scope . dirty . delete ( ptn ) ;
281
+ // This is a fatal error which causes the resolver to instantly bail out
282
+ // on this pattern. The length check protects against excessive memory
283
+ // usage, and throwing protects against eating up the CPU when long
284
+ // placeables are deeply nested.
285
+ throw new RangeError (
286
+ "Too many characters in placeable " +
287
+ `(${ part . length } , max allowed is ${ MAX_PLACEABLE_LENGTH } )`
285
288
) ;
286
- result . push ( part . slice ( MAX_PLACEABLE_LENGTH ) ) ;
287
- } else {
288
- result . push ( part ) ;
289
289
}
290
290
291
+ result . push ( part ) ;
292
+
291
293
if ( useIsolating ) {
292
294
result . push ( PDI ) ;
293
295
}
Original file line number Diff line number Diff line change @@ -30,12 +30,10 @@ suite('Reference bombs', function() {
30
30
` ) ;
31
31
} ) ;
32
32
33
- // XXX Protect the FTL Resolver against the billion laughs attack
34
- // https://bugzil.la/1307126
35
- test . skip ( 'does not expand all placeables' , function ( ) {
33
+ test ( 'does not expand all placeables' , function ( ) {
36
34
const msg = bundle . getMessage ( 'lolz' ) ;
37
35
const val = bundle . formatPattern ( msg . value , args , errs ) ;
38
- assert . strictEqual ( val , '???' ) ;
36
+ assert . strictEqual ( val , '{ ???} ' ) ;
39
37
assert . strictEqual ( errs . length , 1 ) ;
40
38
} ) ;
41
39
} ) ;
You can’t perform that action at this time.
0 commit comments