summaryrefslogtreecommitdiff
path: root/dev/lib/handlebars/src/spec/index.security.test.ts
blob: 878a0931f0da5271b06663dd9669c0d50ef7b12e (plain)
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
/*
 * This file is forked from the handlebars project (https://github.com/handlebars-lang/handlebars.js),
 * and may include modifications made by Elasticsearch B.V.
 * Elasticsearch B.V. licenses this file to you under the MIT License.
 * See `packages/kbn-handlebars/LICENSE` for more information.
 */

import Handlebars from '../..';
import { expectTemplate } from '../__jest__/test_bench';

describe('security issues', () => {
  describe('GH-1495: Prevent Remote Code Execution via constructor', () => {
    it('should not allow constructors to be accessed', () => {
      expectTemplate('{{lookup (lookup this "constructor") "name"}}').withInput({}).toCompileTo('');
      expectTemplate('{{constructor.name}}').withInput({}).toCompileTo('');
    });

    it('GH-1603: should not allow constructors to be accessed (lookup via toString)', () => {
      expectTemplate('{{lookup (lookup this (list "constructor")) "name"}}')
        .withInput({})
        .withHelper('list', function (element) {
          return [element];
        })
        .toCompileTo('');
    });

    it('should allow the "constructor" property to be accessed if it is an "ownProperty"', () => {
      expectTemplate('{{constructor.name}}')
        .withInput({ constructor: { name: 'here we go' } })
        .toCompileTo('here we go');

      expectTemplate('{{lookup (lookup this "constructor") "name"}}')
        .withInput({ constructor: { name: 'here we go' } })
        .toCompileTo('here we go');
    });

    it('should allow the "constructor" property to be accessed if it is an "own property"', () => {
      expectTemplate('{{lookup (lookup this "constructor") "name"}}')
        .withInput({ constructor: { name: 'here we go' } })
        .toCompileTo('here we go');
    });
  });

  describe('GH-1558: Prevent explicit call of helperMissing-helpers', () => {
    describe('without the option "allowExplicitCallOfHelperMissing"', () => {
      it('should throw an exception when calling  "{{helperMissing}}" ', () => {
        expectTemplate('{{helperMissing}}').toThrow(Error);
      });

      it('should throw an exception when calling  "{{#helperMissing}}{{/helperMissing}}" ', () => {
        expectTemplate('{{#helperMissing}}{{/helperMissing}}').toThrow(Error);
      });

      it('should throw an exception when calling  "{{blockHelperMissing "abc" .}}" ', () => {
        const functionCalls = [];
        expect(() => {
          const template = Handlebars.compile('{{blockHelperMissing "abc" .}}');
          template({
            fn() {
              functionCalls.push('called');
            },
          });
        }).toThrow(Error);
        expect(functionCalls.length).toEqual(0);
      });

      it('should throw an exception when calling  "{{#blockHelperMissing .}}{{/blockHelperMissing}}"', () => {
        expectTemplate('{{#blockHelperMissing .}}{{/blockHelperMissing}}')
          .withInput({
            fn() {
              return 'functionInData';
            },
          })
          .toThrow(Error);
      });
    });
  });

  describe('GH-1563', () => {
    it('should not allow to access constructor after overriding via __defineGetter__', () => {
      // @ts-expect-error
      if ({}.__defineGetter__ == null || {}.__lookupGetter__ == null) {
        return; // Browser does not support this exploit anyway
      }
      expectTemplate(
        '{{__defineGetter__ "undefined" valueOf }}' +
          '{{#with __lookupGetter__ }}' +
          '{{__defineGetter__ "propertyIsEnumerable" (this.bind (this.bind 1)) }}' +
          '{{constructor.name}}' +
          '{{/with}}'
      )
        .withInput({})
        .toThrow(/Missing helper: "__defineGetter__"/);
    });
  });

  describe('GH-1595: dangerous properties', () => {
    const templates = [
      '{{constructor}}',
      '{{__defineGetter__}}',
      '{{__defineSetter__}}',
      '{{__lookupGetter__}}',
      '{{__proto__}}',
      '{{lookup this "constructor"}}',
      '{{lookup this "__defineGetter__"}}',
      '{{lookup this "__defineSetter__"}}',
      '{{lookup this "__lookupGetter__"}}',
      '{{lookup this "__proto__"}}',
    ];

    templates.forEach((template) => {
      describe('access should be denied to ' + template, () => {
        it('by default', () => {
          expectTemplate(template).withInput({}).toCompileTo('');
        });
      });
    });
  });

  describe('escapes template variables', () => {
    it('in default mode', () => {
      expectTemplate("{{'a\\b'}}").withCompileOptions().withInput({ 'a\\b': 'c' }).toCompileTo('c');
    });

    it('in strict mode', () => {
      expectTemplate("{{'a\\b'}}")
        .withCompileOptions({ strict: true })
        .withInput({ 'a\\b': 'c' })
        .toCompileTo('c');
    });
  });
});