Skip to content

Commit 76713d4

Browse files
authored
Merge pull request #30 from mamod/2.2.0
2.2.0
2 parents 6d1996d + 2359d5d commit 76713d4

File tree

13 files changed

+806
-40
lines changed

13 files changed

+806
-40
lines changed

Changes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Revision history for Perl module JavaScript::Duktape
22

3+
2.2.0 2017 2017-06-02
4+
- add sandboxing options and methods, timeout and max_memory
5+
- update docs
6+
- make dll.t test optional
7+
38
2.1.5 2017 2017-05-26
49
- update duktape engine to version 2.1.0
510

MANIFEST

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ t/perl-data.t
7171
t/pointer.t
7272
t/prop.t
7373
t/safe-call.t
74+
t/sandbox/00-memory.t
75+
t/sandbox/01-memory.t
76+
t/sandbox/02-memory.t
77+
t/sandbox/bubble-timeout.t
78+
t/sandbox/check.t
79+
t/sandbox/timeout.t
7480
t/trycatch.t
7581
t/typescript.t
7682
t/uncaught.t

README.pod

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,67 @@ JavaScript::Duktape - Perl interface to Duktape embeddable javascript engine
99

1010
use JavaScript::Duktape;
1111

12-
##create new js context
12+
## create new js context
1313
my $js = JavaScript::Duktape->new();
1414

15-
#set function to be used from javascript land
15+
# set function to be used from javascript land
1616
$js->set('write' => sub {
1717
print $_[0], "\n";
1818
});
1919

20-
$js->eval(qq~
20+
$js->eval(qq{
2121
(function(){
2222
for (var i = 0; i < 100; i++){
2323
write(i);
2424
}
2525
})();
26-
~);
26+
});
2727

2828
=head1 DESCRIPTION
2929

3030
JavaScript::Duktape implements almost all duktape javascript engine api, the c code is just
3131
a thin layer that maps duktape api to perl, and all other functions implemented in perl
3232
it self, so maintaing and contributing to the base code should be easy.
3333

34+
=head1 JavaScript::Duktape->new(%options)
35+
36+
initiate JavaScript::Duktape with options
37+
38+
=head2 options
39+
40+
=over 4
41+
42+
=item max_memory
43+
44+
Set maximum memory allowed for the excuted javascript code to consume, not setting
45+
this option is the default, which means no restricts on the maximum memory that can
46+
be consumed.
47+
48+
Minumum value to set for the C<max_memory> option is 256 * 1024 = (256k)
49+
setting number below 256k will croak.
50+
51+
max_memory => 256 * 1024 * 2
52+
53+
You can resize the memory allowed to consume on different executions by calling
54+
C<resize_memory> method, see L<Sandboxing> section below.
55+
56+
=item timout
57+
58+
Set maximum time javascript code can run, this value represented in seconds and is not 100% guranteed
59+
that the javascript code will fail after the exact value passed, but it will eventually fail on first tick checking.
60+
61+
Not setting this option is the default, which means no timeout checking at all
62+
63+
timeout => 5
64+
65+
You can override this value later on another code evaluation by calling C<set_timeout> method
66+
67+
$js->set_timeout(25);
68+
69+
See L<Sandboxing> section below
70+
71+
=back
72+
3473
=head1 methods
3574

3675
=over 4
@@ -97,6 +136,93 @@ examples provided with this distribution
97136

98137
=back
99138

139+
=head1 Sandboxing
140+
141+
As of version C<2.2.0> C<JavaScript::Duktape> integrated some of
142+
Duktape Engine Sandboxing methods, this will allow developers to restrict
143+
the running javascript code by restricting memory consumption and running time
144+
145+
C<DUK_USE_EXEC_TIMEOUT_CHECK> flag is set by default to enable
146+
L<< Bytecode execution timeout|https://github.com/svaarala/duktape/blob/master/doc/sandboxing.rst#bytecode-execution-timeout-details >>
147+
148+
# prevent javascript code to consume memory more
149+
# than max_memory option
150+
151+
my $js = JavaScript::Duktape->new( max_memory => 256 * 1024 );
152+
153+
# this will fail with "Error: alloc failed" message
154+
# when running, because it will consume more memory
155+
# than the allowed max_memory
156+
$js->eval(q{
157+
var str = '';
158+
while(1){ str += 'XXXX' }
159+
});
160+
161+
=head2 C<set_timout(t)>
162+
163+
Enable/Disable timeout checking, to disable set the value to 0
164+
this value is in seconds
165+
166+
my $js = JavaScript::Duktape->new();
167+
168+
# throw 'time out' Error if executed
169+
# js code does not finish after 5 seconds
170+
$js->set_timeout(5);
171+
172+
eval {
173+
$js->eval(q{
174+
while(1){}
175+
});
176+
};
177+
178+
print $@, "\n"; #RangeError: execution timeout
179+
180+
# disable timeout checking
181+
$js->set_timeout(0);
182+
183+
# now will run infinitely
184+
$js->eval(q{
185+
while(1){}
186+
});
187+
188+
This method can be used with duktape VM instance too
189+
190+
my $js = JavaScript::Duktape->new();
191+
my $duk = $js->duk();
192+
193+
$duk->set_timeout(3);
194+
$duk->peva_stringl(q{
195+
while (1){}
196+
});
197+
198+
print $duk->safe_to_string(-1); # Error: execution 'time out'
199+
200+
=head2 C<resize_memory(m)>
201+
202+
This method will have effect only if you intiated with max_memory option
203+
204+
my $js = JavaScript::Duktape->new( max_memory => 1024 * 256 );
205+
206+
207+
eval {
208+
$js->eval(q{
209+
var buf = Buffer(( 1024 * 256 ) + 1000 );
210+
print('does not reach');
211+
});
212+
};
213+
214+
print $@, "\n"; # Error: 'alloc failed'
215+
216+
$js->resize_memory( 1024 * 256 * 2 );
217+
218+
# now it will not throw
219+
$js->eval(q{
220+
var buf = Buffer(( 1024 * 256 ) + 1000 );
221+
print('ok');
222+
});
223+
224+
225+
100226
=head1 VM API
101227

102228
vm api corresponds to Duktape Engine API see L<http://duktape.org/api.html>
@@ -327,16 +453,25 @@ Mamod Mehyar C<< <[email protected]> >>
327453

328454
=head1 CONTRIBUTORS
329455

330-
Big thanks for the much appreciated contributors
456+
Thanks for everyone who contributed to this module, either by code, bug reports, API design
457+
or suggestions
331458

332459
=over 4
333460

334461
=item * Rodrigo de Oliveira L<@rodrigolive|https://github.com/rodrigolive>
335462

336463
=item * jomo666 L<@jomo666|https://github.com/jomo666>
337464

465+
=item * Viacheslav Tykhanovskyi L<@vti|https://github.com/vti>
466+
467+
=item * Slaven Rezić L<@eserte|https://github.com/eserte>
468+
338469
=back
339470

471+
=head1 APPRECIATION
472+
473+
Credits should go to L<< Duktape Javascript embeddable engine|http://duktape.org >> and it's creator L<< Sami Vaarala|https://github.com/svaarala >>
474+
340475
=head1 LICENSE
341476

342477
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

0 commit comments

Comments
 (0)