Skip to content

Bail out of Patterns with too long placeables #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions fluent/src/bundle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {resolveComplexPattern} from "./resolver.js";
import FluentResource from "./resource.js";
import { FluentNone } from "./types.js";

/**
* Message bundles are single-language stores of translations. They are
Expand Down Expand Up @@ -220,8 +221,13 @@ export default class FluentBundle {
// Resolve a complex pattern.
if (Array.isArray(pattern)) {
let scope = this._createScope(args, errors);
let value = resolveComplexPattern(scope, pattern);
return value.toString(scope);
try {
let value = resolveComplexPattern(scope, pattern);
return value.toString(scope);
} catch (err) {
scope.errors.push(err);
return new FluentNone().toString(scope);
}
}

throw new TypeError("Invalid Pattern type");
Expand Down
18 changes: 10 additions & 8 deletions fluent/src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,19 @@ export function resolveComplexPattern(scope, ptn) {
}

if (part.length > MAX_PLACEABLE_LENGTH) {
scope.errors.push(
new RangeError(
"Too many characters in placeable " +
`(${part.length}, max allowed is ${MAX_PLACEABLE_LENGTH})`
)
scope.dirty.delete(ptn);
// This is a fatal error which causes the resolver to instantly bail out
// on this pattern. The length check protects against excessive memory
// usage, and throwing protects against eating up the CPU when long
// placeables are deeply nested.
throw new RangeError(
"Too many characters in placeable " +
`(${part.length}, max allowed is ${MAX_PLACEABLE_LENGTH})`
);
result.push(part.slice(MAX_PLACEABLE_LENGTH));
} else {
result.push(part);
}

result.push(part);

if (useIsolating) {
result.push(PDI);
}
Expand Down
6 changes: 2 additions & 4 deletions fluent/test/bomb_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ suite('Reference bombs', function() {
`);
});

// XXX Protect the FTL Resolver against the billion laughs attack
// https://bugzil.la/1307126
test.skip('does not expand all placeables', function() {
test('does not expand all placeables', function() {
const msg = bundle.getMessage('lolz');
const val = bundle.formatPattern(msg.value, args, errs);
assert.strictEqual(val, '???');
assert.strictEqual(val, '{???}');
assert.strictEqual(errs.length, 1);
});
});
Expand Down