@lvce-editor/eslint-plugin-extension-json 17.3.1

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.
Files changed (2) hide show
  1. package/dist/index.js +114 -0
  2. package/package.json +15 -0
package/dist/index.js ADDED
@@ -0,0 +1,114 @@
1
+ import json from '@eslint/json';
2
+
3
+ const isArrayNode = node => {
4
+ return node && node.type === 'Array';
5
+ };
6
+ const isObjectNode = node => {
7
+ return node && node.type === 'Object';
8
+ };
9
+ const isStringNode = node => {
10
+ return node && node.type === 'String';
11
+ };
12
+ const getMemberName = member => {
13
+ if (member.name.type === 'String') {
14
+ return member.name.value;
15
+ }
16
+ if (member.name.type === 'Identifier') {
17
+ return member.name.name;
18
+ }
19
+ return undefined;
20
+ };
21
+ const findMember = (node, name) => {
22
+ if (!isObjectNode(node)) {
23
+ return undefined;
24
+ }
25
+ for (const member of node.members) {
26
+ if (getMemberName(member) === name) {
27
+ return member;
28
+ }
29
+ }
30
+ return undefined;
31
+ };
32
+
33
+ const requiredFirstDirective = "default-src 'none'";
34
+ const meta = {
35
+ docs: {
36
+ description: 'Require a secure content security policy for extension workers'
37
+ },
38
+ messages: {
39
+ insecureContentSecurityPolicy: `extension contentSecurityPolicy must start with ${requiredFirstDirective}`,
40
+ invalidContentSecurityPolicy: 'extension contentSecurityPolicy must be an array of strings',
41
+ missingContentSecurityPolicy: 'extensions with a main entry must configure contentSecurityPolicy'
42
+ },
43
+ type: 'problem'
44
+ };
45
+ const hasMainEntry = manifest => {
46
+ return Boolean(findMember(manifest, 'browser') || findMember(manifest, 'main'));
47
+ };
48
+ const isValidPolicyArray = node => {
49
+ return isArrayNode(node) && node.elements.every(element => isStringNode(element.value));
50
+ };
51
+ const create = context => {
52
+ return {
53
+ Document(node) {
54
+ if (!isObjectNode(node.body)) {
55
+ return;
56
+ }
57
+ const contentSecurityPolicy = findMember(node.body, 'contentSecurityPolicy');
58
+ if (!contentSecurityPolicy) {
59
+ if (hasMainEntry(node.body)) {
60
+ context.report({
61
+ loc: node.body.loc,
62
+ messageId: 'missingContentSecurityPolicy'
63
+ });
64
+ }
65
+ return;
66
+ }
67
+ if (!isValidPolicyArray(contentSecurityPolicy.value)) {
68
+ context.report({
69
+ loc: contentSecurityPolicy.name.loc,
70
+ messageId: 'invalidContentSecurityPolicy'
71
+ });
72
+ return;
73
+ }
74
+ const [firstDirective] = contentSecurityPolicy.value.elements;
75
+ if (!firstDirective || firstDirective.value.value !== requiredFirstDirective) {
76
+ context.report({
77
+ loc: contentSecurityPolicy.name.loc,
78
+ messageId: 'insecureContentSecurityPolicy'
79
+ });
80
+ }
81
+ }
82
+ };
83
+ };
84
+
85
+ const contentSecurityPolicy = {
86
+ __proto__: null,
87
+ create,
88
+ meta
89
+ };
90
+
91
+ const plugin = {
92
+ configs: {},
93
+ meta: {
94
+ name: 'extension-json',
95
+ version: '0.0.1'
96
+ },
97
+ rules: {
98
+ 'content-security-policy': contentSecurityPolicy
99
+ }
100
+ };
101
+ const recommended = [{
102
+ files: ['**/extension.json'],
103
+ language: 'json/json',
104
+ plugins: {
105
+ 'extension-json': plugin,
106
+ // @ts-ignore
107
+ json
108
+ },
109
+ rules: {
110
+ 'extension-json/content-security-policy': 'error'
111
+ }
112
+ }];
113
+
114
+ export { recommended as default };
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@lvce-editor/eslint-plugin-extension-json",
3
+ "version": "17.3.1",
4
+ "main": "dist/index.js",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/lvce-editor/eslint-config.git"
8
+ },
9
+ "type": "module",
10
+ "license": "MIT",
11
+ "description": "ESLint rules for LVCE extension manifests.",
12
+ "dependencies": {
13
+ "@eslint/json": "2.0.1"
14
+ }
15
+ }