@sshkeda/check 4.0.1 → 4.1.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/docs/how-to-grant-a-permission.md +1 -0
- package/package.json +1 -1
- package/src/internal/confine-node-console-to-its-console-export.ts +77 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +4 -2
- package/src/internal/report-code-grants-nothing-uses.ts +32 -4
- package/src/shared/bans-each-permission-lifts.ts +23 -1
- package/src/shared/record-and-grant-permissions-by-path.ts +14 -1
|
@@ -25,4 +25,5 @@ export default lintConfig({
|
|
|
25
25
|
- `trustsUnverifiedTypes`: Allows trusting a value's type without checking it, through `Schema.declare`.
|
|
26
26
|
- `handlesEveryFailure`: Allows catching every error, through `Effect.catch`, `Effect.exit`, a forked fiber or `Effect.runPromise`.
|
|
27
27
|
- `fallsBackToFixedValues`: Allows answering every failure, or a whole library error such as `PlatformError`, with a fixed value that never reads the failure.
|
|
28
|
+
- `replacesTheConsole`: Allows `Console` from `node:console`, and reading, calling and assigning these members of the global `console` by name: `log`, `warn`, `error`, `info`, `debug`, `dir`, `dirxml`, `table`, `trace`, `assert`, `clear`, `count`, `countReset`, `group`, `groupCollapsed`, `groupEnd`, `time`, `timeEnd` and `timeLog`.
|
|
28
29
|
- `isGeneratedByATool`: Check skips this path, so it can't have any other permission.
|
package/package.json
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Context, CreateRule, ESTree } from "@oxlint/plugins";
|
|
2
|
+
import { Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
equivalentModuleSpecifiers,
|
|
6
|
+
REPLACES_THE_CONSOLE,
|
|
7
|
+
} from "../shared/bans-each-permission-lifts.ts";
|
|
8
|
+
import { CONSOLE_REPLACED } from "../shared/record-and-grant-permissions-by-path.ts";
|
|
9
|
+
import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
|
|
10
|
+
import {
|
|
11
|
+
importsOnlyTypes,
|
|
12
|
+
moduleExportName,
|
|
13
|
+
staticKeyName,
|
|
14
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
15
|
+
|
|
16
|
+
const NODE_CONSOLE = "node:console";
|
|
17
|
+
const CONSOLE_EXPORT = "Console";
|
|
18
|
+
const NODE_CONSOLE_SPECIFIERS: ReadonlySet<string> = new Set(
|
|
19
|
+
equivalentModuleSpecifiers(NODE_CONSOLE),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
const NODE_CONSOLE_OPTIONS = Schema.Struct({
|
|
23
|
+
console: Schema.optionalKey(Schema.Literal(CONSOLE_REPLACED)),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const isTheConsoleExport = (specifier: ESTree.ImportDeclaration["specifiers"][number]) =>
|
|
27
|
+
specifier.type === "ImportSpecifier" && moduleExportName(specifier.imported) === CONSOLE_EXPORT;
|
|
28
|
+
|
|
29
|
+
export const noNodeConsole = {
|
|
30
|
+
meta: {
|
|
31
|
+
type: "problem",
|
|
32
|
+
messages: {
|
|
33
|
+
outsideTheGrant: `${CONSOLE_EXPORT} from {{module}} is used outside a file allowed ${REPLACES_THE_CONSOLE}`,
|
|
34
|
+
forbiddenForm: `{{module}} is imported in a way no file may. A file allowed ${REPLACES_THE_CONSOLE} may import only ${CONSOLE_EXPORT} from ${NODE_CONSOLE}`,
|
|
35
|
+
},
|
|
36
|
+
schema: ruleOptionsJsonSchema(NODE_CONSOLE_OPTIONS),
|
|
37
|
+
},
|
|
38
|
+
create(context: Pick<Context, "options" | "report" | "sourceCode">) {
|
|
39
|
+
const isGranted =
|
|
40
|
+
decodeRuleOptions(NODE_CONSOLE_OPTIONS, context.options)?.console === CONSOLE_REPLACED;
|
|
41
|
+
const report = (source: ESTree.Node, module: string, isTheConsoleOnly: boolean) => {
|
|
42
|
+
if (!isTheConsoleOnly) {
|
|
43
|
+
context.report({ node: source, messageId: "forbiddenForm", data: { module } });
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (!isGranted) {
|
|
47
|
+
context.report({ node: source, messageId: "outsideTheGrant", data: { module } });
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
ImportDeclaration(node: ESTree.ImportDeclaration) {
|
|
52
|
+
const isNodeConsoleWithValues =
|
|
53
|
+
NODE_CONSOLE_SPECIFIERS.has(node.source.value) &&
|
|
54
|
+
!importsOnlyTypes(node, context.sourceCode);
|
|
55
|
+
if (!isNodeConsoleWithValues) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const valueSpecifiers = node.specifiers.filter(
|
|
59
|
+
(specifier) => specifier.type !== "ImportSpecifier" || specifier.importKind !== "type",
|
|
60
|
+
);
|
|
61
|
+
report(
|
|
62
|
+
node.source,
|
|
63
|
+
node.source.value,
|
|
64
|
+
valueSpecifiers.length > 0 &&
|
|
65
|
+
valueSpecifiers.every((specifier) => isTheConsoleExport(specifier)),
|
|
66
|
+
);
|
|
67
|
+
},
|
|
68
|
+
ImportExpression(node: ESTree.ImportExpression) {
|
|
69
|
+
const module = staticKeyName(node.source);
|
|
70
|
+
const loadsNodeConsole = module !== undefined && NODE_CONSOLE_SPECIFIERS.has(module);
|
|
71
|
+
if (loadsNodeConsole) {
|
|
72
|
+
report(node.source, module, false);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
} satisfies CreateRule;
|
|
@@ -2,6 +2,7 @@ import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
|
2
2
|
import { Record } from "effect";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
CONSOLE,
|
|
5
6
|
everyBanOf,
|
|
6
7
|
IMPORT_META,
|
|
7
8
|
moduleSpecifiersOfGlobalObject,
|
|
@@ -18,6 +19,7 @@ import {
|
|
|
18
19
|
permissionBanRuleId,
|
|
19
20
|
FAILURES_AS_RESULTS_RULE,
|
|
20
21
|
FIXED_FALLBACKS_RULE,
|
|
22
|
+
NODE_CONSOLE_RULE,
|
|
21
23
|
STDIO_SERVICE_RULE,
|
|
22
24
|
TEST_TIMEOUTS_RULE,
|
|
23
25
|
TYPE_PREDICATES_RULE,
|
|
@@ -60,6 +62,7 @@ import { noVagueFileNames } from "./ban-vague-file-names.ts";
|
|
|
60
62
|
import { permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
|
|
61
63
|
import { noWhile } from "./ban-while-do-while-and-for-loops.ts";
|
|
62
64
|
import { noSharedHolders } from "./ban-writing-captured-objects-inside-effect-callbacks.ts";
|
|
65
|
+
import { noNodeConsole } from "./confine-node-console-to-its-console-export.ts";
|
|
63
66
|
import {
|
|
64
67
|
noServiceDeclarations,
|
|
65
68
|
noUnusedServiceGrants,
|
|
@@ -105,8 +108,6 @@ export const RESTRICTED_IMPORT_PATHS = RESTRICTED_PROPERTIES.flatMap(({ object,
|
|
|
105
108
|
})),
|
|
106
109
|
);
|
|
107
110
|
|
|
108
|
-
const CONSOLE = "console";
|
|
109
|
-
|
|
110
111
|
const GLOBALS_WHOSE_MEMBERS_OTHER_RULES_BAN = ["Atomics", "Intl", "Number"] as const;
|
|
111
112
|
|
|
112
113
|
const GLOBALS_WHOSE_MEMBERS_PERMISSIONS_BAN = Object.values(PERMISSION_BANS)
|
|
@@ -147,6 +148,7 @@ const NAMED_RULES = {
|
|
|
147
148
|
"no-declared-values": noDeclaredValues,
|
|
148
149
|
[TYPE_PREDICATES_RULE]: noTypePredicates,
|
|
149
150
|
[STDIO_SERVICE_RULE]: noStdioService,
|
|
151
|
+
[NODE_CONSOLE_RULE]: noNodeConsole,
|
|
150
152
|
[FAILURES_AS_RESULTS_RULE]: noFailuresAsResults,
|
|
151
153
|
"no-overloads": noOverloads,
|
|
152
154
|
"no-re-exports": noReExports,
|
|
@@ -2,6 +2,7 @@ import type { Context, CreateRule, Visitor } from "@oxlint/plugins";
|
|
|
2
2
|
import { Schema } from "effect";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
+
CONSOLE,
|
|
5
6
|
HANDLES_EVERY_FAILURE,
|
|
6
7
|
PERMISSION_BANS,
|
|
7
8
|
RULES_EACH_PERMISSION_LIFTS,
|
|
@@ -14,6 +15,8 @@ import {
|
|
|
14
15
|
CODE_GRANTS,
|
|
15
16
|
FAILURES_AS_RESULTS_RULE,
|
|
16
17
|
INSIDE_THE_TEST_HOME,
|
|
18
|
+
NO_CONSOLE_RULE,
|
|
19
|
+
NODE_CONSOLE_RULE,
|
|
17
20
|
rulesACodeGrantChanges,
|
|
18
21
|
SCHEDULES_RULE,
|
|
19
22
|
SLEEP_RULE,
|
|
@@ -30,17 +33,41 @@ import { noClockReadsAndSleeps } from "./ban-reading-the-clock-and-sleeping-outs
|
|
|
30
33
|
import { noSchedules } from "./ban-timer-schedules-without-deadlines.ts";
|
|
31
34
|
import { noTypePredicates } from "./ban-type-predicates.ts";
|
|
32
35
|
import { mergedVisitors, permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
|
|
36
|
+
import { noNodeConsole } from "./confine-node-console-to-its-console-export.ts";
|
|
33
37
|
import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
|
|
38
|
+
import { isGlobalReference, memberName } from "./resolve-what-an-expression-imports.ts";
|
|
34
39
|
|
|
35
|
-
const
|
|
40
|
+
const NO_CONSOLE_OPTIONS = Schema.Struct({
|
|
41
|
+
allow: Schema.optionalKey(Schema.Array(Schema.String)),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const consoleMembersNoConsoleReports = {
|
|
45
|
+
create(context: Pick<Context, "options" | "report" | "sourceCode">) {
|
|
46
|
+
const allowed = new Set(decodeRuleOptions(NO_CONSOLE_OPTIONS, context.options)?.allow);
|
|
47
|
+
return {
|
|
48
|
+
MemberExpression(node) {
|
|
49
|
+
const name = memberName(node);
|
|
50
|
+
const isReported =
|
|
51
|
+
isGlobalReference(node.object, context.sourceCode, CONSOLE) &&
|
|
52
|
+
(name === undefined || !allowed.has(name));
|
|
53
|
+
if (isReported) {
|
|
54
|
+
context.report({ node, message: name ?? CONSOLE });
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
} satisfies Visitor;
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const RULES_CODE_GRANTS_CHANGE = new Map<
|
|
36
62
|
string,
|
|
37
63
|
{
|
|
38
64
|
readonly create: (context: Pick<Context, "options" | "report" | "sourceCode">) => Visitor;
|
|
39
65
|
}
|
|
40
|
-
>
|
|
41
|
-
Object.entries({
|
|
66
|
+
>([
|
|
67
|
+
...Object.entries({
|
|
42
68
|
[PERMISSION_BANS[HANDLES_EVERY_FAILURE].ruleName]: permissionBanRule(HANDLES_EVERY_FAILURE),
|
|
43
69
|
[PERMISSION_BANS[TRUSTS_UNVERIFIED_TYPES].ruleName]: permissionBanRule(TRUSTS_UNVERIFIED_TYPES),
|
|
70
|
+
[NODE_CONSOLE_RULE]: noNodeConsole,
|
|
44
71
|
[FAILURES_AS_RESULTS_RULE]: noFailuresAsResults,
|
|
45
72
|
[UNNAMED_FALLBACKS_RULE]: noUnnamedFallbacks,
|
|
46
73
|
[SLEEP_RULE]: effectOperationRule(SLEEP_RULE),
|
|
@@ -50,7 +77,8 @@ const RULES_CODE_GRANTS_CHANGE: ReadonlyMap<
|
|
|
50
77
|
[RULES_EACH_PERMISSION_LIFTS[SHORT_NAMES]]: noAbbreviations,
|
|
51
78
|
[TYPE_PREDICATES_RULE]: noTypePredicates,
|
|
52
79
|
}).map(([name, rule]) => [`sshkeda/${name}`, rule] as const),
|
|
53
|
-
|
|
80
|
+
[NO_CONSOLE_RULE, consoleMembersNoConsoleReports],
|
|
81
|
+
]);
|
|
54
82
|
|
|
55
83
|
const UNUSED_CODE_GRANT_OPTIONS = Schema.Struct({
|
|
56
84
|
permissions: Schema.optionalKey(Schema.Array(Schema.Literals(CODE_GRANTS))),
|
|
@@ -109,6 +109,29 @@ export const CREATE_REQUIRE_VALUE_USAGE = "createRequire used other than by a ca
|
|
|
109
109
|
|
|
110
110
|
export const TRUSTS_UNVERIFIED_TYPES = "trustsUnverifiedTypes";
|
|
111
111
|
export const WRITES_TO_THE_TERMINAL = "writesToTheTerminal";
|
|
112
|
+
export const REPLACES_THE_CONSOLE = "replacesTheConsole";
|
|
113
|
+
export const CONSOLE = "console";
|
|
114
|
+
export const CONSOLE_METHODS = [
|
|
115
|
+
"log",
|
|
116
|
+
"warn",
|
|
117
|
+
"error",
|
|
118
|
+
"info",
|
|
119
|
+
"debug",
|
|
120
|
+
"dir",
|
|
121
|
+
"dirxml",
|
|
122
|
+
"table",
|
|
123
|
+
"trace",
|
|
124
|
+
"assert",
|
|
125
|
+
"clear",
|
|
126
|
+
"count",
|
|
127
|
+
"countReset",
|
|
128
|
+
"group",
|
|
129
|
+
"groupCollapsed",
|
|
130
|
+
"groupEnd",
|
|
131
|
+
"time",
|
|
132
|
+
"timeEnd",
|
|
133
|
+
"timeLog",
|
|
134
|
+
] as const;
|
|
112
135
|
export const READS_CONFIG = "readsConfig";
|
|
113
136
|
const LOCKS_FILES = "locksFiles";
|
|
114
137
|
export const HANDLES_EVERY_FAILURE = "handlesEveryFailure";
|
|
@@ -344,7 +367,6 @@ const GRANTABLE_BANS = {
|
|
|
344
367
|
"{{usage}} writes to the terminal in a way no file may; write through the Effect Stdio service in a file allowed writesToTheTerminal",
|
|
345
368
|
bans: [
|
|
346
369
|
effectMembersBan([["Console"]]),
|
|
347
|
-
moduleBan(["node:console"]),
|
|
348
370
|
globalMembersBan("process", ["stdout", "stderr"]),
|
|
349
371
|
globalMembersBan("Bun", ["stdout", "stderr"]),
|
|
350
372
|
],
|
|
@@ -3,10 +3,12 @@ import { Array as Arrays, Result, Schema, SchemaIssue } from "effect";
|
|
|
3
3
|
import {
|
|
4
4
|
BANS_NO_GRANT_LIFTS,
|
|
5
5
|
bansNoGrantLifts,
|
|
6
|
+
CONSOLE_METHODS,
|
|
6
7
|
GRANTABLE_BAN_NAMES,
|
|
7
8
|
HANDLES_EVERY_FAILURE,
|
|
8
9
|
PERMISSION_BANS,
|
|
9
10
|
READS_CONFIG,
|
|
11
|
+
REPLACES_THE_CONSOLE,
|
|
10
12
|
RULES_EACH_PERMISSION_LIFTS,
|
|
11
13
|
SHORT_NAMES,
|
|
12
14
|
TRUSTS_UNVERIFIED_TYPES,
|
|
@@ -29,6 +31,9 @@ export const UNNAMED_FALLBACKS_RULE = "no-unnamed-fallbacks";
|
|
|
29
31
|
export const FIXED_FALLBACKS = "fallsBackToFixedValues";
|
|
30
32
|
export const FIXED_FALLBACKS_RULE = "no-fixed-fallbacks";
|
|
31
33
|
export const FIXED_FALLBACKS_GRANTED = "granted";
|
|
34
|
+
export const NO_CONSOLE_RULE = "eslint/no-console";
|
|
35
|
+
export const NODE_CONSOLE_RULE = "no-node-console";
|
|
36
|
+
export const CONSOLE_REPLACED = "replaced";
|
|
32
37
|
const SCHEDULES = "pollsWithADeadline";
|
|
33
38
|
const TIMERS = "waitsOnTimers";
|
|
34
39
|
export const SCHEDULES_RULE = "no-schedules";
|
|
@@ -56,6 +61,7 @@ export const PERMISSION_NAMES = [
|
|
|
56
61
|
TRUSTS_UNVERIFIED_TYPES,
|
|
57
62
|
HANDLES_EVERY_FAILURE,
|
|
58
63
|
FIXED_FALLBACKS,
|
|
64
|
+
REPLACES_THE_CONSOLE,
|
|
59
65
|
GENERATED,
|
|
60
66
|
] as const;
|
|
61
67
|
|
|
@@ -87,7 +93,7 @@ const severityOnceGranted = (permission: keyof typeof PERMISSION_BANS) =>
|
|
|
87
93
|
|
|
88
94
|
const ruleSeverities = (
|
|
89
95
|
severities: Readonly<
|
|
90
|
-
Record<string, readonly ["error", Readonly<Record<string, string>>] | "off">
|
|
96
|
+
Record<string, readonly ["error", Readonly<Record<string, string[] | string>>] | "off">
|
|
91
97
|
>,
|
|
92
98
|
) => Object.entries(severities);
|
|
93
99
|
|
|
@@ -127,6 +133,12 @@ const rulesGrantedBy = (permission: (typeof PERMISSION_NAMES)[number]) => {
|
|
|
127
133
|
[`sshkeda/${FIXED_FALLBACKS_RULE}`]: ["error", { fixedFallbacks: FIXED_FALLBACKS_GRANTED }],
|
|
128
134
|
} as const);
|
|
129
135
|
}
|
|
136
|
+
if (permission === REPLACES_THE_CONSOLE) {
|
|
137
|
+
return ruleSeverities({
|
|
138
|
+
[`sshkeda/${NODE_CONSOLE_RULE}`]: ["error", { console: CONSOLE_REPLACED }],
|
|
139
|
+
[NO_CONSOLE_RULE]: ["error", { allow: [...CONSOLE_METHODS] }],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
130
142
|
if (permission === TRUSTS_UNVERIFIED_TYPES) {
|
|
131
143
|
return ruleSeverities({
|
|
132
144
|
[permissionBanRuleId(permission)]: "off",
|
|
@@ -152,6 +164,7 @@ export const CODE_GRANTS = [
|
|
|
152
164
|
SCHEDULES,
|
|
153
165
|
SHORT_NAMES,
|
|
154
166
|
TRUSTS_UNVERIFIED_TYPES,
|
|
167
|
+
REPLACES_THE_CONSOLE,
|
|
155
168
|
] as const;
|
|
156
169
|
|
|
157
170
|
const isCodeGrant = Schema.is(Schema.Literals(CODE_GRANTS));
|