eslint-plugin-sfmc 4.10.0 → 4.11.0
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
CHANGED
|
@@ -131,6 +131,7 @@ export default [...sfmc.configs['recommended-next'], ...sfmc.configs['embedded-n
|
|
|
131
131
|
| [`sfmc/ssjs-no-deprecated-function`](docs/rules/ssjs/no-deprecated-function.md) | `error` | Flag deprecated SFMC SSJS APIs (Classic Content classes, ErrorUtil); reports a stronger "undefined at runtime" message when the loaded Core version no longer provides the API |
|
|
132
132
|
| [`sfmc/ssjs-no-nonexistent-global`](docs/rules/ssjs/no-nonexistent-global.md) | `error` | Flag documented SSJS globals that throw ReferenceError at runtime |
|
|
133
133
|
| [`sfmc/ssjs-no-property-call`](docs/rules/ssjs/no-property-call.md) | `error` | Disallow calling Platform.Request/Response properties as functions |
|
|
134
|
+
| [`sfmc/ssjs-no-invalid-property-access`](docs/rules/ssjs/no-invalid-property-access.md) | `error` | Disallow reading write-only properties (`postData`) and assigning to read-only ones (`Platform.Request.*`) |
|
|
134
135
|
| [`sfmc/ssjs-no-clr-header-access`](docs/rules/ssjs/no-clr-header-access.md) | `error` | Disallow CLR-unsafe reads of `HttpResponse.headers`; read via `for..in` |
|
|
135
136
|
| [`sfmc/ssjs-require-string-clr-content`](docs/rules/ssjs/require-string-clr-content.md) | `error` | Require wrapping `HttpResponse.content` with `String()` before use |
|
|
136
137
|
| [`sfmc/ssjs-platform-function-arity`](docs/rules/ssjs/platform-function-arity.md) | `error` | Enforce correct arity for `Platform.Function.*` |
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# `sfmc/ssjs-no-invalid-property-access`
|
|
2
|
+
|
|
3
|
+
Flags property accesses that go against the direction the SFMC runtime actually supports —
|
|
4
|
+
reading a property that can only be written, or writing one that can only be read.
|
|
5
|
+
|
|
6
|
+
The restriction lives in [`ssjs-data`](../../../..) as an `access` field on the property
|
|
7
|
+
entry (exposed via `propertyAccessLookup`), so this rule and the VS Code / Cursor
|
|
8
|
+
diagnostic (`ssjs/invalid-property-access`) share one source of truth, and flagging a
|
|
9
|
+
future property is a one-field data change.
|
|
10
|
+
|
|
11
|
+
## The three restrictions
|
|
12
|
+
|
|
13
|
+
| `access` | Runtime behaviour | Flagged usage | Members |
|
|
14
|
+
| --- | --- | --- | --- |
|
|
15
|
+
| `write-only` | Assignment works; reading throws `"Property Get method was not found."` — outside a `try`/`catch` that throw aborts the whole page | read | `Script.Util.HttpRequest.postData` |
|
|
16
|
+
| `write-only-opaque` | Assignment works; reading returns an opaque CLR value instead of the assigned string (no throw) | read | `Platform.Response.ContentType`, `Platform.Response.CharacterSet` |
|
|
17
|
+
| `read-only` | Reading works; assignment is silently ineffective | write | `Platform.Request.Browser`, `ClientIP`, `HasSSL`, `IsSSL`, `Method`, `QueryString`, `ReferrerURL`, `RequestURL`, `UserAgent` |
|
|
18
|
+
|
|
19
|
+
## Detection
|
|
20
|
+
|
|
21
|
+
The rule tracks data flow for HTTP instances, so it only fires on a genuine request object:
|
|
22
|
+
|
|
23
|
+
1. A **request** variable is one assigned from `new Script.Util.HttpRequest(...)` or
|
|
24
|
+
`Script.Util.HttpGet(...)`. A property read on an untracked variable is ignored.
|
|
25
|
+
2. `Platform.Request.*` / `Platform.Response.*` are matched on the literal member path.
|
|
26
|
+
3. A member expression that is the left-hand side of an assignment counts as a **write**;
|
|
27
|
+
any other member access counts as a **read**.
|
|
28
|
+
|
|
29
|
+
Related: [`sfmc/ssjs-no-property-call`](no-property-call.md) reports the *call* form of a
|
|
30
|
+
read-only write (`Platform.Request.Method("POST")`). This rule handles the *assignment*
|
|
31
|
+
form (`Platform.Request.Method = "POST"`), which that rule does not see. There is no
|
|
32
|
+
overlap.
|
|
33
|
+
|
|
34
|
+
## Examples
|
|
35
|
+
|
|
36
|
+
### ❌ Incorrect
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
var req = new Script.Util.HttpRequest("https://api.example.com/data");
|
|
40
|
+
req.postData = Stringify(payload);
|
|
41
|
+
Write(req.postData); // ← throws and aborts the page
|
|
42
|
+
|
|
43
|
+
var contentType = Platform.Response.ContentType; // ← opaque CLR value, not the assigned string
|
|
44
|
+
|
|
45
|
+
Platform.Request.Method = "POST"; // ← read-only, has no effect
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### ✅ Correct
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
var req = new Script.Util.HttpRequest("https://api.example.com/data");
|
|
52
|
+
var body = Stringify(payload);
|
|
53
|
+
req.postData = body;
|
|
54
|
+
Write(body); // keep your own variable
|
|
55
|
+
|
|
56
|
+
Platform.Response.ContentType = "application/json";
|
|
57
|
+
|
|
58
|
+
var method = String(Platform.Request.Method);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Fix
|
|
62
|
+
|
|
63
|
+
Not fixable and no suggestions — the correct fix is to keep the value in your own
|
|
64
|
+
JavaScript variable (or to drop the ineffective assignment), which is a refactor rather
|
|
65
|
+
than a mechanical edit.
|
|
66
|
+
|
|
67
|
+
## Rule details
|
|
68
|
+
|
|
69
|
+
- **Type:** `problem`
|
|
70
|
+
- **Fixable:** No
|
|
71
|
+
- **Recommended:** Yes (`error`)
|
|
72
|
+
- **Strict:** Yes (`error`)
|
|
73
|
+
- **MCN:** Off (SSJS is not supported on Marketing Cloud Next)
|
|
@@ -9,6 +9,11 @@ The rule is fixable:
|
|
|
9
9
|
- **Single-arg calls on `Platform.Response.*`** (`ContentType("text/html")`) → rewrites to an assignment (`= value`), including when the call appears in a comma (`SequenceExpression`) expression.
|
|
10
10
|
- **Any-arg calls on `Platform.Request.*`** → reported as an error with no auto-fix, because `Request` properties are read-only.
|
|
11
11
|
|
|
12
|
+
This rule only sees the **call** form. The assignment form of the same mistake
|
|
13
|
+
(`Platform.Request.Method = "POST"`), and reads that return nothing useful
|
|
14
|
+
(`var ct = Platform.Response.ContentType`), are handled by
|
|
15
|
+
[`sfmc/ssjs-no-invalid-property-access`](no-invalid-property-access.md).
|
|
16
|
+
|
|
12
17
|
## Properties affected
|
|
13
18
|
|
|
14
19
|
### `Platform.Request` — read-only
|
|
@@ -25,7 +30,7 @@ The rule is fixable:
|
|
|
25
30
|
| `RequestURL` |
|
|
26
31
|
| `UserAgent` |
|
|
27
32
|
|
|
28
|
-
### `Platform.Response` —
|
|
33
|
+
### `Platform.Response` — writable (reads return an opaque value)
|
|
29
34
|
|
|
30
35
|
| Property |
|
|
31
36
|
|---|
|
|
@@ -82,7 +87,6 @@ Platform.Request.Method("POST"); // ← read-only, cannot be set
|
|
|
82
87
|
// Reading properties — no parentheses
|
|
83
88
|
var method = Platform.Request.Method;
|
|
84
89
|
var ip = Platform.Request.ClientIP;
|
|
85
|
-
var ct = Platform.Response.ContentType;
|
|
86
90
|
|
|
87
91
|
// Setting writable Response properties — use assignment
|
|
88
92
|
Platform.Response.ContentType = "application/json";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-sfmc",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.11.0",
|
|
4
4
|
"description": "ESLint plugin for Salesforce Marketing Cloud Engagement+Next - AMPscript, Server-Side JavaScript (SSJS) and Handlebars",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"eslint-plugin-mso-email": "^2.0.0",
|
|
43
43
|
"handlebars-data": "^0.3.1",
|
|
44
44
|
"mso-conditional-parser": "^2.0.1",
|
|
45
|
-
"ssjs-data": "^1.
|
|
45
|
+
"ssjs-data": "^1.5.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"eslint": ">=9.0.0"
|
package/src/index.js
CHANGED
|
@@ -61,6 +61,7 @@ import ssjsNoNonfunctionalMethod from './rules/ssjs/ssjs-no-nonfunctional-method
|
|
|
61
61
|
import ssjsNoClrHeaderAccess from './rules/ssjs/no-clr-header-access.js';
|
|
62
62
|
import ssjsRequireStringClrContent from './rules/ssjs/require-string-clr-content.js';
|
|
63
63
|
import ssjsHttpPropertyValue from './rules/ssjs/http-property-value.js';
|
|
64
|
+
import ssjsNoInvalidPropertyAccess from './rules/ssjs/no-invalid-property-access.js';
|
|
64
65
|
|
|
65
66
|
// ── Handlebars (MCN) rules ──────────────────────────────────────────────────────
|
|
66
67
|
|
|
@@ -145,6 +146,7 @@ const plugin = {
|
|
|
145
146
|
'ssjs-no-clr-header-access': ssjsNoClrHeaderAccess,
|
|
146
147
|
'ssjs-require-string-clr-content': ssjsRequireStringClrContent,
|
|
147
148
|
'ssjs-http-property-value': ssjsHttpPropertyValue,
|
|
149
|
+
'ssjs-no-invalid-property-access': ssjsNoInvalidPropertyAccess,
|
|
148
150
|
|
|
149
151
|
// Handlebars (MCN) rules (hbs- prefix)
|
|
150
152
|
'hbs-no-unknown-helper': hbsNoUnknownHelper,
|
|
@@ -193,6 +195,7 @@ const ssjsMcnRules = {
|
|
|
193
195
|
'sfmc/ssjs-no-clr-header-access': 'off',
|
|
194
196
|
'sfmc/ssjs-require-string-clr-content': 'off',
|
|
195
197
|
'sfmc/ssjs-http-property-value': 'off',
|
|
198
|
+
'sfmc/ssjs-no-invalid-property-access': 'off',
|
|
196
199
|
'no-cond-assign': 'off',
|
|
197
200
|
};
|
|
198
201
|
|
|
@@ -266,6 +269,7 @@ const ssjsRecommendedRules = {
|
|
|
266
269
|
'sfmc/ssjs-no-clr-header-access': 'error',
|
|
267
270
|
'sfmc/ssjs-require-string-clr-content': 'error',
|
|
268
271
|
'sfmc/ssjs-http-property-value': 'error',
|
|
272
|
+
'sfmc/ssjs-no-invalid-property-access': 'error',
|
|
269
273
|
'no-cond-assign': 'error',
|
|
270
274
|
};
|
|
271
275
|
|
|
@@ -292,6 +296,7 @@ const ssjsStrictRules = {
|
|
|
292
296
|
'sfmc/ssjs-no-clr-header-access': 'error',
|
|
293
297
|
'sfmc/ssjs-require-string-clr-content': 'error',
|
|
294
298
|
'sfmc/ssjs-http-property-value': 'error',
|
|
299
|
+
'sfmc/ssjs-no-invalid-property-access': 'error',
|
|
295
300
|
'no-cond-assign': 'error',
|
|
296
301
|
};
|
|
297
302
|
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rule: ssjs-no-invalid-property-access
|
|
3
|
+
*
|
|
4
|
+
* Flags property accesses that go against the direction the runtime supports.
|
|
5
|
+
* Three distinct runtime behaviours, all driven by the `access` field in
|
|
6
|
+
* ssjs-data (`propertyAccessLookup`) so flagging a future property is a
|
|
7
|
+
* one-field data change:
|
|
8
|
+
*
|
|
9
|
+
* 1. write-only — assignment works, reading THROWS:
|
|
10
|
+
* req.postData = body; // fine
|
|
11
|
+
* Write(req.postData); // throws "Property Get method was not
|
|
12
|
+
* // found." and aborts the whole page
|
|
13
|
+
*
|
|
14
|
+
* 2. write-only-opaque — assignment works, reading returns an opaque CLR
|
|
15
|
+
* value instead of the assigned string (no throw):
|
|
16
|
+
* Platform.Response.ContentType = "application/json"; // fine
|
|
17
|
+
* var ct = Platform.Response.ContentType; // opaque value
|
|
18
|
+
*
|
|
19
|
+
* 3. read-only — reading works, assignment is silently ineffective:
|
|
20
|
+
* var m = Platform.Request.Method; // fine
|
|
21
|
+
* Platform.Request.Method = "POST"; // no effect
|
|
22
|
+
*
|
|
23
|
+
* Note `ssjs-no-property-call` already reports the CALL form of a read-only
|
|
24
|
+
* write (`Platform.Request.Method("POST")`). This rule handles the ASSIGNMENT
|
|
25
|
+
* form, which that rule does not see. There is no overlap.
|
|
26
|
+
*
|
|
27
|
+
* Not fixable — the correct fix (keep the value in your own variable) is a
|
|
28
|
+
* refactor, not a mechanical edit.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { propertyAccessLookup } from 'ssjs-data';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Returns true when the node is `Script.Util.HttpRequest` or
|
|
35
|
+
* `Script.Util.HttpGet` (a MemberExpression, optionally the callee of `new`).
|
|
36
|
+
*
|
|
37
|
+
* @param {import('eslint').Rule.Node} node - MemberExpression to test
|
|
38
|
+
* @returns {boolean} Whether it references a Script.Util HTTP constructor
|
|
39
|
+
*/
|
|
40
|
+
function isHttpConstructorMember(node) {
|
|
41
|
+
return (
|
|
42
|
+
node.type === 'MemberExpression' &&
|
|
43
|
+
node.property.type === 'Identifier' &&
|
|
44
|
+
(node.property.name === 'HttpRequest' || node.property.name === 'HttpGet') &&
|
|
45
|
+
node.object.type === 'MemberExpression' &&
|
|
46
|
+
node.object.property.type === 'Identifier' &&
|
|
47
|
+
node.object.property.name === 'Util' &&
|
|
48
|
+
node.object.object.type === 'Identifier' &&
|
|
49
|
+
node.object.object.name === 'Script'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns true when the node constructs a Script.Util HTTP request, i.e.
|
|
55
|
+
* `new Script.Util.HttpRequest(...)` or `Script.Util.HttpGet(...)`.
|
|
56
|
+
*
|
|
57
|
+
* @param {import('eslint').Rule.Node} node - init expression of a declarator
|
|
58
|
+
* @returns {boolean} Whether the expression yields an HTTP request instance
|
|
59
|
+
*/
|
|
60
|
+
function isHttpRequestInit(node) {
|
|
61
|
+
if (!node) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (node.type === 'NewExpression' || node.type === 'CallExpression') {
|
|
65
|
+
return isHttpConstructorMember(node.callee);
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Resolves the owner namespace of a member expression's object, but only for
|
|
72
|
+
* the shapes this rule tracks: a `Platform.Request` / `Platform.Response`
|
|
73
|
+
* member expression, or an identifier holding a Script.Util HTTP instance.
|
|
74
|
+
*
|
|
75
|
+
* @param {import('eslint').Rule.Node} objectNode - the `object` of the MemberExpression
|
|
76
|
+
* @param {Set.<string>} requestVariables - identifiers holding an HTTP request instance
|
|
77
|
+
* @returns {string|null} Qualified owner name, or null when not tracked
|
|
78
|
+
*/
|
|
79
|
+
function resolveOwner(objectNode, requestVariables) {
|
|
80
|
+
if (objectNode.type === 'Identifier') {
|
|
81
|
+
return requestVariables.has(objectNode.name) ? 'Script.Util.HttpRequest' : null;
|
|
82
|
+
}
|
|
83
|
+
if (
|
|
84
|
+
objectNode.type === 'MemberExpression' &&
|
|
85
|
+
!objectNode.computed &&
|
|
86
|
+
objectNode.object.type === 'Identifier' &&
|
|
87
|
+
objectNode.object.name === 'Platform' &&
|
|
88
|
+
objectNode.property.type === 'Identifier'
|
|
89
|
+
) {
|
|
90
|
+
const ns = objectNode.property.name;
|
|
91
|
+
if (ns === 'Request' || ns === 'Response') {
|
|
92
|
+
return `Platform.${ns}`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Looks up the access restriction for a member expression, or null when the
|
|
100
|
+
* accessed member has no restriction (or is not on a tracked owner).
|
|
101
|
+
*
|
|
102
|
+
* @param {import('eslint').Rule.Node} node - the MemberExpression being accessed
|
|
103
|
+
* @param {Set.<string>} requestVariables - identifiers holding an HTTP request instance
|
|
104
|
+
* @returns {{name: string, owner: string, access: string}|null} the matching entry or null
|
|
105
|
+
*/
|
|
106
|
+
function lookupAccess(node, requestVariables) {
|
|
107
|
+
if (node.computed || node.property.type !== 'Identifier') {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const owner = resolveOwner(node.object, requestVariables);
|
|
111
|
+
if (!owner) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return propertyAccessLookup.get(`${owner}.${node.property.name}`.toLowerCase()) ?? null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns true when the member expression is the target of an assignment
|
|
119
|
+
* (`x.y = …`, including compound assignments such as `x.y += …`).
|
|
120
|
+
*
|
|
121
|
+
* @param {import('eslint').Rule.Node} node - the MemberExpression to test
|
|
122
|
+
* @returns {boolean} Whether the node is an assignment target
|
|
123
|
+
*/
|
|
124
|
+
function isAssignmentTarget(node) {
|
|
125
|
+
return node.parent.type === 'AssignmentExpression' && node.parent.left === node;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const MESSAGE_BY_ACCESS = {
|
|
129
|
+
'write-only': 'writeOnlyRead',
|
|
130
|
+
'write-only-opaque': 'opaqueRead',
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export default {
|
|
134
|
+
meta: {
|
|
135
|
+
type: 'problem',
|
|
136
|
+
docs: {
|
|
137
|
+
description:
|
|
138
|
+
'Disallow reading write-only SSJS properties (e.g. postData) and assigning to read-only ones (e.g. Platform.Request.Method)',
|
|
139
|
+
},
|
|
140
|
+
messages: {
|
|
141
|
+
writeOnlyRead:
|
|
142
|
+
"'{{owner}}.{{name}}' is write-only. Reading it throws " +
|
|
143
|
+
'"Property Get method was not found." at runtime — outside a try/catch that throw ' +
|
|
144
|
+
'aborts the whole page. Keep the value in your own variable instead.',
|
|
145
|
+
opaqueRead:
|
|
146
|
+
"'{{owner}}.{{name}}' does not read back the value you assigned — the runtime " +
|
|
147
|
+
'returns an opaque CLR value. Keep the value in your own variable instead.',
|
|
148
|
+
readOnlyWrite: "'{{owner}}.{{name}}' is read-only. Assigning to it has no effect.",
|
|
149
|
+
},
|
|
150
|
+
schema: [],
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
create(context) {
|
|
154
|
+
const requestVariables = new Set();
|
|
155
|
+
const pending = [];
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
VariableDeclarator(node) {
|
|
159
|
+
if (node.id.type === 'Identifier' && isHttpRequestInit(node.init)) {
|
|
160
|
+
requestVariables.add(node.id.name);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
MemberExpression(node) {
|
|
165
|
+
const entry = lookupAccess(node, requestVariables);
|
|
166
|
+
if (!entry) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
// Reads of a write-only property: the node must NOT be the
|
|
170
|
+
// left-hand side of an assignment — that direction is allowed.
|
|
171
|
+
// The call form (`Platform.Response.ContentType()`) belongs to
|
|
172
|
+
// `ssjs-no-property-call`, so it is skipped here.
|
|
173
|
+
if (
|
|
174
|
+
entry.access === 'read-only' ||
|
|
175
|
+
isAssignmentTarget(node) ||
|
|
176
|
+
(node.parent.type === 'CallExpression' && node.parent.callee === node)
|
|
177
|
+
) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
pending.push({ node, entry, messageId: MESSAGE_BY_ACCESS[entry.access] });
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
AssignmentExpression(node) {
|
|
184
|
+
if (node.left.type !== 'MemberExpression') {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const entry = lookupAccess(node.left, requestVariables);
|
|
188
|
+
if (!entry || entry.access !== 'read-only') {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
pending.push({ node: node.left, entry, messageId: 'readOnlyWrite' });
|
|
192
|
+
},
|
|
193
|
+
|
|
194
|
+
'Program:exit'() {
|
|
195
|
+
for (const { node, entry, messageId } of pending) {
|
|
196
|
+
context.report({
|
|
197
|
+
node,
|
|
198
|
+
messageId,
|
|
199
|
+
data: { owner: entry.owner, name: entry.name },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
},
|
|
205
|
+
};
|