@autotelic/plumb 0.5.2 → 0.6.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.
@@ -23,6 +23,8 @@ import { preferEffectMatchRule } from "./rules/prefer-effect-match.js";
23
23
  import { noNestedLayerProvideRule } from "./rules/no-nested-layer-provide.js";
24
24
  import { noCascadingLayerProvideRule } from "./rules/no-cascading-layer-provide.js";
25
25
  import { noStaticEffectServiceForwardersRule } from "./rules/no-static-effect-service-forwarders.js";
26
+ import { noRawErrorForwardingRule } from "./rules/no-raw-error-forwarding.js";
27
+ import { noNullishDefaultOnPartialInputRule } from "./rules/no-nullish-default-on-partial-input.js";
26
28
  /** Opt-in Oxlint rules for Effect service and Layer architecture. */
27
29
  const plumbEffectPlugin = eslintCompatPlugin({
28
30
  meta: { name: "plumb-effect" },
@@ -51,6 +53,8 @@ const plumbEffectPlugin = eslintCompatPlugin({
51
53
  "no-nested-layer-provide": noNestedLayerProvideRule,
52
54
  "no-cascading-layer-provide": noCascadingLayerProvideRule,
53
55
  "no-static-effect-service-forwarders": noStaticEffectServiceForwardersRule,
56
+ "no-raw-error-forwarding": noRawErrorForwardingRule,
57
+ "no-nullish-default-on-partial-input": noNullishDefaultOnPartialInputRule,
54
58
  },
55
59
  });
56
60
  export default plumbEffectPlugin;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Disallow using nullish coalescing (`??`) with a literal default on a
3
+ * member expression of an external input object. In partial updates
4
+ * (PATCH/PUT), a field might be intentionally absent; `?? false` conflates
5
+ * "absent" with "explicitly false", so you can't distinguish "don't change
6
+ * this field" from "set it to false".
7
+ *
8
+ * This pattern re-invent's Effect's Schema: it manually detects absence where
9
+ * `Schema.optional` / `Schema.nullable` would distinguish absent from falsy
10
+ * with full type safety. The schema also centralizes the rule so it can't
11
+ * drift between the API and the UI.
12
+ */
13
+ export declare const noNullishDefaultOnPartialInputRule: import("@oxlint/plugins").Rule;
@@ -0,0 +1,109 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import { firstOptionRecord } from "../../shared/rule-options.js";
3
+ import { isString } from "../../shared/structural.js";
4
+ const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
5
+ /** Object names that suggest external input: request bodies, params, etc. */
6
+ const DEFAULT_INPUT_NAMES = new Set([
7
+ "body",
8
+ "params",
9
+ "query",
10
+ "data",
11
+ "input",
12
+ "payload",
13
+ ]);
14
+ /**
15
+ * Whether a node is a literal value (boolean, string, number, null).
16
+ * @param {ESTree.Node} node - The node to test.
17
+ * @returns {boolean} True when the node is a literal.
18
+ */
19
+ function isLiteral(node) {
20
+ return node.type === "Literal";
21
+ }
22
+ /**
23
+ * The display string for a literal value.
24
+ * @param {ESTree.Expression} node - The literal node.
25
+ * @returns {string} The display string.
26
+ */
27
+ function literalDisplay(node) {
28
+ if (node.type === "Literal" && node.value === null)
29
+ return "null";
30
+ if (node.type === "Literal")
31
+ return String(node.value);
32
+ if (node.type === "UnaryExpression" && node.argument.type === "Literal")
33
+ return String(node.argument.value);
34
+ return "unknown";
35
+ }
36
+ /**
37
+ * Disallow using nullish coalescing (`??`) with a literal default on a
38
+ * member expression of an external input object. In partial updates
39
+ * (PATCH/PUT), a field might be intentionally absent; `?? false` conflates
40
+ * "absent" with "explicitly false", so you can't distinguish "don't change
41
+ * this field" from "set it to false".
42
+ *
43
+ * This pattern re-invent's Effect's Schema: it manually detects absence where
44
+ * `Schema.optional` / `Schema.nullable` would distinguish absent from falsy
45
+ * with full type safety. The schema also centralizes the rule so it can't
46
+ * drift between the API and the UI.
47
+ */
48
+ export const noNullishDefaultOnPartialInputRule = defineRule({
49
+ meta: {
50
+ type: "problem",
51
+ docs: {
52
+ description: "Disallow nullish coalescing (??) with literal defaults on member expressions of external input objects; this conflates 'absent' with 'falsy' in partial updates and re-invent's Effect's Schema. Use Schema.optional / Schema.nullable to distinguish absent from falsy.",
53
+ },
54
+ messages: {
55
+ nullishDefaultOnPartialInput: "`?? {{default}}` on `{{object}}.{{property}}` conflates 'absent' with '{{default}}' in partial updates and re-invent's Effect's Schema. Use `Schema.optional` / `Schema.nullable` to distinguish absent from falsy with full type safety.",
56
+ },
57
+ schema: [
58
+ {
59
+ type: "object",
60
+ properties: {
61
+ allow: { type: "array", items: { type: "string" } },
62
+ },
63
+ additionalProperties: false,
64
+ },
65
+ ],
66
+ defaultOptions: [{ allow: [] }],
67
+ },
68
+ createOnce(context) {
69
+ const option = firstOptionRecord(context.options);
70
+ const allowed = new Set(Array.isArray(option.allow) ? option.allow.filter(isString) : []);
71
+ const inputNames = new Set(DEFAULT_INPUT_NAMES);
72
+ for (const name of allowed)
73
+ inputNames.delete(name);
74
+ return {
75
+ before() {
76
+ if (TEST_FILE.test(context.filename.replaceAll("\\\\", "/")))
77
+ return false;
78
+ },
79
+ LogicalExpression(node) {
80
+ if (node.operator !== "??")
81
+ return;
82
+ if (node.left.type !== "MemberExpression")
83
+ return;
84
+ if (!isLiteral(node.right))
85
+ return;
86
+ const member = node.left;
87
+ if (member.object.type !== "Identifier")
88
+ return;
89
+ const objectName = member.object.name;
90
+ if (!inputNames.has(objectName))
91
+ return;
92
+ const propertyName = !member.computed && member.property.type === "Identifier"
93
+ ? member.property.name
94
+ : undefined;
95
+ if (propertyName === undefined)
96
+ return;
97
+ context.report({
98
+ node,
99
+ messageId: "nullishDefaultOnPartialInput",
100
+ data: {
101
+ default: literalDisplay(node.right),
102
+ object: objectName,
103
+ property: propertyName,
104
+ },
105
+ });
106
+ },
107
+ };
108
+ },
109
+ });
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Disallow forwarding raw error messages from caught exceptions. This pattern
3
+ * re-invent's Effect's typed error channel: raw strings leak internal details
4
+ * (stack traces, file paths, database errors) and lose structure so callers
5
+ * can't match on the failure kind.
6
+ *
7
+ * Use Effect's `TaggedError`: a tagged class per failure kind carrying
8
+ * structured evidence as fields, rendering its message from those fields. The
9
+ * error then flows through Effect's typed error channel instead of being
10
+ * stringified at every boundary.
11
+ */
12
+ export declare const noRawErrorForwardingRule: import("@oxlint/plugins").Rule;
@@ -0,0 +1,103 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import { ancestorsOf } from "../../shared/ancestors.js";
3
+ const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
4
+ /** Error properties that carry internal details and must not be forwarded raw. */
5
+ const ERROR_PROPS = new Set(["message", "stack"]);
6
+ /**
7
+ * Whether a member expression accesses an error property on a catch parameter.
8
+ * @param {ESTree.MemberExpression} member - The member expression to test.
9
+ * @param {ReadonlySet<string>} catchParams - Names of catch parameters in scope.
10
+ * @returns {boolean} True when the member accesses an error property on a catch param.
11
+ */
12
+ function isErrorPropAccess(member, catchParams) {
13
+ if (member.object.type !== "Identifier")
14
+ return false;
15
+ if (!catchParams.has(member.object.name))
16
+ return false;
17
+ if (member.computed)
18
+ return false;
19
+ return member.property.type === "Identifier" && ERROR_PROPS.has(member.property.name);
20
+ }
21
+ /**
22
+ * Whether the error property access is inside a console.* call (logging, not forwarding).
23
+ * @param {SourceCode} sourceCode - The rule's source-code accessor.
24
+ * @param {ESTree.Node} node - The node to test.
25
+ * @returns {boolean} True when the access is inside a console.* call.
26
+ */
27
+ function isLoggingCall(sourceCode, node) {
28
+ const chain = ancestorsOf(sourceCode, node);
29
+ for (let i = chain.length - 1; i >= 0; i--) {
30
+ const ancestor = chain[i];
31
+ if (ancestor === undefined)
32
+ continue;
33
+ if (ancestor.type === "CallExpression" && ancestor.callee.type === "MemberExpression") {
34
+ const obj = ancestor.callee.object;
35
+ if (obj.type === "Identifier" && obj.name === "console")
36
+ return true;
37
+ }
38
+ if (ancestor.type === "Program" || ancestor.type === "BlockStatement")
39
+ return false;
40
+ }
41
+ return false;
42
+ }
43
+ /**
44
+ * Disallow forwarding raw error messages from caught exceptions. This pattern
45
+ * re-invent's Effect's typed error channel: raw strings leak internal details
46
+ * (stack traces, file paths, database errors) and lose structure so callers
47
+ * can't match on the failure kind.
48
+ *
49
+ * Use Effect's `TaggedError`: a tagged class per failure kind carrying
50
+ * structured evidence as fields, rendering its message from those fields. The
51
+ * error then flows through Effect's typed error channel instead of being
52
+ * stringified at every boundary.
53
+ */
54
+ export const noRawErrorForwardingRule = defineRule({
55
+ meta: {
56
+ type: "problem",
57
+ docs: {
58
+ description: "Disallow forwarding raw error messages (e.message, e.stack) from caught exceptions; this pattern re-invent's Effect's typed error channel. Use Effect's TaggedError so failures carry structured evidence and flow through the typed channel.",
59
+ },
60
+ messages: {
61
+ rawErrorForwarding: "Forwarding the raw error '{{prop}}' re-invent's Effect's typed error channel: it leaks internal details and loses structure so callers can't match on the failure kind. Use Effect's TaggedError (a tagged class per failure kind carrying structured evidence as fields) so the error flows through the typed channel.",
62
+ },
63
+ },
64
+ createOnce(context) {
65
+ const catchParamStack = [];
66
+ return {
67
+ before() {
68
+ if (TEST_FILE.test(context.filename.replaceAll("\\\\", "/")))
69
+ return false;
70
+ },
71
+ CatchClause(node) {
72
+ const param = node.param;
73
+ if (param !== null && param !== undefined && param.type === "Identifier") {
74
+ catchParamStack.push(new Set([param.name]));
75
+ }
76
+ else {
77
+ catchParamStack.push(new Set());
78
+ }
79
+ },
80
+ "CatchClause:exit"() {
81
+ catchParamStack.pop();
82
+ },
83
+ MemberExpression(node) {
84
+ if (catchParamStack.length === 0)
85
+ return;
86
+ const allParams = new Set();
87
+ for (const params of catchParamStack) {
88
+ for (const name of params)
89
+ allParams.add(name);
90
+ }
91
+ if (!isErrorPropAccess(node, allParams))
92
+ return;
93
+ if (isLoggingCall(context.sourceCode, node))
94
+ return;
95
+ context.report({
96
+ node,
97
+ messageId: "rawErrorForwarding",
98
+ data: { prop: node.property.type === "Identifier" ? node.property.name : "message" },
99
+ });
100
+ },
101
+ };
102
+ },
103
+ });
package/dist/index.js CHANGED
@@ -47,6 +47,7 @@ import { noMultipleFunctionParamsRule } from "./rules/no-multiple-function-param
47
47
  import { noOptionalFunctionParametersRule } from "./rules/no-optional-function-parameters.js";
48
48
  import { noSingleUsePrivateFunctionsRule } from "./rules/no-single-use-private-functions.js";
49
49
  import { noMutableEnvironmentCaptureRule } from "./rules/no-mutable-environment-capture.js";
50
+ import { noSqlStringInterpolationRule } from "./rules/no-sql-string-interpolation.js";
50
51
  /** Generic Oxlint rules that reject low-evidence and low-signal implementation patterns. */
51
52
  const plumbPlugin = eslintCompatPlugin({
52
53
  meta: { name: "plumb" },
@@ -99,6 +100,7 @@ const plumbPlugin = eslintCompatPlugin({
99
100
  "no-optional-function-parameters": noOptionalFunctionParametersRule,
100
101
  "no-single-use-private-functions": noSingleUsePrivateFunctionsRule,
101
102
  "no-mutable-environment-capture": noMutableEnvironmentCaptureRule,
103
+ "no-sql-string-interpolation": noSqlStringInterpolationRule,
102
104
  },
103
105
  });
104
106
  export default plumbPlugin;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Disallow SQL queries built from template literal interpolation. String
3
+ * interpolation in SQL is an injection vector: attacker-controlled values
4
+ * become query structure. Use parameterized queries or a query builder
5
+ * instead so values never become syntax.
6
+ */
7
+ export declare const noSqlStringInterpolationRule: import("@oxlint/plugins").Rule;
@@ -0,0 +1,62 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
3
+ /**
4
+ * SQL keywords that suggest a string is a query. Case-insensitive match
5
+ * against the raw template string portions.
6
+ */
7
+ const SQL_KEYWORDS = [
8
+ "SELECT",
9
+ "INSERT",
10
+ "UPDATE",
11
+ "DELETE",
12
+ "FROM",
13
+ "WHERE",
14
+ "JOIN",
15
+ "CREATE\\s+TABLE",
16
+ "DROP\\s+TABLE",
17
+ "ALTER\\s+TABLE",
18
+ ];
19
+ const SQL_PATTERN = new RegExp("\\b(?:" + SQL_KEYWORDS.join("|") + ")\\b", "iu");
20
+ /**
21
+ * Whether a template literal's raw text contains a SQL keyword.
22
+ * @param {ESTree.TemplateElement} quasi - The template element to test.
23
+ * @returns {boolean} True when the raw text contains a SQL keyword.
24
+ */
25
+ function hasSqlKeyword(quasi) {
26
+ return SQL_PATTERN.test(quasi.value.raw);
27
+ }
28
+ /**
29
+ * Disallow SQL queries built from template literal interpolation. String
30
+ * interpolation in SQL is an injection vector: attacker-controlled values
31
+ * become query structure. Use parameterized queries or a query builder
32
+ * instead so values never become syntax.
33
+ */
34
+ export const noSqlStringInterpolationRule = defineRule({
35
+ meta: {
36
+ type: "problem",
37
+ docs: {
38
+ description: "Disallow SQL queries built via template literal interpolation; use parameterized queries or a query builder to prevent SQL injection.",
39
+ },
40
+ messages: {
41
+ sqlInterpolation: "SQL via template literal interpolation is an injection vector. Use parameterized queries or a query builder so values never become syntax.",
42
+ },
43
+ },
44
+ createOnce(context) {
45
+ return {
46
+ before() {
47
+ if (TEST_FILE.test(context.filename.replaceAll("\\\\", "/")))
48
+ return false;
49
+ },
50
+ TemplateLiteral(node) {
51
+ if (node.expressions.length === 0)
52
+ return;
53
+ for (const quasi of node.quasis) {
54
+ if (hasSqlKeyword(quasi)) {
55
+ context.report({ node, messageId: "sqlInterpolation" });
56
+ return;
57
+ }
58
+ }
59
+ },
60
+ };
61
+ },
62
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autotelic/plumb",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Opinionated Oxlint rules that keep TypeScript true: generic and Effect-specific patterns, maintained as one source of truth for the autotelic repos.",
5
5
  "license": "MIT",
6
6
  "files": [