-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepairES5.js
6103 lines (5782 loc) · 221 KB
/
repairES5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Options: --free-variable-checker --require --validate
/*global navigator, document, DOMException, require __x __y*/
// Copyright (C) 2011 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Monkey patch almost ES5 platforms into a closer
* emulation of full <a href=
* "https://code.google.com/p/es-lab/wiki/SecureableES5">Secureable
* ES5</a>.
*
* <p>Assumes only ES3, but only proceeds to do useful repairs when
* the platform is close enough to ES5 to be worth attempting
* repairs. Compatible with almost-ES5, ES5, ES5-strict, and
* anticipated ES6.
*
* <p>Ignore the "...requires ___global_test_function___" below. We
* create it, use it, and delete it all within this module. But we
* need to lie to the linter since it can't tell.
*
* //requires ses.logger
* //requires ses.severities, ses.statuses, ses._repairer
* //optionally requires ses.mitigateSrcGotchas, ses._primordialsHaveBeenFrozen
* //provides ses.ok, ses.okToLoad, ses.getMaxSeverity, ses.updateMaxSeverity
* //provides ses.is, ses.makeDelayedTamperProof
* //provides ses.isInBrowser, ses.isInNodeJS, ses.inFreshRealm
* //provides ses.makeCallerHarmless, ses.makeArgumentsHarmless
* //provides ses.noFuncPoison
* //provides ses.verifyStrictFunctionBody
* //provides ses.getUndeniables, ses.earlyUndeniables
* //provides ses.getAnonIntrinsics
* //provides ses.funcLike, ses.kludge_test_FREEZING_BREAKS_PROTOTYPES
*
* @author Mark S. Miller
* @requires ___global_test_function___, ___global_valueOf_function___
* @requires JSON, eval, this
* @requires navigator, document, DOMException, require
* @overrides ses, repairES5Module
* @overrides RegExp, Object, parseInt
*/
var RegExp;
var ses;
/**
* <p>Qualifying platforms generally include all JavaScript platforms
* shown on <a href="http://kangax.github.com/es5-compat-table/"
* >ECMAScript 5 compatibility table</a> that implement {@code
* Object.getOwnPropertyNames}. At the time of this writing,
* qualifying browsers already include the latest released versions of
* Internet Explorer (9), Firefox (4), Chrome (11), and Safari
* (5.0.5), their corresponding standalone (e.g., server-side) JavaScript
* engines, Rhino 1.73, and BESEN.
*
* <p>On such not-quite-ES5 platforms, some elements of these
* emulations may lose SES safety, as enumerated in the comment on
* each problem record in the {@code baseProblems} and {@code
* supportedProblems} array below. The platform must at least provide
* {@code Object.getOwnPropertyNames}, because it cannot reasonably be
* emulated.
*
* <p>This file is useful by itself, as it has no dependencies on the
* rest of SES. It creates no new global bindings, but merely repairs
* standard globals or standard elements reachable from standard
* globals. If the future-standard {@code WeakMap} global is present,
* as it is currently on FF7.0a1, then it will repair it in place. The
* one non-standard element that this file uses is {@code console} if
* present, in order to report the repairs it found necessary, in
* which case we use its {@code log, info, warn}, and {@code error}
* methods. If {@code console.log} is absent, then this file performs
* its repairs silently.
*
* <p>Generally, this file should be run as the first script in a
* JavaScript context (i.e. a browser frame), as it relies on other
* primordial objects and methods not yet being perturbed.
*
* <p>TODO(erights): This file tries to protect itself from some
* post-initialization perturbation by stashing some of the
* primordials it needs for later use, but this attempt is currently
* incomplete. We need to revisit this when we support Confined-ES5,
* as a variant of SES in which the primordials are not frozen. See
* previous failed attempt at <a
* href="https://codereview.appspot.com/5278046/" >Speeds up
* WeakMap. Preparing to support unfrozen primordials.</a>. From
* analysis of this failed attempt, it seems that the only practical
* way to support CES is by use of two frames, where most of initSES
* runs in a SES frame, and so can avoid worrying about most of these
* perturbations.
*/
(function repairES5Module() {
"use strict";
// Still valid. Won't be after startSES is called.
var global = (1,eval)('this');
var logger = ses.logger;
var severities = ses.severities;
var statuses = ses.statuses;
/**
* As we start to repair, this will track the worst post-repair
* severity seen so far.
*
* TODO(kpreid): Revisit this; it's a shim for the old "ses.maxSeverity"
* which is no longer a global property since it's now internal state of
* the repairer.
*/
ses.getMaxSeverity = function getMaxSeverity() {
return ses._repairer.getCurrentSeverity();
};
/**
* Are we in a condition to safely operate as SES?
*
* TODO(kpreid): This should subsume the 'dirty' flag from startSES
* by making that into a "problem".
*/
ses.ok = function ok(maxSeverity) {
return ses._repairer.okToUse(maxSeverity);
};
/**
* Are we in a condition to continue initializing SES (as opposed to
* aborting)?
*
* Does not take a max severity argument because the severity during loading
* is pre-chosen by maxAcceptableSeverity.
*/
ses.okToLoad = function okToLoad() {
if (arguments.length !== 0) {
// catch a plausible mistake
throw new Error('okToLoad takes no arguments');
}
return ses._repairer.okToLoad();
};
/**
* Update the max based on the provided severity.
*
* <p>If the provided severity exceeds the max so far, update the
* max to match.
*/
ses.updateMaxSeverity = function updateMaxSeverity(severity) {
// TODO(kpreid): Replace uses of this with new repair framework
return ses._repairer.updateMaxSeverity(severity);
};
//////// Prepare for "caller" and "argument" testing and repair /////////
/**
* Needs to work on ES3, since repairES5.js may be run on an ES3
* platform.
*/
function strictForEachFn(list, callback) {
for (var i = 0, len = list.length; i < len; i++) {
callback(list[i], i);
}
}
/**
* A known strict-mode function for tests to use.
*/
function strictFnSpecimen() {}
/**
* Sample map early, to obtain a representative built-in for testing.
*
* <p>There is no reliable test for whether a function is a
* built-in, and it is possible some of the tests below might
* replace the built-in Array.prototype.map, though currently none
* do. Since we <i>assume</i> (but with no reliable way to check)
* that repairES5.js runs in its JavaScript context before anything
* which might have replaced map, we sample it now. The map method
* is a particularly nice one to sample, since it can easily be used
* to test what the "caller" and "arguments" properties on a
* in-progress built-in method reveals.
*/
var builtInMapMethod = Array.prototype.map;
var builtInForEach = Array.prototype.forEach;
/**
* At https://bugs.ecmascript.org/show_bug.cgi?id=3113#c24 Jason
* Orendorff states the best draft for a simpler safe spec for the
* .caller and .argument properties on functions, that may or may
* not make it into ES6, but is on a track to standardization
* regardless. In Firefox 34 and
* https://bugzilla.mozilla.org/show_bug.cgi?id=969478 apparently
* this was implemented, or a reasonable approximation that we need
* to determine can be made SES-safe. Since this is a very different
* situation that the ES5 spec for these, we test which regime we
* seem to be in up front, so we can switch other logic based on this.
*
* If we seem to be in the new regime, then we try to delete the
* poison properties for simple safety, rather than trying to find
* subtle corner cases by which they might lose safety. If any of
* this fails, then we proceed under the assumption we're in the old
* regime.
*
* If noFuncPoison, then we're in the new regime made simply safe by
* these deletions, and we do not treat the names 'caller' and
* 'arguments' on functions as special.
*/
var noFuncPoison =
Function.prototype.hasOwnProperty('caller') &&
Function.prototype.hasOwnProperty('arguments') &&
!strictFnSpecimen.hasOwnProperty('caller') &&
!strictFnSpecimen.hasOwnProperty('arguments') &&
!builtInMapMethod.hasOwnProperty('caller') &&
!builtInMapMethod.hasOwnProperty('arguments') &&
delete Function.prototype.caller &&
delete Function.prototype.arguments &&
!Function.prototype.hasOwnProperty('caller') &&
!Function.prototype.hasOwnProperty('arguments');
ses.noFuncPoison = noFuncPoison;
/**
* http://wiki.ecmascript.org/doku.php?id=harmony:egal
*/
var is = ses.is = Object.is || function(x, y) {
if (x === y) {
// 0 === -0, but they are not identical
return x !== 0 || 1 / x === 1 / y;
}
// NaN !== NaN, but they are identical.
// NaNs are the only non-reflexive value, i.e., if x !== x,
// then x is a NaN.
// isNaN is broken: it converts its argument to number, so
// isNaN("foo") => true
return x !== x && y !== y;
};
/**
* By the time this module exits, either this is repaired to be a
* function that is adequate to make the "caller" property of a
* strict or built-in function harmess, or this module has reported
* a failure to repair.
*
* <p>Start off with the optimistic assumption that nothing is
* needed to make the "caller" property of a strict or built-in
* function harmless. We are not concerned with the "caller"
* property of non-strict functions. It is not the responsibility of
* this module to actually make these "caller" properties
* harmless. Rather, this module only provides this function so
* clients such as startSES.js can use it to do so on the functions
* they whitelist.
*
* <p>If the "caller" property of strict functions are not already
* harmless, then this platform cannot be repaired to be
* SES-safe. The only reason why {@code makeCallerHarmless} must
* work on strict functions in addition to built-in is that some of
* the other repairs below will replace some of the built-ins with
* strict functions, so startSES.js will apply {@code
* makeCallerHarmless} blindly to both strict and built-in
* functions. {@code makeCallerHarmless} simply need not to complete
* without breaking anything when given a strict function argument.
*/
ses.makeCallerHarmless = function assumeCallerHarmless(func, path) {
return 'Apparently fine';
};
/**
* By the time this module exits, either this is repaired to be a
* function that is adequate to make the "arguments" property of a
* strict or built-in function harmess, or this module has reported
* a failure to repair.
*
* Exactly analogous to {@code makeCallerHarmless}, but for
* "arguments" rather than "caller".
*/
ses.makeArgumentsHarmless = function assumeArgumentsHarmless(func, path) {
return 'Apparently fine';
};
var simpleTamperProofOk = false;
/**
* preemptThaw is only set to something other than a noop function
* when repairing THROWING_THAWS_FROZEN_OBJECT. It is made visible
* here because tamperProof needs to call it before it virtualizes
* data properties into accessor properties.
*/
var preemptThaw = function(obj) {};
/**
* "makeTamperProof()" returns a "tamperProof(obj, opt_pushNext)"
* function that acts like "Object.freeze(obj)", except that, if obj
* is a <i>prototypical</i> object (defined below), it ensures that
* the effect of freezing properties of obj does not suppress the
* ability to override these properties on derived objects by simple
* assignment.
*
* <p>If opt_pushNext is provided, then it is called for each value
* obtained from an own property by reflective property access, so
* that tamperProof's caller can arrange to visit each of these
* values after tamperProof returns if it wishes to recur.
*
* <p>Because of lack of sufficient foresight at the time, ES5
* unfortunately specified that a simple assignment to a
* non-existent property must fail if it would override a
* non-writable data property of the same name. (In retrospect, this
* was a mistake, but it is now too late and we must live with the
* consequences.) As a result, simply freezing an object to make it
* tamper proof has the unfortunate side effect of breaking
* previously correct code that is considered to have followed JS
* best practices, if this previous code used assignment to
* override.
*
* <p>To work around this mistake, tamperProof(obj) detects if obj
* is <i>prototypical</i>, i.e., is an object whose own
* "constructor" is a function whose "prototype" is this obj. For example,
* Object.prototype and Function.prototype are prototypical. If so,
* then when tamper proofing it, prior to freezing, replace all its
* configurable own data properties with accessor properties which
* simulate what we should have specified -- that assignments to
* derived objects succeed if otherwise possible. In this case,
* opt_pushNext, if provided, is called on the value that this data
* property had <i>and</i> on the accessors which replaced it.
*
* <p>Some platforms (Chrome and Safari as of this writing)
* implement the assignment semantics ES5 should have specified
* rather than what it did specify.
* "test_ASSIGN_CAN_OVERRIDE_FROZEN()" below tests whether we are on
* such a platform. If so, "repair_ASSIGN_CAN_OVERRIDE_FROZEN()"
* sets simpleTamperProofOk, which informs makeTamperProof that the
* complex workaround here is not needed on those platforms. If
* opt_pushNext is provided, it must still use reflection to obtain
* those values.
*
* <p>"makeTamperProof" should only be called after the trusted
* initialization has done all the monkey patching that it is going
* to do on the Object.* methods, but before any untrusted code runs
* in this context.
*/
function makeTamperProof() {
// Sample these after all trusted monkey patching initialization
// but before any untrusted code runs in this frame.
var gopd = Object.getOwnPropertyDescriptor;
var gopn = Object.getOwnPropertyNames;
var freeze = Object.freeze;
var isFrozen = Object.isFrozen;
var defProp = Object.defineProperty;
var call = Function.prototype.call;
function forEachNonPoisonOwn(obj, callback) {
var list = gopn(obj);
var len = list.length;
var i, j, name; // crockford rule
if (typeof obj === 'function') {
for (i = 0, j = 0; i < len; i++) {
name = list[i];
if (noFuncPoison || (name !== 'caller' && name !== 'arguments')) {
callback(name, j);
j++;
}
}
} else {
strictForEachFn(list, callback);
}
}
function simpleTamperProof(obj, opt_pushNext) {
if (obj !== Object(obj)) { return obj; }
if (opt_pushNext) {
forEachNonPoisonOwn(obj, function(name) {
var desc = gopd(obj, name);
if ('value' in desc) {
opt_pushNext(desc.value);
} else {
opt_pushNext(desc.get);
opt_pushNext(desc.set);
}
});
}
return freeze(obj);
}
function tamperProof(obj, opt_pushNext) {
if (obj !== Object(obj)) { return obj; }
var func;
if ((typeof obj === 'object' || obj === Function.prototype) &&
!!gopd(obj, 'constructor') &&
typeof (func = obj.constructor) === 'function' &&
func.prototype === obj &&
!isFrozen(obj)) {
var pushNext = opt_pushNext || function(v) {};
preemptThaw(obj);
forEachNonPoisonOwn(obj, function(name) {
var value;
function getter() {
return value;
}
function setter(newValue) {
if (obj === this) {
throw new TypeError('Cannot set virtually frozen property: ' +
name);
}
if (!!gopd(this, name)) {
this[name] = newValue;
}
// TODO(erights): Do all the inherited property checks
defProp(this, name, {
value: newValue,
writable: true,
enumerable: true,
configurable: true
});
}
var desc = gopd(obj, name);
if ('value' in desc) {
value = desc.value;
// On some engines, and perhaps to become standard in ES6,
// __proto__ already behaves as an accessor but is made to
// appear to be a data property, so we should not try to
// reconfigure it into another accessor.
if (desc.configurable && name !== '__proto__') {
getter.prototype = null;
setter.prototype = null;
defProp(obj, name, {
get: getter,
set: setter,
// We should be able to omit the enumerable line, since it
// should default to its existing setting.
enumerable: desc.enumerable,
configurable: false
});
pushNext(getter);
pushNext(setter);
}
pushNext(value);
} else {
pushNext(desc.get);
pushNext(desc.set);
}
});
return freeze(obj);
} else {
return simpleTamperProof(obj, opt_pushNext);
}
}
return simpleTamperProofOk ? simpleTamperProof : tamperProof;
};
var needToTamperProof = [];
/**
* Various repairs may expose non-standard objects that are not
* reachable from startSES's root, and therefore not freezable by
* startSES's normal whitelist traversal. However, freezing these
* during repairES5.js may be too early, as it is before WeakMap.js
* has had a chance to monkey patch Object.freeze if necessary, in
* order to install hidden properties for its own use before the
* object becomes non-extensible.
* TODO(kpreid): Revisit this time-of-execution commentary in new world
*/
function rememberToTamperProof(obj) {
needToTamperProof.push(obj);
}
/**
* Makes and returns a tamperProof(obj) function, and uses it to
* tamper proof all objects whose tamper proofing had been delayed.
*
* <p>"makeDelayedTamperProof()" must only be called once.
*/
var makeDelayedTamperProofCalled = false;
ses.makeDelayedTamperProof = function makeDelayedTamperProof() {
if (makeDelayedTamperProofCalled) {
throw 'makeDelayedTamperProof() must only be called once.';
}
var tamperProof = makeTamperProof();
strictForEachFn(needToTamperProof, tamperProof);
needToTamperProof = void 0;
makeDelayedTamperProofCalled = true;
return tamperProof;
};
////////////////////// Brand testing /////////////////////
/**
* Note that, as of ES5, Object.prototype.toString.call(foo) (for
* the original Object.prototype.toString and original
* Function.prototype.call) was a reliable branding mechanism for
* distinguishing the built-in types. This is no longer true of ES6
* once untrusted code runs in that realm, and so should no longer
* be used for that purpose. See makeBrandTester and the brands it
* makes.
*/
var objToString = Object.prototype.toString;
/**
* For reliably testing that a specimen is an exotic object of some
* built-in exotic type.
*
* <p>The exotic type should be those objects normally made by
* ctor. methodName must be the name of a method on ctor.prototype
* that, when applied to an exotic object of this exotic type as
* this-value, with the provided args list, will return without
* error, but when applied to any other object as this-value will
* throw an error. opt_example, if provided, must be an example of
* such an exotic object that can be used for internal sanity
* checking before returning a brandTester.
*
* <p>Uses Allen's trick from
* https://esdiscuss.org/topic/tostringtag-spoofing-for-null-and-undefined#content-59
* for brand testing that will remain reliable in ES6.
* However, testing reveals that, on FF 35.0.1, a proxy on an exotic
* object X will pass this brand test when X will. This is fixed as of
* FF Nightly 38.0a1.
*
* <p>Returns a brandTester function such that, if brandTester(specimen)
* returns true, this is a reliable indicator that specimen actually
* is an exotic object of that type.
*
* <p>As a convenience, ctor may be undefined, in which
* case we assume that there are no exotic objects of that kind. In
* this case, the returned brandTester always says false.
*/
function makeBrandTester(ctor, methodName, args, opt_example) {
if (ctor === void 0) {
// If there is no built-in ctor, then we assume there cannot
// be any objects that are genuinely of that brand.
return function absentCtorBrandTester(specimen) { return false; };
}
var originalMethod = ctor.prototype[methodName];
function brandTester(specimen) {
if (specimen !== Object(specimen)) { return false; }
try {
originalMethod.apply(specimen, args);
return true;
} catch (_) {
return false;
}
};
// a bit of sanity checking before proceeding
var counterExamples = [null, void 0, true, 1, 'x', {}];
if (opt_example !== void 0) {
counterExamples.push({valueOf: function() { return opt_example; }});
counterExamples.push(Object.create(opt_example));
}
strictForEachFn(counterExamples, function(v, i) {
if (brandTester(v)) {
logger.error('Brand test ' + i + ' for ' + ctor + ' passed: ' + v);
ses._repairer.updateMaxSeverity(severities.NOT_SUPPORTED);
}
});
if (opt_example !== void 0 && typeof global.Proxy === 'function') {
// We treat the Proxy counter-example more gently for two reasons:
// * The test fails as of FF 35.0.1, which, as of this writing,
// Caja must still support.
// * It currently does not cause an insecurity for us, since we
// do not yet whitelist Proxy. We might use it internally (see
// startSES.js) but we do not yet make it available to any
// code running within SES.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1133249
// TODO(erights): Add a test for this to test262
// TODO(erights): Extract all these self-tests into tests
// performed within the repair framework.
// TODO(erights): Add a self-test that will catch any
// whitelisting of Proxy while this is still an issue.
var proxy = new global.Proxy(opt_example, {});
if (brandTester(proxy)) {
logger.warn('Brand test of proxy for ' + ctor + ' passed: ' + proxy);
ses._repairer.updateMaxSeverity(severities.SAFE_SPEC_VIOLATION);
}
}
if (opt_example !== void 0 && !brandTester(opt_example)) {
logger.error('Brand test for ' + ctor + ' failed: ' + opt_example);
ses._repairer.updateMaxSeverity(severities.NOT_SUPPORTED);
}
return brandTester;
}
/**
* A reliable brand test for whether specimen has a [[Class]] of
* "Date", or, in ES6 terminology, whether it has a [[DateValue]]
* internal slot.
*/
var isBuiltinDate = makeBrandTester(
Date, 'getDay', [], new Date());
/**
* A reliable brand test for whether specimen has a [[Class]] of
* "Number", or, in ES6 terminology, whether it has a [[NumberData]]
* internal slot.
*/
var isBuiltinNumberObject = makeBrandTester(
Number, 'toString', [], new Number(3));
/**
* A reliable brand test for whether specimen has a [[Class]] of
* "Boolean", or, in ES6 terminology, whether it has a [[BooleanData]]
* internal slot.
*/
var isBuiltinBooleanObject = makeBrandTester(
Boolean, 'toString', [], new Boolean(true));
/**
* A reliable brand test for whether specimen has a [[Class]] of
* "String", or, in ES6 terminology, whether it has a [[StringData]]
* internal slot.
*/
var isBuiltinStringObject = makeBrandTester(
String, 'toString', [], new String('y'));
/**
* A reliable brand test for whether specimen has a [[Class]] of
* "RegExp", or, in ES6 terminology, whether it has a [[RegExpMatcher]]
* internal slot.
*/
var isBuiltinRegExp = makeBrandTester(
RegExp, 'exec', ['x'], /x/);
/**
* A reliable brand test for whether specimen has a [[WeakMapData]]
* internal slot.
*/
var isBuiltinWeakMap = makeBrandTester(
global.WeakMap, 'get', [{}], global.WeakMap ? new WeakMap() : void 0);
//////////////// Undeniables and Intrinsics //////////////
/**
* A known strict function which returns its arguments object.
*/
function strictArguments() { return arguments; }
/**
* A known sloppy function which returns its arguments object.
*
* Defined using Function so it'll be sloppy (not strict and not
* builtin).
*/
var sloppyArguments = Function('return arguments;');
/**
* If present, a known strict generator function which yields its
* arguments object.
*
* <p>TODO: once all supported browsers implement ES6 generators, we
* can drop the "try"s below, drop the check for old Mozilla
* generator syntax, and treat strictArgumentsGenerator as
* unconditional in the test of the code.
*/
var strictArgumentsGenerator = void 0;
try {
// ES6 syntax
strictArgumentsGenerator =
eval('(function*() { "use strict"; yield arguments; })');
} catch (ex) {
if (!(ex instanceof SyntaxError)) { throw ex; }
try {
// Old Firefox syntax
strictArgumentsGenerator =
eval('(function() { "use strict"; yield arguments; })');
} catch (ex2) {
if (!(ex2 instanceof SyntaxError)) { throw ex2; }
}
}
/**
* The undeniables are the primordial objects which are ambiently
* reachable via compositions of strict syntax, primitive wrapping
* (new Object(x)), and prototype navigation (the equivalent of
* Object.getPrototypeOf(x) or x.__proto__). Although we could in
* theory monkey patch primitive wrapping or prototype navigation,
* we won't. Hence, without parsing, the following are undeniable no
* matter what <i>other</i> monkey patching we do to the primordial
* environment.
*/
function getUndeniables() {
var gopd = Object.getOwnPropertyDescriptor;
var getProto = Object.getPrototypeOf;
// The first element of each undeniableTuple is a string used to
// name the undeniable object for reporting purposes. It has no
// other programmatic use.
//
// The second element of each undeniableTuple should be the
// undeniable itself.
//
// The optional third element of the undeniableTuple, if present,
// should be an example of syntax, rather than use of a monkey
// patchable API, evaluating to a value from which the undeniable
// object in the second element can be reached by only the
// following steps:
// If the value is primitve, convert to an Object wrapper.
// Is the resulting object either the undeniable object, or does
// it inherit directly from the undeniable object?
var undeniableTuples = [
['Object.prototype', Object.prototype, {}],
['Function.prototype', Function.prototype, function(){}],
['Array.prototype', Array.prototype, []],
['RegExp.prototype', RegExp.prototype, /x/],
['Boolean.prototype', Boolean.prototype, true],
['Number.prototype', Number.prototype, 1],
['String.prototype', String.prototype, 'x'],
];
var result = {};
// Get the ES6 %Generator% intrinsic, if present.
// It is undeniable because individual generator functions inherit
// from it.
(function() {
// See http://people.mozilla.org/~jorendorff/figure-2.png
// i.e., Figure 2 of section 25.2 "Generator Functions" of the
// ES6 spec.
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects
if (!strictArgumentsGenerator) { return; }
var Generator = getProto(strictArgumentsGenerator);
undeniableTuples.push(['%Generator%', Generator,
strictArgumentsGenerator]);
strictArgumentsGenerator = strictArgumentsGenerator;
}());
strictForEachFn(undeniableTuples, function(tuple) {
var name = tuple[0];
var undeniable = tuple[1];
var start = tuple[2];
result[name] = undeniable;
if (start === void 0) { return; }
start = Object(start);
if (undeniable === start) { return; }
if (undeniable === getProto(start)) { return; }
throw new Error('Unexpected undeniable: ' + undeniable);
});
return result;
}
ses.getUndeniables = getUndeniables;
// For consistency checking, once we've done all our whitelist
// processing and monkey patching, we will call getUndeniables again
// and check that the undeniables are the same.
ses.earlyUndeniables = getUndeniables();
/**
* Get the intrinsics not otherwise reachable by named own property
* traversal. See
* https://people.mozilla.org/~jorendorff/es6-draft.html#sec-well-known-intrinsic-objects
* and the instrinsics section of whitelist.js
*
* <p>Unlike getUndeniables(), the result of getAnonIntrinsics()
* does depend on the current state of the primordials, so we must
* run this again after all other relevant monkey patching is done,
* in order to properly initialize cajaVM.intrinsics
*/
function getAnonIntrinsics() {
var gopd = Object.getOwnPropertyDescriptor;
var getProto = Object.getPrototypeOf;
var result = {};
// If there are still other ThrowTypeError objects left after
// noFuncPoison-ing, this should be caught by
// test_THROWTYPEERROR_NOT_UNIQUE below, so we assume here that
// this is the only surviving ThrowTypeError intrinsic.
result.ThrowTypeError = gopd(arguments, 'caller').get;
// Get the ES6 %ArrayIteratorPrototype%, %StringIteratorPrototype%,
// and %IteratorPrototype% intrinsics, if present.
// TODO %MapIteratorPrototype%, %SetIteratorPrototype%
// It is currently safe to omit %MapIteratorPrototype% and
// %SetIteratorPrototype% since we do not yet whitelist Map and
// Set.
(function() {
var iteratorSym = global.Symbol && global.Symbol.iterator ||
"@@iterator"; // used instead of a symbol on FF35
if ([][iteratorSym]) {
var arrayIter = [][iteratorSym]();
var ArrayIteratorPrototype = getProto(arrayIter);
result.ArrayIteratorPrototype = ArrayIteratorPrototype;
var arrayIterProtoBase = getProto(ArrayIteratorPrototype);
if (arrayIterProtoBase !== Object.prototype) {
if (getProto(arrayIterProtoBase) !== Object.prototype) {
throw new Error(
'%IteratorPrototype%.__proto__ was not Object.prototype');
}
result.IteratorPrototype = arrayIterProtoBase;
}
}
if (''[iteratorSym]) {
var stringIter = ''[iteratorSym]();
var StringIteratorPrototype = getProto(stringIter);
result.StringIteratorPrototype = StringIteratorPrototype;
var stringIterProtoBase = getProto(StringIteratorPrototype);
if (stringIterProtoBase !== Object.prototype) {
if (!result.IteratorPrototype) {
if (getProto(stringIterProtoBase) !== Object.prototype) {
throw new Error(
'%IteratorPrototype%.__proto__ was not Object.prototype');
}
result.IteratorPrototype = stringIterProtoBase;
} else {
if (result.IteratorPrototype !== stringIterProtoBase) {
throw new Error('unexpected %StringIteratorPrototype%.__proto__');
}
}
}
}
}());
// Get the ES6 %GeneratorFunction% intrinsic, if present.
(function() {
var Generator = ses.earlyUndeniables['%Generator%'];
if (!Generator || Generator === Function.prototype) { return; }
if (getProto(Generator) !== Function.prototype) {
throw new Error('Generator.__proto__ was not Function.prototype');
}
var GeneratorFunction = Generator.constructor;
if (GeneratorFunction === Function) { return; }
if (getProto(GeneratorFunction) !== Function) {
throw new Error('GeneratorFunction.__proto__ was not Function');
}
result.GeneratorFunction = GeneratorFunction;
var genProtoBase = getProto(Generator.prototype);
if (genProtoBase !== result.IteratorPrototype &&
genProtoBase !== Object.prototype) {
throw new Error('Unexpected Generator.prototype.__proto__');
}
}());
// Get the ES6 %TypedArray% intrinsic, if present.
(function() {
if (!global.Float32Array) { return; }
var TypedArray = getProto(global.Float32Array);
if (TypedArray === Function.prototype) { return; }
if (getProto(TypedArray) !== Function.prototype) {
// http://bespin.cz/~ondras/html/classv8_1_1ArrayBufferView.html
// has me worried that someone might make such an intermediate
// object visible.
throw new Error('TypedArray.__proto__ was not Function.prototype');
}
result.TypedArray = TypedArray;
}());
for (var name in result) {
if (result[name] === void 0) {
throw new Error('Malformed intrinsic: ' + name);
}
}
return result;
}
ses.getAnonIntrinsics = getAnonIntrinsics;
var unsafeIntrinsics = getAnonIntrinsics();
//////////////////////////////////////////////////////////
/**
* Fails if {@code funcBodySrc} does not parse as a strict
* FunctionBody.
*
* <p>ses.verifyStrictFunctionBody is exported from repairES5
* because the best way to perform this verification on a given
* platform depends on whether the platform's Function constructor
* <a href=
* "https://code.google.com/p/google-caja/issues/detail?id=1616"
* >fails to verify that its body parses as a FunctionBody</a>. If
* it does, repairES5 could have repaired the Function constructor
* itself, but chooses not to, since its main client, startSES, has
* to wrap and replace the Function constructor anyway.
*
* <p>On platforms not suffering from this bug,
* ses.verifyStrictFunctionBody just calls the original Function
* constructor to do this verification (See
* simpleVerifyStrictFunctionBody). Otherwise, we repair
* ses.verifyStrictFunctionBody
*
* <p>See verifyStrictFunctionBodyByEvalThrowing and
* verifyStrictFunctionBodyByParsing.
*
* <p>Note that all verify*(allegedString) functions now always
* start by coercing the alleged string to a guaranteed primitive
* string, do their verification checks on that, and if it passes,
* returns that. Otherwise they throw. If you don't know whether
* something is a string before verifying, use only the output of
* the verifier, not the input. Or coerce it early yourself.
*/
ses.verifyStrictFunctionBody = simpleVerifyStrictFunctionBody;
/**
* The unsafe* variables hold precious values that must not escape
* to untrusted code. When {@code eval} is invoked via {@code
* unsafeEval}, this is a call to the indirect eval function, not
* the direct eval operator.
*/
var unsafeEval = eval;
var UnsafeFunction = Function;
/**
* <p>We use Crock's trick of simply passing {@code funcBodySrc} to
* the original {@code Function} constructor, which will throw a
* SyntaxError if it does not parse as a FunctionBody.
*/
function simpleVerifyStrictFunctionBody(funcBodySrc) {
funcBodySrc = ''+funcBodySrc;
UnsafeFunction('"use strict";' + funcBodySrc);
return funcBodySrc;
}
/**
* If Crock's trick is not safe, then
* repair_CANT_SAFELY_VERIFY_SYNTAX may replace it with Ankur's trick,
* depending on whether the platform also suffers from bugs that
* would block it. See repair_CANT_SAFELY_VERIFY_SYNTAX for details.
*
* <p>To use Ankur's trick to check a FunctionBody rather than a
* program, we use the trick in comment 7 at
* https://code.google.com/p/google-caja/issues/detail?id=1616#c7
* The criticism of it in comment 8 is blocked by Ankur's trick,
* given the absence of the other bugs that
* repair_CANT_SAFELY_VERIFY_SYNTAX checks in order to decide.
*
* <p>Testing once revealed that Crock's trick
* (simpleVerifyStrictFunctionBody) executed over 100x faster on V8.
*/
function verifyStrictFunctionBodyByEvalThrowing(funcBodySrc) {
funcBodySrc = ''+funcBodySrc;
try {
unsafeEval('"use strict"; throw "not a SyntaxError 1";' +
'(function(){' + funcBodySrc +'\n});');
} catch (outerErr) {
if (outerErr === 'not a SyntaxError 1') {
try {
unsafeEval('throw "not a SyntaxError 2";' +
'(function(){{' + funcBodySrc +'\n}})');
} catch (innerErr) {
if (innerErr === 'not a SyntaxError 2') {
// Presumably, if we got here, funcBodySrc parsed as a strict
// function body but was not executed, and {funcBodySrc}
// parsed as a non-strict function body but was not executed.
// We try it again non-strict so that body level nested
// function declarations will not get rejected. Accepting
// them is beyond the ES5 spec, but is known to happen in
// all implementations.
return funcBodySrc;
}
if (innerErr instanceof SyntaxError) {
// This case is likely symptomatic of an attack. But the
// attack is thwarted and so need not be reported as
// anything other than the SyntaxError it is.
throw innerErr;
}
}
}
if (outerErr instanceof SyntaxError) {
throw outerErr;
}
}
throw new TypeError('Unexpected verification outcome');
}
var canMitigateSrcGotchas = typeof ses.mitigateSrcGotchas === 'function';
/**
* Due to https://code.google.com/p/v8/issues/detail?id=2728
* we can't assume that SyntaxErrors are always early. If they're
* not, then even Ankur's trick doesn't work, so we resort to a full
* parse.
*
* <p>Only applicable if ses.mitigateSrcGotchas is available. To
* accommodate constraints of Caja's initialization order, we do not
* capture or invoke ses.mitigateSrcGotchas as the time repairES5 is
* run. Rather we only test for its presence at repair time in order
* to decide what verifier to use. We only use ses.mitigateSrcGotchas
* later when we actually verify eval'ed code, and at that time we
* use the current binding of ses.mitigateSrcGotchas.
*
* <p>Thus, clients (like Caja) that know they will be making a
* real ses.mitigateSrcGotchas available after repair can
* pre-install a placeholder function that, if accidentally invoked,
* throws an error to complain that it was not replaced by the real
* one. Then, sometime prior to the first verification, the client
* should overwrite ses.mitigateSrcGotchas with the real one.
*/
function verifyStrictFunctionBodyByParsing(funcBodySrc) {