-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(end-to-end): metering works for some malicious code
- Loading branch information
1 parent
5c9e1e7
commit 905061c
Showing
7 changed files
with
117 additions
and
33 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
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
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,66 @@ | ||
/* global globalThis */ | ||
/* eslint-disable no-await-in-loop */ | ||
import test from 'tape-promise/tape'; | ||
import * as babelCore from '@babel/core'; | ||
import SES from 'ses'; | ||
|
||
import { | ||
makeMeterAndResetters, | ||
makeMeteringEndowments, | ||
makeMeteringTransformer, | ||
} from '../src/index'; | ||
|
||
test('metering end-to-end', async t => { | ||
try { | ||
const [meter, reset] = makeMeterAndResetters(); | ||
const { meterId, meteringTransform } = makeMeteringTransformer(babelCore); | ||
const endowments = makeMeteringEndowments(meter, globalThis, {}, meterId); | ||
const transforms = [meteringTransform]; | ||
|
||
const s = SES.makeSESRootRealm({ transforms }); | ||
|
||
const myEval = src => { | ||
Object.values(reset).forEach(r => r()); | ||
return s.evaluate(src, endowments); | ||
}; | ||
|
||
const src1 = `123; 456;`; | ||
t.equals(myEval(src1), 456, 'trivial source succeeds'); | ||
|
||
const src2 = `\ | ||
function f() { | ||
f(); | ||
return 1; | ||
} | ||
f(); | ||
`; | ||
t.throws( | ||
() => myEval(src2), | ||
/Stack meter exceeded/, | ||
'stack overflow fails', | ||
); | ||
|
||
const src3 = `\ | ||
while (true) {} | ||
`; | ||
t.throws( | ||
() => myEval(src3), | ||
/Compute meter exceeded/, | ||
'infinite loop fails', | ||
); | ||
|
||
const src4 = `\ | ||
/(x+x+)+y/.test('x'.repeat(10000)); | ||
`; | ||
t.equals(myEval(src4), false, `catastrophic backtracking doesn't happen`); | ||
|
||
const src5 = `\ | ||
new Array(1e6).map(Object.create) | ||
`; | ||
t.throws(() => myEval(src5), RangeError, 'long map fails'); | ||
} catch (e) { | ||
t.isNot(e, e, 'unexpected exception'); | ||
} finally { | ||
t.end(); | ||
} | ||
}); |