@markdoc/markdoc 0.5.12 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +472 -448
- package/dist/index.js.map +4 -4
- package/dist/index.mjs +472 -448
- package/dist/index.mjs.map +4 -4
- package/dist/react.js +4 -4
- package/dist/react.js.map +2 -2
- package/dist/src/ast/base.d.ts +1 -1
- package/dist/src/ast/base.d.ts.map +1 -1
- package/dist/src/ast/function.d.ts +1 -1
- package/dist/src/ast/function.d.ts.map +1 -1
- package/dist/src/ast/index.d.ts +2 -2
- package/dist/src/ast/index.d.ts.map +1 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/ast/variable.d.ts +1 -1
- package/dist/src/ast/variable.d.ts.map +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/functions/index.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/renderers/html.d.ts.map +1 -1
- package/dist/src/renderers/react/react.d.ts +2 -2
- package/dist/src/renderers/react/react.d.ts.map +1 -1
- package/dist/src/renderers/react/static.d.ts +1 -1
- package/dist/src/renderers/react/static.d.ts.map +1 -1
- package/dist/src/schema-types/class.d.ts +1 -1
- package/dist/src/schema-types/class.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +1 -1
- package/dist/src/schema-types/conditional.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/conditional.d.ts +1 -1
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/validator.d.ts +1 -1
- package/dist/src/validator.d.ts.map +1 -1
- package/index.ts +21 -22
- package/package.json +9 -14
- package/spec/support/matchers.d.ts +6 -4
- package/src/ast/base.ts +21 -6
- package/src/ast/function.ts +1 -1
- package/src/ast/index.ts +3 -4
- package/src/ast/node.test.ts +126 -36
- package/src/ast/node.ts +4 -4
- package/src/ast/variable.test.ts +5 -4
- package/src/ast/variable.ts +1 -1
- package/src/formatter.test.ts +4 -3
- package/src/formatter.ts +11 -11
- package/src/functions/index.test.ts +8 -7
- package/src/functions/index.ts +1 -1
- package/src/grammar/tag.d.ts +1 -1
- package/src/grammar/tag.js +208 -285
- package/src/grammar/tag.pegjs +8 -8
- package/src/grammar/tag.test.ts +80 -48
- package/src/parser.test.ts +124 -66
- package/src/parser.ts +29 -7
- package/src/renderers/html.test.ts +56 -16
- package/src/renderers/html.ts +10 -2
- package/src/renderers/react/react.test.ts +39 -27
- package/src/renderers/react/react.ts +9 -10
- package/src/renderers/react/static.ts +5 -6
- package/src/schema-types/class.ts +1 -1
- package/src/schema-types/conditional.ts +1 -1
- package/src/schema.ts +9 -4
- package/src/tag.test.ts +2 -1
- package/src/tag.ts +1 -1
- package/src/tags/conditional.ts +4 -4
- package/src/tags/index.ts +2 -2
- package/src/tags/partial.ts +1 -1
- package/src/tokenizer/index.ts +6 -5
- package/src/tokenizer/plugins/annotations.test.ts +29 -28
- package/src/tokenizer/plugins/annotations.ts +12 -12
- package/src/tokenizer/plugins/comments.test.ts +11 -10
- package/src/tokenizer/plugins/comments.ts +2 -2
- package/src/tokenizer/plugins/frontmatter.test.ts +7 -6
- package/src/tokenizer/plugins/frontmatter.ts +2 -2
- package/src/tokenizer/plugins/link.test.ts +14 -13
- package/src/transformer.ts +11 -7
- package/src/transforms/table.test.ts +18 -17
- package/src/transforms/table.ts +1 -1
- package/src/utils.test.ts +23 -22
- package/src/utils.ts +3 -3
- package/src/validator.test.ts +205 -31
- package/src/validator.ts +21 -13
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
import Markdoc from '../../index';
|
|
2
3
|
|
|
3
4
|
function validate(string: string, config = {}) {
|
|
4
5
|
return Markdoc.validate(Markdoc.parse(string), config);
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
describe('table transform validation',
|
|
8
|
-
it('produces an error for non-row content at the row level of a table',
|
|
8
|
+
describe('table transform validation', () => {
|
|
9
|
+
it('produces an error for non-row content at the row level of a table', () => {
|
|
9
10
|
const input = `{% table %}
|
|
10
11
|
* Heading 1
|
|
11
12
|
* Heading 2
|
|
@@ -20,7 +21,7 @@ This is invalid non-conditional content at the row level.
|
|
|
20
21
|
|
|
21
22
|
const errors = validate(input);
|
|
22
23
|
const tableSyntaxErrors = errors.filter(
|
|
23
|
-
(e) => e.error.id === 'table-syntax'
|
|
24
|
+
(e) => e.error.id === 'table-syntax',
|
|
24
25
|
);
|
|
25
26
|
|
|
26
27
|
// One error for the paragraph inside the conditional,
|
|
@@ -34,10 +35,10 @@ This is invalid non-conditional content at the row level.
|
|
|
34
35
|
|
|
35
36
|
// Errors should point to the actual invalid content, not the table
|
|
36
37
|
const conditionalError = tableSyntaxErrors.find(
|
|
37
|
-
(e) => e.location?.start.line === 7
|
|
38
|
+
(e) => e.location?.start.line === 7,
|
|
38
39
|
);
|
|
39
40
|
const rowLevelError = tableSyntaxErrors.find(
|
|
40
|
-
(e) => e.location?.start.line === 9
|
|
41
|
+
(e) => e.location?.start.line === 9,
|
|
41
42
|
);
|
|
42
43
|
expect(conditionalError).toBeDefined();
|
|
43
44
|
expect(conditionalError?.location?.start.line).toBe(7);
|
|
@@ -49,7 +50,7 @@ This is invalid non-conditional content at the row level.
|
|
|
49
50
|
expect(rowLevelError?.lines).toEqual([9, 10]);
|
|
50
51
|
});
|
|
51
52
|
|
|
52
|
-
it('does not produce errors for valid conditional rows',
|
|
53
|
+
it('does not produce errors for valid conditional rows', () => {
|
|
53
54
|
const input = `{% table %}
|
|
54
55
|
* Heading 1
|
|
55
56
|
* Heading 2
|
|
@@ -70,13 +71,13 @@ This is invalid non-conditional content at the row level.
|
|
|
70
71
|
|
|
71
72
|
const errors = validate(input);
|
|
72
73
|
const tableSyntaxErrors = errors.filter(
|
|
73
|
-
(e) => e.error.id === 'table-syntax'
|
|
74
|
+
(e) => e.error.id === 'table-syntax',
|
|
74
75
|
);
|
|
75
76
|
|
|
76
77
|
expect(tableSyntaxErrors).toEqual([]);
|
|
77
78
|
});
|
|
78
79
|
|
|
79
|
-
it('does not produce errors for valid conditionals within a cell',
|
|
80
|
+
it('does not produce errors for valid conditionals within a cell', () => {
|
|
80
81
|
const input = `{% table %}
|
|
81
82
|
* Heading 1
|
|
82
83
|
* Heading 2
|
|
@@ -92,13 +93,13 @@ This is invalid non-conditional content at the row level.
|
|
|
92
93
|
|
|
93
94
|
const errors = validate(input);
|
|
94
95
|
const tableSyntaxErrors = errors.filter(
|
|
95
|
-
(e) => e.error.id === 'table-syntax'
|
|
96
|
+
(e) => e.error.id === 'table-syntax',
|
|
96
97
|
);
|
|
97
98
|
|
|
98
99
|
expect(tableSyntaxErrors).toEqual([]);
|
|
99
100
|
});
|
|
100
101
|
|
|
101
|
-
it('does not produce errors for valid conditional with multiple rows and hr separators',
|
|
102
|
+
it('does not produce errors for valid conditional with multiple rows and hr separators', () => {
|
|
102
103
|
const input = `{% table %}
|
|
103
104
|
* Heading 1
|
|
104
105
|
* Heading 2
|
|
@@ -114,13 +115,13 @@ This is invalid non-conditional content at the row level.
|
|
|
114
115
|
|
|
115
116
|
const errors = validate(input);
|
|
116
117
|
const tableSyntaxErrors = errors.filter(
|
|
117
|
-
(e) => e.error.id === 'table-syntax'
|
|
118
|
+
(e) => e.error.id === 'table-syntax',
|
|
118
119
|
);
|
|
119
120
|
|
|
120
121
|
expect(tableSyntaxErrors).toEqual([]);
|
|
121
122
|
});
|
|
122
123
|
|
|
123
|
-
it('does not produce errors for comments in a table',
|
|
124
|
+
it('does not produce errors for comments in a table', () => {
|
|
124
125
|
function validateWithComments(string: string, config = {}) {
|
|
125
126
|
const tokenizer = new Markdoc.Tokenizer({ allowComments: true });
|
|
126
127
|
const tokens = tokenizer.tokenize(string);
|
|
@@ -147,13 +148,13 @@ comment inside conditional
|
|
|
147
148
|
|
|
148
149
|
const errors = validateWithComments(input);
|
|
149
150
|
const tableSyntaxErrors = errors.filter(
|
|
150
|
-
(e) => e.error.id === 'table-syntax'
|
|
151
|
+
(e) => e.error.id === 'table-syntax',
|
|
151
152
|
);
|
|
152
153
|
|
|
153
154
|
expect(tableSyntaxErrors).toEqual([]);
|
|
154
155
|
});
|
|
155
156
|
|
|
156
|
-
it('produces an error for invalid tags inside a table conditional',
|
|
157
|
+
it('produces an error for invalid tags inside a table conditional', () => {
|
|
157
158
|
const input = `{% table %}
|
|
158
159
|
* Heading 1
|
|
159
160
|
* Heading 2
|
|
@@ -171,7 +172,7 @@ This is not a valid row
|
|
|
171
172
|
|
|
172
173
|
const errors = validate(input, config);
|
|
173
174
|
const tableSyntaxErrors = errors.filter(
|
|
174
|
-
(e) => e.error.id === 'table-syntax'
|
|
175
|
+
(e) => e.error.id === 'table-syntax',
|
|
175
176
|
);
|
|
176
177
|
|
|
177
178
|
expect(tableSyntaxErrors.length).toBe(1);
|
|
@@ -183,7 +184,7 @@ This is not a valid row
|
|
|
183
184
|
expect(tableSyntaxErrors[0].lines).toEqual([5, 6]);
|
|
184
185
|
});
|
|
185
186
|
|
|
186
|
-
it('produces an error for non-conditional tags at the row level of a table',
|
|
187
|
+
it('produces an error for non-conditional tags at the row level of a table', () => {
|
|
187
188
|
const input = `{% table %}
|
|
188
189
|
* Heading 1
|
|
189
190
|
* Heading 2
|
|
@@ -199,7 +200,7 @@ This is not a valid row
|
|
|
199
200
|
|
|
200
201
|
const errors = validate(input, config);
|
|
201
202
|
const tableSyntaxErrors = errors.filter(
|
|
202
|
-
(e) => e.error.id === 'table-syntax'
|
|
203
|
+
(e) => e.error.id === 'table-syntax',
|
|
203
204
|
);
|
|
204
205
|
|
|
205
206
|
expect(tableSyntaxErrors.length).toBe(1);
|
package/src/transforms/table.ts
CHANGED
|
@@ -36,7 +36,7 @@ function unexpectedNodeError(node: Node): ValidationError {
|
|
|
36
36
|
|
|
37
37
|
export default function transform(
|
|
38
38
|
document: Node,
|
|
39
|
-
conditionalTags: string[] = ['if']
|
|
39
|
+
conditionalTags: string[] = ['if'],
|
|
40
40
|
) {
|
|
41
41
|
for (const node of document.walk()) {
|
|
42
42
|
if (node.type !== 'tag' || node.tag !== 'table') continue;
|
package/src/utils.test.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
1
2
|
import { findTagEnd, parseTags } from '../src/utils';
|
|
2
3
|
|
|
3
|
-
describe('Templating',
|
|
4
|
-
describe('parseTags',
|
|
5
|
-
it('simple example',
|
|
4
|
+
describe('Templating', () => {
|
|
5
|
+
describe('parseTags', () => {
|
|
6
|
+
it('simple example', () => {
|
|
6
7
|
const example =
|
|
7
8
|
'this is a {% foo blah="asdf" %}test{% /foo %} of template parsing';
|
|
8
9
|
const output = parseTags(example);
|
|
@@ -35,59 +36,59 @@ describe('Templating', function () {
|
|
|
35
36
|
});
|
|
36
37
|
});
|
|
37
38
|
|
|
38
|
-
describe('findTagEnd',
|
|
39
|
-
describe('inline tags',
|
|
40
|
-
it('in a heading',
|
|
39
|
+
describe('findTagEnd', () => {
|
|
40
|
+
describe('inline tags', () => {
|
|
41
|
+
it('in a heading', () => {
|
|
41
42
|
const example = `# Testing {% #foo.bar baz=1 %}`;
|
|
42
43
|
const end = findTagEnd(example, 0);
|
|
43
44
|
expect(end).toEqual(28);
|
|
44
45
|
expect(example[end]).toEqual('%');
|
|
45
46
|
});
|
|
46
47
|
|
|
47
|
-
it('with string',
|
|
48
|
+
it('with string', () => {
|
|
48
49
|
const example = `# Testing {% #foo.bar baz="example" test=true %}`;
|
|
49
50
|
const end = findTagEnd(example, 0);
|
|
50
51
|
expect(end).toEqual(46);
|
|
51
52
|
expect(example[end]).toEqual('%');
|
|
52
53
|
});
|
|
53
54
|
|
|
54
|
-
it('with object literal attribute value',
|
|
55
|
+
it('with object literal attribute value', () => {
|
|
55
56
|
const example = `# Testing {% #foo.bar baz={test: 1, foo: {test: "asdf{"}} %}`;
|
|
56
57
|
const end = findTagEnd(example, 0);
|
|
57
58
|
expect(end).toEqual(58);
|
|
58
59
|
expect(example[end]).toEqual('%');
|
|
59
60
|
});
|
|
60
61
|
|
|
61
|
-
it('in a simple container',
|
|
62
|
+
it('in a simple container', () => {
|
|
62
63
|
const end = findTagEnd('{% foo %}');
|
|
63
64
|
expect(end).toEqual(7);
|
|
64
65
|
});
|
|
65
66
|
|
|
66
|
-
it('in a container with shortcuts',
|
|
67
|
+
it('in a container with shortcuts', () => {
|
|
67
68
|
const end = findTagEnd('{% foo .bar.baz#test %}');
|
|
68
69
|
expect(end).toEqual(21);
|
|
69
70
|
});
|
|
70
71
|
|
|
71
|
-
it('in a container with a string attribute',
|
|
72
|
+
it('in a container with a string attribute', () => {
|
|
72
73
|
const end = findTagEnd('{% foo test="this is a test" %}');
|
|
73
74
|
expect(end).toEqual(29);
|
|
74
75
|
});
|
|
75
76
|
|
|
76
|
-
it('for an invalid container',
|
|
77
|
+
it('for an invalid container', () => {
|
|
77
78
|
const end = findTagEnd('{% foo .bar#baz');
|
|
78
79
|
expect(end).toBeNull();
|
|
79
80
|
});
|
|
80
81
|
|
|
81
|
-
it('in a complex container',
|
|
82
|
+
it('in a complex container', () => {
|
|
82
83
|
const end = findTagEnd(
|
|
83
|
-
'{% #foo .bar .baz test="this} is \\"{test}\\" a test" %} this is a test'
|
|
84
|
+
'{% #foo .bar .baz test="this} is \\"{test}\\" a test" %} this is a test',
|
|
84
85
|
);
|
|
85
86
|
expect(end).toEqual(52);
|
|
86
87
|
});
|
|
87
88
|
});
|
|
88
89
|
|
|
89
|
-
describe('multiline tags',
|
|
90
|
-
it('simple',
|
|
90
|
+
describe('multiline tags', () => {
|
|
91
|
+
it('simple', () => {
|
|
91
92
|
const example = `
|
|
92
93
|
{% test #foo.bar
|
|
93
94
|
baz=1 %}
|
|
@@ -98,7 +99,7 @@ describe('Templating', function () {
|
|
|
98
99
|
expect(example[end]).toEqual('%');
|
|
99
100
|
});
|
|
100
101
|
|
|
101
|
-
it('with string',
|
|
102
|
+
it('with string', () => {
|
|
102
103
|
const example = `
|
|
103
104
|
{% test #foo.bar
|
|
104
105
|
baz="this is a test"
|
|
@@ -110,7 +111,7 @@ describe('Templating', function () {
|
|
|
110
111
|
expect(example[end]).toEqual('%');
|
|
111
112
|
});
|
|
112
113
|
|
|
113
|
-
it('with string and escaped quote',
|
|
114
|
+
it('with string and escaped quote', () => {
|
|
114
115
|
const example = `
|
|
115
116
|
{% test #foo.bar
|
|
116
117
|
baz="this \\"is a test"
|
|
@@ -122,7 +123,7 @@ describe('Templating', function () {
|
|
|
122
123
|
expect(example[end]).toEqual('%');
|
|
123
124
|
});
|
|
124
125
|
|
|
125
|
-
it('with string that has an opening brace',
|
|
126
|
+
it('with string that has an opening brace', () => {
|
|
126
127
|
const example = `
|
|
127
128
|
{% test #foo.bar
|
|
128
129
|
baz="this {is a test"
|
|
@@ -134,7 +135,7 @@ describe('Templating', function () {
|
|
|
134
135
|
expect(example[end]).toEqual('%');
|
|
135
136
|
});
|
|
136
137
|
|
|
137
|
-
it('with string that has escapes and braces',
|
|
138
|
+
it('with string that has escapes and braces', () => {
|
|
138
139
|
const example = `
|
|
139
140
|
{% test #foo.bar
|
|
140
141
|
baz="th\\"is {is a \\\\te\\"st"
|
|
@@ -146,7 +147,7 @@ describe('Templating', function () {
|
|
|
146
147
|
expect(example[end]).toEqual('%');
|
|
147
148
|
});
|
|
148
149
|
|
|
149
|
-
it('with an object literal attribute value',
|
|
150
|
+
it('with an object literal attribute value', () => {
|
|
150
151
|
const example = `
|
|
151
152
|
{% test #foo.bar
|
|
152
153
|
foo={testing: "this } is a test", bar: {baz: 1}}
|
|
@@ -158,7 +159,7 @@ describe('Templating', function () {
|
|
|
158
159
|
expect(example[end]).toEqual('%');
|
|
159
160
|
});
|
|
160
161
|
|
|
161
|
-
it('with an invalid object literal attribute value',
|
|
162
|
+
it('with an invalid object literal attribute value', () => {
|
|
162
163
|
const example = `
|
|
163
164
|
{test #foo.bar
|
|
164
165
|
foo={testing: "this } is a test", bar: {baz: 1}
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parse, SyntaxError } from './grammar/tag';
|
|
2
|
-
import Variable from './ast/variable';
|
|
3
1
|
import Function from './ast/function';
|
|
2
|
+
import Variable from './ast/variable';
|
|
3
|
+
import { parse, SyntaxError } from './grammar/tag';
|
|
4
4
|
|
|
5
5
|
import type { Token } from './types';
|
|
6
6
|
|
|
@@ -53,7 +53,7 @@ export function findTagEnd(content: string, start = 0) {
|
|
|
53
53
|
|
|
54
54
|
export function containsMarkdocTagInUrl(
|
|
55
55
|
content: string,
|
|
56
|
-
allowedProtocols: string[]
|
|
56
|
+
allowedProtocols: string[],
|
|
57
57
|
) {
|
|
58
58
|
for (let pos = 0; pos < content.length; pos++) {
|
|
59
59
|
if (!content.startsWith(OPEN, pos)) continue;
|
package/src/validator.test.ts
CHANGED
|
@@ -1,13 +1,187 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import Markdoc, { Node, nodes, Tag } from '../index';
|
|
2
3
|
import type { Config, ValidationError } from './types';
|
|
3
4
|
|
|
4
5
|
function validate(string, config) {
|
|
5
6
|
return Markdoc.validate(Markdoc.parse(string), config);
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
describe('validate',
|
|
9
|
-
describe('
|
|
10
|
-
|
|
9
|
+
describe('validate', () => {
|
|
10
|
+
describe('prototype-safe attributes', () => {
|
|
11
|
+
let config: Config;
|
|
12
|
+
let customValidate: ReturnType<typeof vi.fn>;
|
|
13
|
+
const unsafe = 'javascript:alert(1)';
|
|
14
|
+
const trusted = 'https://trusted.example/embed';
|
|
15
|
+
const React = {
|
|
16
|
+
Fragment: 'fragment',
|
|
17
|
+
createElement(name, attributes, ...children) {
|
|
18
|
+
return { name, attributes, children };
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
customValidate = vi.fn((value) =>
|
|
24
|
+
value === trusted
|
|
25
|
+
? []
|
|
26
|
+
: [
|
|
27
|
+
{
|
|
28
|
+
id: 'untrusted-src',
|
|
29
|
+
level: 'critical',
|
|
30
|
+
message: 'Untrusted URL',
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
);
|
|
34
|
+
config = {
|
|
35
|
+
tags: {
|
|
36
|
+
embed: {
|
|
37
|
+
render: 'iframe',
|
|
38
|
+
selfClosing: true,
|
|
39
|
+
attributes: {
|
|
40
|
+
src: {
|
|
41
|
+
type: String,
|
|
42
|
+
required: true,
|
|
43
|
+
matches: /^https:\/\/trusted\.example\//,
|
|
44
|
+
errorLevel: 'critical',
|
|
45
|
+
validate: customValidate,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('validates direct values with the pattern and custom validator', () => {
|
|
54
|
+
const ast = Markdoc.parse(`{% embed src="${unsafe}" /%}`);
|
|
55
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
56
|
+
{ error: { id: 'attribute-value-invalid', level: 'critical' } },
|
|
57
|
+
{ error: { id: 'untrusted-src', level: 'critical' } },
|
|
58
|
+
]);
|
|
59
|
+
expect(customValidate).toHaveBeenCalledWith(
|
|
60
|
+
unsafe,
|
|
61
|
+
expect.anything(),
|
|
62
|
+
'src',
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('enforces the URL schema when only errors and critical findings block rendering', () => {
|
|
67
|
+
delete config.tags!.embed.attributes!.src.validate;
|
|
68
|
+
const render = (source: string) => {
|
|
69
|
+
const ast = Markdoc.parse(source);
|
|
70
|
+
const findings = Markdoc.validate(ast, config);
|
|
71
|
+
if (
|
|
72
|
+
findings.some(({ error }) =>
|
|
73
|
+
['error', 'critical'].includes(error.level),
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
return null;
|
|
77
|
+
return Markdoc.renderers.html(Markdoc.transform(ast, config));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
expect(render(`{% embed src="${unsafe}" /%}`)).toBeNull();
|
|
81
|
+
expect(render(`{% embed __proto__={src:"${unsafe}"} /%}`)).toBeNull();
|
|
82
|
+
expect(render(`{% embed src="${trusted}" /%}`)).toBe(
|
|
83
|
+
`<article><iframe src="${trusted}"></iframe></article>`,
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('blocks prototype attribute smuggling before rendering', () => {
|
|
88
|
+
const ast = Markdoc.parse(`{% embed __proto__={src:"${unsafe}"} /%}`);
|
|
89
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
90
|
+
{ error: { id: 'attribute-name-invalid', level: 'critical' } },
|
|
91
|
+
{ error: { id: 'attribute-missing-required', level: 'error' } },
|
|
92
|
+
]);
|
|
93
|
+
expect(customValidate).not.toHaveBeenCalled();
|
|
94
|
+
expect(Object.keys(ast.children[0].attributes)).toEqual([]);
|
|
95
|
+
const output = Markdoc.transform(ast.children[0], config);
|
|
96
|
+
expect(Markdoc.renderers.html(output)).toBe('<iframe></iframe>');
|
|
97
|
+
expect(Markdoc.renderers.react(output, React)).toDeepEqual({
|
|
98
|
+
name: 'iframe',
|
|
99
|
+
attributes: null,
|
|
100
|
+
children: [],
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('still applies type validation to own values', () => {
|
|
105
|
+
const ast = Markdoc.parse('{% embed src=42 /%}');
|
|
106
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
107
|
+
{ error: { id: 'attribute-type-invalid', level: 'critical' } },
|
|
108
|
+
{ error: { id: 'attribute-value-invalid', level: 'critical' } },
|
|
109
|
+
{ error: { id: 'untrusted-src', level: 'critical' } },
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('validates an unsafe own value despite a smuggling attempt', () => {
|
|
114
|
+
const ast = Markdoc.parse(
|
|
115
|
+
`{% embed __proto__={src:"${trusted}"} src="${unsafe}" /%}`,
|
|
116
|
+
);
|
|
117
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
118
|
+
{ error: { id: 'attribute-name-invalid', level: 'critical' } },
|
|
119
|
+
{ error: { id: 'attribute-value-invalid', level: 'critical' } },
|
|
120
|
+
{ error: { id: 'untrusted-src', level: 'critical' } },
|
|
121
|
+
]);
|
|
122
|
+
expect(customValidate).toHaveBeenCalledWith(
|
|
123
|
+
unsafe,
|
|
124
|
+
expect.anything(),
|
|
125
|
+
'src',
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('validates and renders an own value despite a smuggling attempt', () => {
|
|
130
|
+
const ast = Markdoc.parse(
|
|
131
|
+
`{% embed __proto__={src:"${unsafe}"} src="${trusted}" /%}`,
|
|
132
|
+
);
|
|
133
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
134
|
+
{ error: { id: 'attribute-name-invalid', level: 'critical' } },
|
|
135
|
+
]);
|
|
136
|
+
expect(customValidate).toHaveBeenCalledWith(
|
|
137
|
+
trusted,
|
|
138
|
+
expect.anything(),
|
|
139
|
+
'src',
|
|
140
|
+
);
|
|
141
|
+
expect(
|
|
142
|
+
Markdoc.renderers.html(Markdoc.transform(ast.children[0], config)),
|
|
143
|
+
).toBe(`<iframe src="${trusted}"></iframe>`);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('ignores inherited attributes on a programmatically modified node', () => {
|
|
147
|
+
const node = new Node('tag', {}, [], 'embed');
|
|
148
|
+
node.attributes = Object.create({ src: unsafe });
|
|
149
|
+
expect(Markdoc.validate(node, config)).toDeepEqualSubset([
|
|
150
|
+
{ error: { id: 'attribute-missing-required', level: 'error' } },
|
|
151
|
+
]);
|
|
152
|
+
expect(node.transformAttributes(config).src).toBeUndefined();
|
|
153
|
+
expect(Markdoc.renderers.html(Markdoc.transform(node, config))).toBe(
|
|
154
|
+
'<iframe></iframe>',
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('applies defaults instead of inherited values', () => {
|
|
159
|
+
config.tags!.embed.attributes!.src.default = trusted;
|
|
160
|
+
const node = new Node('tag', {}, [], 'embed');
|
|
161
|
+
node.attributes = Object.create({ src: unsafe });
|
|
162
|
+
expect(node.transformAttributes(config).src).toBe(trusted);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('rejects undeclared attributes whose names exist on Object.prototype', () => {
|
|
166
|
+
const ast = Markdoc.parse('{% embed toString="value" /%}');
|
|
167
|
+
expect(Markdoc.validate(ast, config)).toDeepEqualSubset([
|
|
168
|
+
{ error: { id: 'attribute-undefined', level: 'error' } },
|
|
169
|
+
{ error: { id: 'attribute-missing-required', level: 'error' } },
|
|
170
|
+
]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('resolves variables in prototype-free attribute dictionaries', () => {
|
|
174
|
+
config.variables = { url: trusted };
|
|
175
|
+
const ast = Markdoc.parse('{% embed src=$url /%}');
|
|
176
|
+
expect(Markdoc.validate(ast.resolve(config), config)).toEqual([]);
|
|
177
|
+
expect(Markdoc.transform(ast.children[0], config)).toDeepEqual(
|
|
178
|
+
new Tag('iframe', { src: trusted }),
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('function validation', () => {
|
|
184
|
+
it('ensures that function exists', () => {
|
|
11
185
|
const example = '{% foo bar=baz() /%}';
|
|
12
186
|
const output = validate(example, {
|
|
13
187
|
validation: { validateFunctions: true },
|
|
@@ -30,7 +204,7 @@ describe('validate', function () {
|
|
|
30
204
|
]);
|
|
31
205
|
});
|
|
32
206
|
|
|
33
|
-
describe('return type checking',
|
|
207
|
+
describe('return type checking', () => {
|
|
34
208
|
const example = '{% foo bar=baz() /%}';
|
|
35
209
|
const schema = {
|
|
36
210
|
validation: { validateFunctions: true },
|
|
@@ -69,7 +243,7 @@ describe('validate', function () {
|
|
|
69
243
|
},
|
|
70
244
|
};
|
|
71
245
|
|
|
72
|
-
it('correctly handles union types',
|
|
246
|
+
it('correctly handles union types', () => {
|
|
73
247
|
const example = '{% union-tag-1 foo=withUnion() bar=withUnion() /%}';
|
|
74
248
|
expect(validate(example, schema)).toEqual([]);
|
|
75
249
|
|
|
@@ -85,7 +259,7 @@ describe('validate', function () {
|
|
|
85
259
|
]);
|
|
86
260
|
});
|
|
87
261
|
|
|
88
|
-
it('correctly handles return types for nested function calls',
|
|
262
|
+
it('correctly handles return types for nested function calls', () => {
|
|
89
263
|
const example = '{% foo bar=nested(baz(), number()) /%}';
|
|
90
264
|
expect(validate(example, schema)).toEqual([]);
|
|
91
265
|
|
|
@@ -106,11 +280,11 @@ describe('validate', function () {
|
|
|
106
280
|
]);
|
|
107
281
|
});
|
|
108
282
|
|
|
109
|
-
it('accepts a correct return type',
|
|
283
|
+
it('accepts a correct return type', () => {
|
|
110
284
|
expect(validate(example, schema)).toEqual([]);
|
|
111
285
|
});
|
|
112
286
|
|
|
113
|
-
it('correctly handles no return type',
|
|
287
|
+
it('correctly handles no return type', () => {
|
|
114
288
|
const modifiedSchema = {
|
|
115
289
|
...schema,
|
|
116
290
|
functions: { baz: {} },
|
|
@@ -119,7 +293,7 @@ describe('validate', function () {
|
|
|
119
293
|
expect(validate(example, modifiedSchema)).toEqual([]);
|
|
120
294
|
});
|
|
121
295
|
|
|
122
|
-
it('identifies an incorrect return type',
|
|
296
|
+
it('identifies an incorrect return type', () => {
|
|
123
297
|
const modifiedSchema = {
|
|
124
298
|
...schema,
|
|
125
299
|
functions: { baz: { returns: Number } },
|
|
@@ -130,7 +304,7 @@ describe('validate', function () {
|
|
|
130
304
|
});
|
|
131
305
|
});
|
|
132
306
|
|
|
133
|
-
describe('checks parameters',
|
|
307
|
+
describe('checks parameters', () => {
|
|
134
308
|
const schema = {
|
|
135
309
|
validation: { validateFunctions: true },
|
|
136
310
|
functions: {
|
|
@@ -162,13 +336,13 @@ describe('validate', function () {
|
|
|
162
336
|
},
|
|
163
337
|
};
|
|
164
338
|
|
|
165
|
-
it('with a missing optional parameter',
|
|
339
|
+
it('with a missing optional parameter', () => {
|
|
166
340
|
expect(validate('{% foo bar=qux() /%}', schema)).toEqual([]);
|
|
167
341
|
});
|
|
168
342
|
|
|
169
|
-
it('with a missing required parameter',
|
|
343
|
+
it('with a missing required parameter', () => {
|
|
170
344
|
expect(
|
|
171
|
-
validate('{% foo bar=requiredParam() /%}', schema)
|
|
345
|
+
validate('{% foo bar=requiredParam() /%}', schema),
|
|
172
346
|
).toDeepEqualSubset([
|
|
173
347
|
{
|
|
174
348
|
error: {
|
|
@@ -179,20 +353,20 @@ describe('validate', function () {
|
|
|
179
353
|
]);
|
|
180
354
|
});
|
|
181
355
|
|
|
182
|
-
describe('accepts defined parameters',
|
|
183
|
-
it('with keyed parameter',
|
|
356
|
+
describe('accepts defined parameters', () => {
|
|
357
|
+
it('with keyed parameter', () => {
|
|
184
358
|
expect(
|
|
185
|
-
validate('{% foo bar=qux(test="example") /%}', schema)
|
|
359
|
+
validate('{% foo bar=qux(test="example") /%}', schema),
|
|
186
360
|
).toEqual([]);
|
|
187
361
|
});
|
|
188
362
|
});
|
|
189
363
|
|
|
190
|
-
it('ignores parameters when there is no typing',
|
|
364
|
+
it('ignores parameters when there is no typing', () => {
|
|
191
365
|
expect(validate('{% foo bar=noTyping(foo=1) /%}', schema)).toEqual([]);
|
|
192
366
|
});
|
|
193
367
|
|
|
194
|
-
describe('rejects undeclared parameters',
|
|
195
|
-
it('with a keyed parameter',
|
|
368
|
+
describe('rejects undeclared parameters', () => {
|
|
369
|
+
it('with a keyed parameter', () => {
|
|
196
370
|
const output = validate('{% foo bar=baz(foo=1) /%}', schema);
|
|
197
371
|
expect(output).toDeepEqualSubset([
|
|
198
372
|
{
|
|
@@ -205,7 +379,7 @@ describe('validate', function () {
|
|
|
205
379
|
]);
|
|
206
380
|
});
|
|
207
381
|
|
|
208
|
-
it('with a positional parameter',
|
|
382
|
+
it('with a positional parameter', () => {
|
|
209
383
|
expect(validate('{% foo bar=baz(1) /%}', schema)).toDeepEqualSubset([
|
|
210
384
|
{
|
|
211
385
|
type: 'tag',
|
|
@@ -217,7 +391,7 @@ describe('validate', function () {
|
|
|
217
391
|
]);
|
|
218
392
|
|
|
219
393
|
expect(
|
|
220
|
-
validate('{% foo bar=baz(1, test=2) /%}', schema)
|
|
394
|
+
validate('{% foo bar=baz(1, test=2) /%}', schema),
|
|
221
395
|
).toDeepEqualSubset([
|
|
222
396
|
{
|
|
223
397
|
type: 'tag',
|
|
@@ -251,7 +425,7 @@ describe('validate', function () {
|
|
|
251
425
|
bar
|
|
252
426
|
{% /baz %}
|
|
253
427
|
`,
|
|
254
|
-
config
|
|
428
|
+
config,
|
|
255
429
|
);
|
|
256
430
|
expect(block).toEqual([]);
|
|
257
431
|
});
|
|
@@ -266,7 +440,7 @@ bar
|
|
|
266
440
|
bar
|
|
267
441
|
{% /foo %}
|
|
268
442
|
`,
|
|
269
|
-
config
|
|
443
|
+
config,
|
|
270
444
|
);
|
|
271
445
|
expect(wrong[0]?.error.id).toEqual('tag-placement-invalid');
|
|
272
446
|
expect(wrong[0]?.error.message).toContain('should be inline');
|
|
@@ -279,7 +453,7 @@ bar
|
|
|
279
453
|
bar
|
|
280
454
|
{% /bar %}
|
|
281
455
|
`,
|
|
282
|
-
config
|
|
456
|
+
config,
|
|
283
457
|
);
|
|
284
458
|
expect(correct).toEqual([]);
|
|
285
459
|
const wrong = validate(`this is inline {% bar %}bar{% /bar %}`, config);
|
|
@@ -549,7 +723,7 @@ bar
|
|
|
549
723
|
it('simple case', () => {
|
|
550
724
|
const example = `{% foo bar={baz: 3} /%}`;
|
|
551
725
|
|
|
552
|
-
function validator(value,
|
|
726
|
+
function validator(value, _config, name) {
|
|
553
727
|
return value.baz < 5
|
|
554
728
|
? [
|
|
555
729
|
{
|
|
@@ -580,7 +754,7 @@ bar
|
|
|
580
754
|
|
|
581
755
|
const errs = validate(example, config);
|
|
582
756
|
expect(errs[0].error.message).toEqual(
|
|
583
|
-
"The value of 'bar.baz' must be less than five"
|
|
757
|
+
"The value of 'bar.baz' must be less than five",
|
|
584
758
|
);
|
|
585
759
|
});
|
|
586
760
|
|
|
@@ -588,7 +762,7 @@ bar
|
|
|
588
762
|
const example = `{% foo bar={baz: 3} /%}`;
|
|
589
763
|
|
|
590
764
|
class CustomType {
|
|
591
|
-
validate(value,
|
|
765
|
+
validate(value, _config, name) {
|
|
592
766
|
return value.baz < 5
|
|
593
767
|
? [
|
|
594
768
|
{
|
|
@@ -618,7 +792,7 @@ bar
|
|
|
618
792
|
|
|
619
793
|
const errs = validate(example, config);
|
|
620
794
|
expect(errs[0].error.message).toEqual(
|
|
621
|
-
"The value of 'bar.baz' must be less than five"
|
|
795
|
+
"The value of 'bar.baz' must be less than five",
|
|
622
796
|
);
|
|
623
797
|
});
|
|
624
798
|
});
|
|
@@ -657,7 +831,7 @@ bar
|
|
|
657
831
|
nodes: {
|
|
658
832
|
heading: {
|
|
659
833
|
...Markdoc.nodes.heading,
|
|
660
|
-
validate(
|
|
834
|
+
validate(_node, config) {
|
|
661
835
|
if (config.validation.parents.find((p) => p.tag === 'foo'))
|
|
662
836
|
return [
|
|
663
837
|
{
|
|
@@ -691,7 +865,7 @@ bar
|
|
|
691
865
|
tags: {
|
|
692
866
|
foo: {},
|
|
693
867
|
bar: {
|
|
694
|
-
validate(
|
|
868
|
+
validate(_node, config) {
|
|
695
869
|
const parents = config?.validation?.parents?.map((p) => p.type);
|
|
696
870
|
expect(parents).toDeepEqual([
|
|
697
871
|
'document',
|