Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit f660ef7

Browse files
committed
Add V8\Context::SetSecurityToken() method test
1 parent 40ba86e commit f660ef7

File tree

4 files changed

+100
-19
lines changed

4 files changed

+100
-19
lines changed

stubs/src/Context.php

-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public function DetachGlobal()
8787
{
8888
}
8989

90-
9190
/**
9291
* Sets the security token for the context. To access an object in
9392
* another context, the security tokens must match.

tests/V8Context_SetSecurityToken.phpt

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
--TEST--
2+
V8\Context::SetSecurityToken()
3+
--SKIPIF--
4+
<?php if (!extension_loaded("v8")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
/** @var \Phpv8Testsuite $helper */
9+
$helper = require '.testsuite.php';
10+
require '.v8-helpers.php';
11+
$v8_helper = new PhpV8Helpers($helper);
12+
13+
14+
$isolate1 = new \V8\Isolate();
15+
16+
$context = new \V8\Context($isolate1);
17+
$v8_helper->injectConsoleLog($context);
18+
19+
$other = new \V8\Context($isolate1);
20+
$v8_helper->injectConsoleLog($other);
21+
22+
$obj_own = new \V8\ObjectValue($context);
23+
$obj_own->Set($context, new \V8\StringValue($isolate1, 'test'), new \V8\StringValue($isolate1, 'own'));
24+
25+
$obj_other = new \V8\ObjectValue($context);
26+
$obj_other->Set($context, new \V8\StringValue($isolate1, 'test'), new \V8\StringValue($isolate1, 'other'));
27+
28+
29+
try {
30+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate1, 'own'), $obj_own);
31+
$context->GlobalObject()->Set($other, new \V8\StringValue($isolate1, 'other'), $obj_other);
32+
$helper->assert('There is no cross-context access by default', false);
33+
} catch (\V8\Exceptions\TryCatchException $e) {
34+
$helper->exception_export($e);
35+
}
36+
37+
$context->SetSecurityToken(new \V8\StringValue($isolate1, 'secret 1'));
38+
$other->SetSecurityToken(new \V8\StringValue($isolate1, 'secret 2'));
39+
40+
try {
41+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate1, 'own'), $obj_own);
42+
$context->GlobalObject()->Set($other, new \V8\StringValue($isolate1, 'other'), $obj_other);
43+
$helper->assert('Different security tokens should not grant cross-context access', false);
44+
} catch (\V8\Exceptions\TryCatchException $e) {
45+
$helper->exception_export($e);
46+
}
47+
48+
49+
$context->SetSecurityToken(new \V8\StringValue($isolate1, 'secret'));
50+
$other->SetSecurityToken(new \V8\StringValue($isolate1, 'secret'));
51+
52+
try {
53+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate1, 'own'), $obj_own);
54+
$context->GlobalObject()->Set($other, new \V8\StringValue($isolate1, 'other'), $obj_other);
55+
$helper->assert('Different security tokens with the same value should not grant cross-context access', false);
56+
} catch (\V8\Exceptions\TryCatchException $e) {
57+
$helper->exception_export($e);
58+
}
59+
60+
61+
$secret = new \V8\StringValue($isolate1, 'secret');
62+
63+
$context->SetSecurityToken($secret);
64+
$other->SetSecurityToken($secret);
65+
66+
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate1, 'own'), $obj_own);
67+
$context->GlobalObject()->Set($other, new \V8\StringValue($isolate1, 'other'), $obj_other);
68+
69+
$helper->line();
70+
71+
72+
$v8_helper->CompileRun($context, <<<'SCRIPT'
73+
console.log('own.test: ', own.test);
74+
console.log('other.test: ', other.test);
75+
SCRIPT
76+
);
77+
78+
echo 'We are done for now', PHP_EOL;
79+
?>
80+
EOF
81+
--EXPECT--
82+
V8\Exceptions\TryCatchException: TypeError: no access
83+
V8\Exceptions\TryCatchException: TypeError: no access
84+
V8\Exceptions\TryCatchException: TypeError: no access
85+
86+
own.test: own
87+
other.test: other
88+
We are done for now
89+
EOF

tests/V8Context_reference_lifecycle.phpt

+7-11
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@ $helper = require '.testsuite.php';
1313
require '.v8-helpers.php';
1414
$v8_helper = new PhpV8Helpers($helper);
1515

16-
class Context extends V8\Context {
17-
public function __destruct() {
18-
echo 'Context dies now', PHP_EOL;
19-
}
20-
}
16+
require '.tracking_dtors.php';
2117

2218
$isolate = new \V8\Isolate();
2319

2420

25-
$obj = $v8_helper->CompileRun(new Context($isolate), 'var obj = {}; obj');
21+
$obj = $v8_helper->CompileRun(new \v8Tests\TrackingDtors\Context($isolate), 'var obj = {}; obj');
2622

2723
//$helper->dump($obj);
2824
$helper->dump($obj->GetContext());
2925

3026

31-
$context = new Context($isolate);
27+
$context = new \v8Tests\TrackingDtors\Context($isolate);
3228
$context->GlobalObject()->Set($context, new \V8\StringValue($isolate, 'obj'), $obj);
3329

3430
$helper->line();
@@ -45,7 +41,7 @@ $helper->dump($obj->GetContext());
4541
$obj = null;
4642
?>
4743
--EXPECT--
48-
object(Context)#4 (1) {
44+
object(v8Tests\TrackingDtors\Context)#4 (1) {
4945
["isolate":"V8\Context":private]=>
5046
object(V8\Isolate)#3 (5) {
5147
["snapshot":"V8\Isolate":private]=>
@@ -61,11 +57,11 @@ object(Context)#4 (1) {
6157
}
6258
}
6359

64-
Context dies now
60+
Context dies now!
6561

6662
Previous context should be dead, creating zval for object from old context
6763

68-
object(Context)#6 (1) {
64+
object(v8Tests\TrackingDtors\Context)#6 (1) {
6965
["isolate":"V8\Context":private]=>
7066
object(V8\Isolate)#3 (5) {
7167
["snapshot":"V8\Isolate":private]=>
@@ -80,4 +76,4 @@ object(Context)#6 (1) {
8076
bool(false)
8177
}
8278
}
83-
Context dies now
79+
Context dies now!

tests/V8Context_weakness.phpt

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ V8\Context weakness
1010
/** @var \Phpv8Testsuite $helper */
1111
$helper = require '.testsuite.php';
1212

13-
class Context extends V8\Context {
14-
public function __destruct() {
15-
echo 'Context dies now', PHP_EOL;
16-
}
17-
}
13+
require '.tracking_dtors.php';
14+
1815

1916
$isolate1 = new \V8\Isolate();
2017

@@ -24,7 +21,7 @@ $source1 = 'var obj = {}; obj';
2421
$file_name1 = 'test.js';
2522

2623
$script1 = new \V8\Script(
27-
new Context($isolate1, $global_template1),
24+
new \v8Tests\TrackingDtors\Context($isolate1, $global_template1),
2825
new \V8\StringValue($isolate1, $source1),
2926
new \V8\ScriptOrigin($file_name1)
3027
);
@@ -40,4 +37,4 @@ EOF
4037
--EXPECT--
4138
We are done for now
4239
EOF
43-
Context dies now
40+
Context dies now!

0 commit comments

Comments
 (0)