@sshkeda/check 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/docs/how-to-make-check-pass.md +71 -0
- package/package.json +51 -4
- package/src/index.ts +8 -0
- package/src/internal/ban-abbreviated-words-in-declared-names.ts +165 -0
- package/src/internal/ban-checked-members-outside-calls.ts +151 -0
- package/src/internal/ban-comments.ts +23 -0
- package/src/internal/ban-comparing-or-switching-on-tag.ts +114 -0
- package/src/internal/ban-deadlines-inside-tests.ts +41 -0
- package/src/internal/ban-declared-values.ts +70 -0
- package/src/internal/ban-dropped-failures-detached-fibers-sleeps-and-untraced-functions.ts +67 -0
- package/src/internal/ban-failures-collected-as-results.ts +57 -0
- package/src/internal/ban-fallbacks-of-named-recoveries.ts +118 -0
- package/src/internal/ban-function-overloads.ts +107 -0
- package/src/internal/ban-global-error-in-failure-types.ts +146 -0
- package/src/internal/ban-instanceof-in-effect-recovery-callbacks.ts +63 -0
- package/src/internal/ban-let-and-effect-mutable-ref.ts +79 -0
- package/src/internal/ban-logic-in-index-files.ts +178 -0
- package/src/internal/ban-namespaces-used-without-a-member.ts +176 -0
- package/src/internal/ban-node-builtins-that-effect-replaces.ts +42 -0
- package/src/internal/ban-parsing-outside-data-without-a-schema.ts +299 -0
- package/src/internal/ban-re-exports.ts +172 -0
- package/src/internal/ban-reading-the-clock-and-sleeping-outside-effect.ts +239 -0
- package/src/internal/ban-retired-words-in-names.ts +49 -0
- package/src/internal/ban-scanning-directories-with-bun-glob.ts +79 -0
- package/src/internal/ban-shebang-lines.ts +26 -0
- package/src/internal/ban-timer-globals-and-the-global-object.ts +34 -0
- package/src/internal/ban-timer-schedules-without-deadlines.ts +240 -0
- package/src/internal/ban-type-and-interface-declarations.ts +48 -0
- package/src/internal/ban-type-predicates.ts +82 -0
- package/src/internal/ban-unknown-in-function-signatures.ts +143 -0
- package/src/internal/ban-vague-error-class-names.ts +28 -0
- package/src/internal/ban-vague-file-names.ts +51 -0
- package/src/internal/ban-what-a-file-is-not-granted.ts +512 -0
- package/src/internal/ban-while-do-while-and-for-loops.ts +27 -0
- package/src/internal/confine-service-declarations-to-granted-files.ts +116 -0
- package/src/internal/confine-the-stdio-service-to-terminal-output.ts +190 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +361 -0
- package/src/internal/describe-and-decode-rule-options.ts +22 -0
- package/src/internal/keep-assertions-in-the-test-home-and-ban-test-runners.ts +128 -0
- package/src/internal/keep-effect-testing-helpers-in-test-files.ts +157 -0
- package/src/internal/match-banned-modules-and-globals.ts +71 -0
- package/src/internal/match-effect-service-factories.ts +20 -0
- package/src/internal/require-a-named-const-for-compound-conditions.ts +44 -0
- package/src/internal/require-capital-snake-case-and-as-const-on-literal-constants.ts +141 -0
- package/src/internal/require-service-keys-to-end-with-their-class-name.ts +64 -0
- package/src/internal/resolve-what-an-expression-imports.ts +295 -0
- package/src/internal/sort-type-constituents.ts +82 -0
- package/src/internal/split-names-into-words.ts +12 -0
- package/src/internal/visit-every-declared-name.ts +264 -0
- package/src/lib/tasks/index.ts +1 -0
- package/src/lib/tasks/internal/declare-the-check-task.ts +29 -0
- package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +41 -0
- package/src/lib/tasks/internal/declare-the-typed-checks-task.ts +25 -0
- package/src/lib/tasks/internal/run-checks-over-one-typescript-project.ts +68 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +15 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/check-effect-diagnostics-with-effect-tsgo.ts +211 -0
- package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/read-effect-tsgo-json-report.ts +39 -0
- package/src/lib/tasks/lib/check-import-boundaries/index.ts +1 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-import-boundary-violations.ts +415 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-subpath-import-targets.ts +117 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/find-what-hides-an-import.ts +65 -0
- package/src/lib/tasks/lib/check-import-boundaries/internal/import-boundary-errors.ts +176 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/index.ts +1 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/declared-use-errors.ts +32 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-declared-use-violations.ts +38 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-sync-calls.ts +23 -0
- package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-type-only-schemas.ts +375 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/index.ts +1 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +648 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/layer-provide-errors.ts +27 -0
- package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/resolve-imported-package-apis.ts +262 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/index.ts +1 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/find-ungranted-stdio-uses.ts +219 -0
- package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/stdio-use-errors.ts +31 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/index.ts +1 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/find-inferrable-annotations.ts +357 -0
- package/src/lib/tasks/lib/detect-inferrable-annotations/internal/inferrable-annotation-errors.ts +33 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/index.ts +1 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/find-repeated-service-yields.ts +162 -0
- package/src/lib/tasks/lib/detect-repeated-service-yields/internal/repeated-service-yield-errors.ts +25 -0
- package/src/lib/tasks/lib/enforce-file-layout/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/check-the-layout-of-each-package.ts +9 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +417 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +83 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +848 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-module-layout-violations.ts +82 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/resolve-task-imports.ts +178 -0
- package/src/lib/tasks/lib/enforce-file-types/index.ts +5 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/file-type-errors.ts +21 -0
- package/src/lib/tasks/lib/enforce-file-types/internal/find-file-type-violations.ts +78 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/index.ts +1 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/check-formatting-with-bun-fmt.ts +157 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +66 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-config-violations.ts +16 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-input-violations.ts +74 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-symbolic-links.ts +14 -0
- package/src/lib/tasks/lib/format-with-bun-fmt/internal/unformatted-file-errors.ts +12 -0
- package/src/lib/tasks/lib/judge-index-re-exports/index.ts +1 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +353 -0
- package/src/lib/tasks/lib/judge-index-re-exports/internal/index-re-export-errors.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/index.ts +15 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +123 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/check-unsafe-enum-assignments.ts +152 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/find-tsgolint-executable.ts +21 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +82 -0
- package/src/lib/tasks/lib/lint-with-oxlint/internal/read-tsgolint-rule-messages.ts +68 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/index.ts +1 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +135 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +507 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-misplaced-files.ts +116 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-subdivided-directories.ts +29 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +451 -0
- package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/list-binding-names.ts +49 -0
- package/src/lib/tasks/lib/refuse-disable-directives/index.ts +15 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/disable-directive-errors.ts +45 -0
- package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +66 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/index.ts +1 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/error-mapping-errors.ts +21 -0
- package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +135 -0
- package/src/lib/tasks/lib/require-separate-service-layers/index.ts +1 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +170 -0
- package/src/lib/tasks/lib/require-separate-service-layers/internal/service-layer-errors.ts +21 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +14 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +117 -0
- package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/linted-file-errors.ts +51 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +11 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/check-types-with-bun-check.ts +54 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/read-bun-check-output.ts +73 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/type-check-errors.ts +10 -0
- package/src/lib/tasks/shared/check-each-nested-package.ts +79 -0
- package/src/lib/tasks/shared/classify-files-by-role.ts +115 -0
- package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +2 -0
- package/src/lib/tasks/shared/create-the-module-resolver.ts +38 -0
- package/src/lib/tasks/shared/declare-lint-config-task.ts +13 -0
- package/src/lib/tasks/shared/declare-typescript-config-task.ts +18 -0
- package/src/lib/tasks/shared/encode-compact-json.ts +5 -0
- package/src/lib/tasks/shared/find-installed-package-binaries.ts +62 -0
- package/src/lib/tasks/shared/find-module-requests.ts +482 -0
- package/src/lib/tasks/shared/find-named-package-manifests.ts +94 -0
- package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +60 -0
- package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +79 -0
- package/src/lib/tasks/shared/list-linted-files.ts +28 -0
- package/src/lib/tasks/shared/list-owned-files.ts +13 -0
- package/src/lib/tasks/shared/list-package-files.ts +66 -0
- package/src/lib/tasks/shared/load-the-lint-config.ts +176 -0
- package/src/lib/tasks/shared/match-granted-patch-files.ts +88 -0
- package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +310 -0
- package/src/lib/tasks/shared/print-failures-and-keep-detail-in-the-trace.ts +99 -0
- package/src/lib/tasks/shared/project-config-and-tool-errors.ts +485 -0
- package/src/lib/tasks/shared/query-the-typescript-project.ts +635 -0
- package/src/lib/tasks/shared/read-named-package-manifests.ts +53 -0
- package/src/lib/tasks/shared/read-package-manifest-object.ts +28 -0
- package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -0
- package/src/lib/tasks/shared/resolve-module-requests.ts +121 -0
- package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +98 -0
- package/src/lib/tasks/shared/tool-file-locations.ts +38 -0
- package/src/shared/bans-each-permission-lifts.ts +454 -0
- package/src/shared/config-file-names.ts +3 -0
- package/src/shared/find-retired-words.ts +17 -0
- package/src/shared/format-reported-failures.ts +31 -0
- package/src/shared/lint-rule-messages-and-patterns.ts +104 -0
- package/src/shared/match-file-names-and-path-prefixes.ts +18 -0
- package/src/shared/permission-grant-errors.ts +42 -0
- package/src/shared/record-and-grant-permissions-by-path.ts +419 -0
- package/src/shared/test-home-directories.ts +4 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { Array as Arrays, Result, Schema, SchemaIssue } from "effect";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BANS_NO_GRANT_LIFTS,
|
|
5
|
+
bansNoGrantLifts,
|
|
6
|
+
GRANTABLE_BAN_NAMES,
|
|
7
|
+
HANDLES_EVERY_FAILURE,
|
|
8
|
+
PERMISSION_BANS,
|
|
9
|
+
RULES_EACH_PERMISSION_LIFTS,
|
|
10
|
+
SCRIPT,
|
|
11
|
+
SHORT_NAMES,
|
|
12
|
+
TRUSTS_UNVERIFIED_TYPES,
|
|
13
|
+
WRITES_TO_THE_TERMINAL,
|
|
14
|
+
} from "./bans-each-permission-lifts.ts";
|
|
15
|
+
import { isWithin } from "./match-file-names-and-path-prefixes.ts";
|
|
16
|
+
import { MalformedGrant, PermissionGrantsRefused } from "./permission-grant-errors.ts";
|
|
17
|
+
import { SCRIPTS_DIRECTORY, TEST_HOME } from "./test-home-directories.ts";
|
|
18
|
+
|
|
19
|
+
export const DIRECTORY = "isAnUnlistedDirectory";
|
|
20
|
+
export const GENERATED = "isGeneratedByATool";
|
|
21
|
+
export const PATCH = "patchesADependency";
|
|
22
|
+
export const FILE_TYPE = "hasAnUnlistedFileType";
|
|
23
|
+
export const TEST_TIMEOUTS_RULE = "no-test-timeouts";
|
|
24
|
+
export const TYPE_PREDICATES_RULE = "no-type-predicates";
|
|
25
|
+
export const STDIO_SERVICE_RULE = "no-stdio-service";
|
|
26
|
+
export const FAILURES_AS_RESULTS_RULE = "no-failures-as-results";
|
|
27
|
+
const UNNAMED_FALLBACKS_RULE = "no-unnamed-fallbacks";
|
|
28
|
+
const SCHEDULES = "pollsWithADeadline";
|
|
29
|
+
const GLOB_CHARACTERS = /[*?[\]{}]/u;
|
|
30
|
+
const ROUTE_PARAMETER_SEGMENT = /^\[\[?(?:\.\.\.)?[A-Za-z0-9_-]+\]\]?$/u;
|
|
31
|
+
|
|
32
|
+
const isGlobPath = (path: string) =>
|
|
33
|
+
path
|
|
34
|
+
.split("/")
|
|
35
|
+
.some((segment) => !ROUTE_PARAMETER_SEGMENT.test(segment) && GLOB_CHARACTERS.test(segment));
|
|
36
|
+
const MARKDOWN_EXTENSIONS = [".md", ".markdown"] as const;
|
|
37
|
+
|
|
38
|
+
const UNREASONED_PERMISSION_NAMES = [
|
|
39
|
+
...GRANTABLE_BAN_NAMES,
|
|
40
|
+
SHORT_NAMES,
|
|
41
|
+
SCHEDULES,
|
|
42
|
+
DIRECTORY,
|
|
43
|
+
PATCH,
|
|
44
|
+
FILE_TYPE,
|
|
45
|
+
] satisfies readonly (
|
|
46
|
+
| (typeof GRANTABLE_BAN_NAMES)[number]
|
|
47
|
+
| typeof DIRECTORY
|
|
48
|
+
| typeof FILE_TYPE
|
|
49
|
+
| typeof PATCH
|
|
50
|
+
| typeof SCHEDULES
|
|
51
|
+
| typeof SHORT_NAMES
|
|
52
|
+
)[];
|
|
53
|
+
|
|
54
|
+
const REASONED_PERMISSION_NAMES: ReadonlySet<string> = new Set([
|
|
55
|
+
TRUSTS_UNVERIFIED_TYPES,
|
|
56
|
+
HANDLES_EVERY_FAILURE,
|
|
57
|
+
GENERATED,
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
export const PERMISSION_NAMES = [
|
|
61
|
+
...UNREASONED_PERMISSION_NAMES,
|
|
62
|
+
TRUSTS_UNVERIFIED_TYPES,
|
|
63
|
+
HANDLES_EVERY_FAILURE,
|
|
64
|
+
GENERATED,
|
|
65
|
+
] as const;
|
|
66
|
+
|
|
67
|
+
const RETIRED_PERMISSIONS = new Map([
|
|
68
|
+
["errors", "error classes may be declared in any file"],
|
|
69
|
+
[SCRIPT, `files directly in ${SCRIPTS_DIRECTORY}/ run tasks`],
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
export const isMarkdown = (path: string) =>
|
|
73
|
+
MARKDOWN_EXTENSIONS.some((extension) => path.toLowerCase().endsWith(extension));
|
|
74
|
+
|
|
75
|
+
export const permissionBanRuleId = (permission: keyof typeof PERMISSION_BANS) =>
|
|
76
|
+
`sshkeda/${PERMISSION_BANS[permission].ruleName}`;
|
|
77
|
+
|
|
78
|
+
const GRANTED = ["error", { bans: BANS_NO_GRANT_LIFTS }] as const;
|
|
79
|
+
|
|
80
|
+
const permissionBanSeverityOnceGranted = (permission: keyof typeof PERMISSION_BANS) =>
|
|
81
|
+
bansNoGrantLifts(permission).length > 0 ? GRANTED : "off";
|
|
82
|
+
|
|
83
|
+
const ruleSeverities = (
|
|
84
|
+
severities: Readonly<
|
|
85
|
+
Record<string, readonly ["error", Readonly<Record<string, string>>] | "off">
|
|
86
|
+
>,
|
|
87
|
+
) => Object.entries(severities);
|
|
88
|
+
|
|
89
|
+
const rulesGrantedBy = (permission: (typeof PERMISSION_NAMES)[number]) => {
|
|
90
|
+
if (permission === SHORT_NAMES) {
|
|
91
|
+
return ruleSeverities({
|
|
92
|
+
[`sshkeda/${RULES_EACH_PERMISSION_LIFTS[permission]}`]: "off",
|
|
93
|
+
} as const);
|
|
94
|
+
}
|
|
95
|
+
if (permission === SCHEDULES) {
|
|
96
|
+
return ruleSeverities({
|
|
97
|
+
"sshkeda/no-schedules": ["error", { timedSchedules: "allowedWithDeadline" }],
|
|
98
|
+
[`sshkeda/${TEST_TIMEOUTS_RULE}`]: "off",
|
|
99
|
+
} as const);
|
|
100
|
+
}
|
|
101
|
+
if (permission === WRITES_TO_THE_TERMINAL) {
|
|
102
|
+
return ruleSeverities({
|
|
103
|
+
[permissionBanRuleId(permission)]: GRANTED,
|
|
104
|
+
[`sshkeda/${STDIO_SERVICE_RULE}`]: "off",
|
|
105
|
+
} as const);
|
|
106
|
+
}
|
|
107
|
+
if (permission === HANDLES_EVERY_FAILURE) {
|
|
108
|
+
return ruleSeverities({
|
|
109
|
+
[permissionBanRuleId(permission)]: "off",
|
|
110
|
+
[`sshkeda/${FAILURES_AS_RESULTS_RULE}`]: "off",
|
|
111
|
+
[`sshkeda/${UNNAMED_FALLBACKS_RULE}`]: "off",
|
|
112
|
+
} as const);
|
|
113
|
+
}
|
|
114
|
+
if (permission === TRUSTS_UNVERIFIED_TYPES) {
|
|
115
|
+
return ruleSeverities({
|
|
116
|
+
[permissionBanRuleId(permission)]: "off",
|
|
117
|
+
[`sshkeda/${TYPE_PREDICATES_RULE}`]: ["error", { schemaDeclarations: "allowed" }],
|
|
118
|
+
} as const);
|
|
119
|
+
}
|
|
120
|
+
const grantsNoRule =
|
|
121
|
+
permission === GENERATED ||
|
|
122
|
+
permission === DIRECTORY ||
|
|
123
|
+
permission === PATCH ||
|
|
124
|
+
permission === FILE_TYPE;
|
|
125
|
+
if (grantsNoRule) {
|
|
126
|
+
return ruleSeverities({});
|
|
127
|
+
}
|
|
128
|
+
return ruleSeverities({
|
|
129
|
+
[permissionBanRuleId(permission)]: permissionBanSeverityOnceGranted(permission),
|
|
130
|
+
} as const);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const ABORT_ON_FAILURE = true;
|
|
134
|
+
|
|
135
|
+
const pathRule = (isAllowed: (path: string) => boolean, violation: (path: string) => string) =>
|
|
136
|
+
Schema.makeFilter(
|
|
137
|
+
(path: string) => isAllowed(path) || violation(path),
|
|
138
|
+
undefined,
|
|
139
|
+
ABORT_ON_FAILURE,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
export const normalizedPath = (path: string) =>
|
|
143
|
+
path
|
|
144
|
+
.split("/")
|
|
145
|
+
.filter((segment) => segment !== "" && segment !== ".")
|
|
146
|
+
.join("/");
|
|
147
|
+
|
|
148
|
+
const GRANT_PATH = Schema.String.check(
|
|
149
|
+
pathRule(
|
|
150
|
+
(path) => path !== "",
|
|
151
|
+
() => "is not a path",
|
|
152
|
+
),
|
|
153
|
+
pathRule(
|
|
154
|
+
(path) => !path.includes("\\"),
|
|
155
|
+
() => "uses backslashes",
|
|
156
|
+
),
|
|
157
|
+
pathRule(
|
|
158
|
+
(path) => !isGlobPath(path),
|
|
159
|
+
() => "is a glob",
|
|
160
|
+
),
|
|
161
|
+
pathRule(
|
|
162
|
+
(path) => !path.startsWith("!"),
|
|
163
|
+
() => "is a negated pattern",
|
|
164
|
+
),
|
|
165
|
+
pathRule(
|
|
166
|
+
(path) => !path.startsWith("/"),
|
|
167
|
+
() => "is an absolute path",
|
|
168
|
+
),
|
|
169
|
+
pathRule(
|
|
170
|
+
(path) => !path.split("/").includes(".."),
|
|
171
|
+
() => "has a .. segment",
|
|
172
|
+
),
|
|
173
|
+
pathRule(
|
|
174
|
+
(path) => normalizedPath(path) === path,
|
|
175
|
+
() => "is not a normalized path",
|
|
176
|
+
),
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
const PERMISSION = Schema.String.check(
|
|
180
|
+
Schema.makeFilter((name: string) => {
|
|
181
|
+
const retirement = RETIRED_PERMISSIONS.get(name);
|
|
182
|
+
return retirement === undefined || `${name} is retired: ${retirement}`;
|
|
183
|
+
}),
|
|
184
|
+
Schema.makeFilter(
|
|
185
|
+
(name: string) => !REASONED_PERMISSION_NAMES.has(name) || `grants ${name} without a reason`,
|
|
186
|
+
),
|
|
187
|
+
).pipe(Schema.decodeTo(Schema.Literals(UNREASONED_PERMISSION_NAMES)));
|
|
188
|
+
|
|
189
|
+
const REASON = Schema.String.check(
|
|
190
|
+
Schema.makeFilter((reason: string) => reason.trim() !== "" || "is blank"),
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
const REASONED_PERMISSIONS = Schema.Struct({
|
|
194
|
+
[TRUSTS_UNVERIFIED_TYPES]: Schema.optionalKey(REASON),
|
|
195
|
+
[HANDLES_EVERY_FAILURE]: Schema.optionalKey(REASON),
|
|
196
|
+
[GENERATED]: Schema.optionalKey(REASON),
|
|
197
|
+
})
|
|
198
|
+
.annotate({ messageUnexpectedKey: "is not a permission that takes a reason" })
|
|
199
|
+
.check(Schema.makeFilter((reasons) => Object.keys(reasons).length > 0 || "grants nothing"));
|
|
200
|
+
|
|
201
|
+
const permissionNamesOf = (allow: readonly (typeof ALLOWED.Type)[number][]) =>
|
|
202
|
+
allow.flatMap((entry) =>
|
|
203
|
+
typeof entry === "string"
|
|
204
|
+
? [entry]
|
|
205
|
+
: [
|
|
206
|
+
...(entry.trustsUnverifiedTypes === undefined
|
|
207
|
+
? []
|
|
208
|
+
: ([TRUSTS_UNVERIFIED_TYPES] as const)),
|
|
209
|
+
...(entry.handlesEveryFailure === undefined ? [] : ([HANDLES_EVERY_FAILURE] as const)),
|
|
210
|
+
...(entry.isGeneratedByATool === undefined ? [] : ([GENERATED] as const)),
|
|
211
|
+
],
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const nonEmptyList = <Entry extends Schema.Top>(entry: Entry) =>
|
|
215
|
+
Schema.Array(entry).check(Schema.isNonEmpty({ message: "is empty" }));
|
|
216
|
+
|
|
217
|
+
const ALLOWED = nonEmptyList(Schema.Union([PERMISSION, REASONED_PERMISSIONS]));
|
|
218
|
+
|
|
219
|
+
const GRANT_FIELDS = {
|
|
220
|
+
allow: Schema.optionalKey(ALLOWED),
|
|
221
|
+
services: Schema.optionalKey(nonEmptyList(Schema.NonEmptyString)),
|
|
222
|
+
cannotImport: Schema.optionalKey(nonEmptyList(GRANT_PATH)),
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const GRANT = Schema.Struct(GRANT_FIELDS)
|
|
226
|
+
.annotate({
|
|
227
|
+
messageUnexpectedKey: `is not one of the grant keys ${Object.keys(GRANT_FIELDS).join(", ")}`,
|
|
228
|
+
})
|
|
229
|
+
.check(Schema.makeFilter((grant) => Object.keys(grant).length > 0 || "grants nothing"));
|
|
230
|
+
|
|
231
|
+
const isGrant = Schema.is(GRANT);
|
|
232
|
+
const UNDECODED_GRANT = Schema.Record(Schema.String, Schema.Unknown);
|
|
233
|
+
const isUndecodedGrant = Schema.is(UNDECODED_GRANT);
|
|
234
|
+
const isList = Schema.is(Schema.Array(Schema.Unknown));
|
|
235
|
+
const isString = Schema.is(Schema.String);
|
|
236
|
+
|
|
237
|
+
const repeatedItems = (path: string, grant: typeof UNDECODED_GRANT.Type) =>
|
|
238
|
+
Object.keys(GRANT_FIELDS).flatMap((key) => {
|
|
239
|
+
const list = grant[key];
|
|
240
|
+
const names = isList(list)
|
|
241
|
+
? list.flatMap((item) => {
|
|
242
|
+
if (isString(item)) {
|
|
243
|
+
return [item];
|
|
244
|
+
}
|
|
245
|
+
return isUndecodedGrant(item) ? Object.keys(item) : [];
|
|
246
|
+
})
|
|
247
|
+
: [];
|
|
248
|
+
const repeated = names.filter((name, index) => names.indexOf(name) !== index);
|
|
249
|
+
return Arrays.dedupe(repeated).map((name) => ({
|
|
250
|
+
path: [path, key],
|
|
251
|
+
issue: `lists "${name}" twice`,
|
|
252
|
+
}));
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const grantMismatches = (path: string, grant: typeof GRANT.Type): readonly Schema.FilterIssue[] => {
|
|
256
|
+
const allowed = permissionNamesOf(grant.allow ?? []);
|
|
257
|
+
const grantsOnlyFileType =
|
|
258
|
+
allowed.every((permission) => permission === FILE_TYPE) && Object.keys(grant).length === 1;
|
|
259
|
+
const grantsGenerated = allowed.includes(GENERATED);
|
|
260
|
+
const grantsGeneratedWithOtherPermissions =
|
|
261
|
+
grantsGenerated && (allowed.length !== 1 || Object.keys(grant).length !== 1);
|
|
262
|
+
const grantsDirectory = allowed.includes(DIRECTORY);
|
|
263
|
+
const grantsMarkdownMoreThanFileType =
|
|
264
|
+
isMarkdown(path) && !grantsOnlyFileType && !grantsDirectory && !grantsGenerated;
|
|
265
|
+
return [
|
|
266
|
+
...(grantsGeneratedWithOtherPermissions
|
|
267
|
+
? [
|
|
268
|
+
{
|
|
269
|
+
path: [path],
|
|
270
|
+
issue:
|
|
271
|
+
"generated outputs cannot also have permissions, services or import restrictions",
|
|
272
|
+
},
|
|
273
|
+
]
|
|
274
|
+
: []),
|
|
275
|
+
...(grant.cannotImport ?? [])
|
|
276
|
+
.filter((target) => isWithin(path, target))
|
|
277
|
+
.map((target) => ({ path: [path, "cannotImport"], issue: `"${target}" contains "${path}"` })),
|
|
278
|
+
...(grantsMarkdownMoreThanFileType
|
|
279
|
+
? [
|
|
280
|
+
{
|
|
281
|
+
path: [path],
|
|
282
|
+
issue: `is Markdown, so it may only be granted ${FILE_TYPE}`,
|
|
283
|
+
},
|
|
284
|
+
]
|
|
285
|
+
: []),
|
|
286
|
+
];
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const GRANT_PATHS = Schema.Record(Schema.String.pipe(Schema.decodeTo(GRANT_PATH)), Schema.Unknown);
|
|
290
|
+
|
|
291
|
+
const GRANTS = Schema.Record(Schema.String, GRANT);
|
|
292
|
+
|
|
293
|
+
const GRANTS_AGAINST_THEIR_PATHS = Schema.Record(Schema.String, Schema.Unknown).check(
|
|
294
|
+
Schema.makeFilter((grants) => {
|
|
295
|
+
const entries = Object.entries(grants);
|
|
296
|
+
return [
|
|
297
|
+
...entries.flatMap(([path, grant]) =>
|
|
298
|
+
isUndecodedGrant(grant) ? repeatedItems(path, grant) : [],
|
|
299
|
+
),
|
|
300
|
+
...entries.flatMap(([path, grant]) => (isGrant(grant) ? grantMismatches(path, grant) : [])),
|
|
301
|
+
];
|
|
302
|
+
}),
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const STANDARD_SCHEMA_ISSUES = SchemaIssue.makeFormatterStandardSchemaV1();
|
|
306
|
+
|
|
307
|
+
const grantOverride = (
|
|
308
|
+
grant: Extract<ReturnType<typeof recordedGrantsOf>, { grants: unknown }>["grants"][number],
|
|
309
|
+
) => ({
|
|
310
|
+
files: [`./${grant.path.replaceAll("[", String.raw`\[`).replaceAll("]", String.raw`\]`)}`],
|
|
311
|
+
rules: {
|
|
312
|
+
...Object.fromEntries(grant.allow.flatMap((permission) => rulesGrantedBy(permission))),
|
|
313
|
+
...Object.fromEntries(
|
|
314
|
+
grant.services.length > 0
|
|
315
|
+
? Object.entries({
|
|
316
|
+
"sshkeda/no-service-declarations": ["error", { services: grant.services }],
|
|
317
|
+
"sshkeda/no-unused-service-grants": ["error", { services: grant.services }],
|
|
318
|
+
} as const)
|
|
319
|
+
: [],
|
|
320
|
+
),
|
|
321
|
+
},
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const TEST_HOME_OVERRIDE = {
|
|
325
|
+
files: [`./${TEST_HOME}**`],
|
|
326
|
+
rules: {
|
|
327
|
+
"sshkeda/no-testing-helpers": "off",
|
|
328
|
+
"sshkeda/no-assertions": "off",
|
|
329
|
+
"sshkeda/no-schedules": "off",
|
|
330
|
+
[`sshkeda/${TEST_TIMEOUTS_RULE}`]: "error",
|
|
331
|
+
},
|
|
332
|
+
} as const;
|
|
333
|
+
|
|
334
|
+
const SCRIPTS_OVERRIDE = {
|
|
335
|
+
files: [`./${SCRIPTS_DIRECTORY}/*`],
|
|
336
|
+
rules: { [permissionBanRuleId(SCRIPT)]: "off" },
|
|
337
|
+
} as const;
|
|
338
|
+
|
|
339
|
+
const DECODE_OPTIONS = {
|
|
340
|
+
errors: "all",
|
|
341
|
+
onExcessProperty: "error",
|
|
342
|
+
reportInput: true,
|
|
343
|
+
} as const;
|
|
344
|
+
|
|
345
|
+
const grantLocation = (
|
|
346
|
+
issuePath: ReturnType<typeof STANDARD_SCHEMA_ISSUES>["issues"][number]["path"],
|
|
347
|
+
) =>
|
|
348
|
+
(issuePath ?? [])
|
|
349
|
+
.map((segment) => (typeof segment === "object" ? segment.key : segment))
|
|
350
|
+
.map((key, index) => {
|
|
351
|
+
if (index === 0) {
|
|
352
|
+
return `"${String(key)}"`;
|
|
353
|
+
}
|
|
354
|
+
return typeof key === "number" ? `[${key}]` : `.${String(key)}`;
|
|
355
|
+
})
|
|
356
|
+
.join("");
|
|
357
|
+
|
|
358
|
+
const malformedGrants = (decoded: Result.Result<unknown, Schema.SchemaError>) =>
|
|
359
|
+
Result.isFailure(decoded)
|
|
360
|
+
? STANDARD_SCHEMA_ISSUES(decoded.failure.issue).issues.map(
|
|
361
|
+
(issue) =>
|
|
362
|
+
new MalformedGrant({
|
|
363
|
+
issuePath: grantLocation(issue.path),
|
|
364
|
+
issueMessage: issue.message,
|
|
365
|
+
}),
|
|
366
|
+
)
|
|
367
|
+
: [];
|
|
368
|
+
|
|
369
|
+
export const recordedGrantsOf = (permissionsByPath: unknown) => {
|
|
370
|
+
const decoded = Schema.decodeUnknownResult(GRANTS)(permissionsByPath, DECODE_OPTIONS);
|
|
371
|
+
const violations = [
|
|
372
|
+
Schema.decodeUnknownResult(GRANT_PATHS)(permissionsByPath, DECODE_OPTIONS),
|
|
373
|
+
decoded,
|
|
374
|
+
Schema.decodeUnknownResult(GRANTS_AGAINST_THEIR_PATHS)(permissionsByPath, DECODE_OPTIONS),
|
|
375
|
+
].flatMap((result) => malformedGrants(result));
|
|
376
|
+
const isRefused = Result.isFailure(decoded) || violations.length > 0;
|
|
377
|
+
if (isRefused) {
|
|
378
|
+
return { violations };
|
|
379
|
+
}
|
|
380
|
+
const grants = Object.entries(decoded.success).map(([path, grant]) => ({
|
|
381
|
+
path,
|
|
382
|
+
allow: permissionNamesOf(grant.allow ?? []),
|
|
383
|
+
services: grant.services ?? [],
|
|
384
|
+
cannotImport: grant.cannotImport ?? [],
|
|
385
|
+
}));
|
|
386
|
+
return { grants };
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
export const generatedPathsOf = (
|
|
390
|
+
grants: NonNullable<ReturnType<typeof recordedGrantsOf>["grants"]>,
|
|
391
|
+
) => new Set(grants.filter((grant) => grant.allow.includes(GENERATED)).map(({ path }) => path));
|
|
392
|
+
|
|
393
|
+
export const grantPermissions = <Config>(
|
|
394
|
+
config: Config,
|
|
395
|
+
permissionsByPath: Readonly<
|
|
396
|
+
Record<
|
|
397
|
+
string,
|
|
398
|
+
{
|
|
399
|
+
readonly [Key in keyof typeof GRANT.Type]-?: Required<Pick<typeof GRANT.Type, Key>> &
|
|
400
|
+
typeof GRANT.Type;
|
|
401
|
+
}[keyof typeof GRANT.Type]
|
|
402
|
+
>
|
|
403
|
+
>,
|
|
404
|
+
) => {
|
|
405
|
+
const recorded = recordedGrantsOf(permissionsByPath);
|
|
406
|
+
if ("violations" in recorded) {
|
|
407
|
+
const failure = new PermissionGrantsRefused({ violations: recorded.violations });
|
|
408
|
+
failure.stack = `${failure._tag}: ${failure.message}`;
|
|
409
|
+
throw failure;
|
|
410
|
+
}
|
|
411
|
+
const overrides = [
|
|
412
|
+
SCRIPTS_OVERRIDE,
|
|
413
|
+
TEST_HOME_OVERRIDE,
|
|
414
|
+
...recorded.grants
|
|
415
|
+
.filter((grant) => grant.allow.length > 0 || grant.services.length > 0)
|
|
416
|
+
.map((grant) => grantOverride(grant)),
|
|
417
|
+
];
|
|
418
|
+
return { ...config, overrides };
|
|
419
|
+
};
|