json.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var browserify = require('../');
  2. var fs = require('fs');
  3. var vm = require('vm');
  4. var semver = require('semver');
  5. var test = require('tap').test;
  6. test('json', function (t) {
  7. t.plan(2);
  8. var b = browserify();
  9. b.add(__dirname + '/json/main.js');
  10. b.bundle(function (err, src) {
  11. if (err) t.fail(err);
  12. var c = {
  13. ex : function (obj) {
  14. t.same(obj, { beep : 'boop', x : 555 });
  15. }
  16. };
  17. vm.runInNewContext(src, c);
  18. });
  19. });
  20. // This works in Node v10 and up thanks to the JSON superset proposal, which
  21. // allows the evil chars in javascript strings.
  22. // https://github.com/tc39/proposal-json-superset
  23. test('verify evil json', { skip: semver.gte(process.version, 'v10.0.0') }, function(t) {
  24. t.plan(1);
  25. fs.readFile(__dirname + '/json/evil-chars.json', function(err, data) {
  26. if (err) t.fail(err);
  27. t.throws(function() {
  28. vm.runInNewContext('(' + data.toString() + ')');
  29. });
  30. });
  31. });
  32. test('evil json', function (t) {
  33. t.plan(2);
  34. var b = browserify();
  35. b.add(__dirname + '/json/evil.js');
  36. b.bundle(function (err, src) {
  37. if (err) t.fail(err);
  38. var c = {
  39. ex : function (obj) {
  40. t.same(obj, { evil : '\u2028\u2029' });
  41. }
  42. };
  43. vm.runInNewContext(src, c);
  44. });
  45. });
  46. test('invalid json', function (t) {
  47. var b = browserify();
  48. t.plan(1);
  49. b.add(__dirname + '/json/invalid.js');
  50. b.bundle(function (err, src) {
  51. t.ok(err);
  52. });
  53. });