@mrciphersmith/keryx 0.4.29 → 0.4.31
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/dist/cli.js +2446 -1391
- package/dist/core.js +90 -27
- package/dist/keryx-general-worker-entry.js +82 -25
- package/package.json +1 -1
- package/src/gdskills/bundled/install-manifest.json +2 -0
- package/src/gdskills/bundled/rules/core/api-contracts.mdc +21 -136
- package/src/gdskills/bundled/rules/core/async-patterns.mdc +18 -171
- package/src/gdskills/bundled/rules/core/clean-architecture.mdc +21 -3
- package/src/gdskills/bundled/rules/core/cli-interface-design.mdc +63 -194
- package/src/gdskills/bundled/rules/core/code-style-patterns.mdc +36 -49
- package/src/gdskills/bundled/rules/core/concurrency-patterns.mdc +32 -0
- package/src/gdskills/bundled/rules/core/database-patterns.mdc +26 -148
- package/src/gdskills/bundled/rules/core/error-handling.mdc +7 -8
- package/src/gdskills/bundled/rules/core/frontend-assistant.mdc +14 -29
- package/src/gdskills/bundled/rules/core/layout-rendering.mdc +51 -0
- package/src/gdskills/bundled/rules/core/mobx-store-template.mdc +69 -511
- package/src/gdskills/bundled/rules/core/nestjs-dto.mdc +18 -36
- package/src/gdskills/bundled/rules/core/playwright-testing.mdc +35 -308
- package/src/gdskills/bundled/rules/core/security-baseline.mdc +18 -140
- package/src/gdskills/bundled/rules/core/solid-principles.mdc +31 -7
- package/src/gdskills/bundled/rules/core/storybook-guidelines.mdc +20 -22
- package/src/gdskills/bundled/rules/core/tdd-workflow.mdc +37 -74
- package/src/gdskills/bundled/rules/core/ui-anti-slop.mdc +48 -37
- package/src/gdskills/bundled/skills/quality/deprecation-path/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-architecture/SKILL.md +24 -60
- package/src/gdskills/bundled/skills/review/review-backend/SKILL.md +13 -65
- package/src/gdskills/bundled/skills/review/review-clean-code/SKILL.md +22 -35
- package/src/gdskills/bundled/skills/review/review-frontend/SKILL.md +16 -309
- package/src/gdskills/bundled/skills/review/review-frontend-conventions/SKILL.md +7 -21
- package/src/gdskills/bundled/skills/review/review-highload/SKILL.md +83 -227
- package/src/gdskills/bundled/skills/review/review-layout/SKILL.md +22 -28
- package/src/gdskills/bundled/skills/review/review-logic/SKILL.md +8 -8
- package/src/gdskills/bundled/skills/review/review-performance/SKILL.md +15 -37
- package/src/gdskills/bundled/skills/review/review-security-code/SKILL.md +7 -7
- package/src/gdskills/bundled/skills/review/review-style/SKILL.md +31 -66
- package/src/gdskills/bundled/skills/review/review-testing-practices/SKILL.md +21 -39
- package/src/gdskills/bundled/skills/review/review-ui-ux/SKILL.md +42 -82
- package/src/gdskills/bundled/stacks/angular/rules/coding-style.mdc +15 -57
- package/src/gdskills/bundled/stacks/angular/rules/patterns.mdc +16 -60
- package/src/gdskills/bundled/stacks/angular/rules/security.mdc +11 -49
- package/src/gdskills/bundled/stacks/angular/rules/testing.mdc +13 -52
- package/src/gdskills/bundled/stacks/c-cpp/rules/coding-style.mdc +33 -57
- package/src/gdskills/bundled/stacks/c-cpp/rules/patterns.mdc +35 -62
- package/src/gdskills/bundled/stacks/c-cpp/rules/security.mdc +35 -62
- package/src/gdskills/bundled/stacks/c-cpp/rules/testing.mdc +34 -54
- package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/patterns.mdc +27 -56
- package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/security.mdc +51 -119
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/coding-style.mdc +42 -74
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/patterns.mdc +45 -84
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/security.mdc +34 -59
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/testing.mdc +33 -62
- package/src/gdskills/bundled/stacks/django/rules/coding-style.mdc +15 -56
- package/src/gdskills/bundled/stacks/django/rules/patterns.mdc +16 -68
- package/src/gdskills/bundled/stacks/django/rules/security.mdc +13 -71
- package/src/gdskills/bundled/stacks/django/rules/testing.mdc +16 -62
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/coding-style.mdc +30 -53
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/patterns.mdc +20 -56
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/security.mdc +52 -122
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/testing.mdc +23 -42
- package/src/gdskills/bundled/stacks/fastapi/rules/coding-style.mdc +13 -47
- package/src/gdskills/bundled/stacks/fastapi/rules/patterns.mdc +18 -81
- package/src/gdskills/bundled/stacks/fastapi/rules/security.mdc +13 -75
- package/src/gdskills/bundled/stacks/fastapi/rules/testing.mdc +14 -58
- package/src/gdskills/bundled/stacks/flutter-dart/rules/coding-style.mdc +36 -74
- package/src/gdskills/bundled/stacks/flutter-dart/rules/patterns.mdc +30 -66
- package/src/gdskills/bundled/stacks/flutter-dart/rules/security.mdc +33 -67
- package/src/gdskills/bundled/stacks/flutter-dart/rules/testing.mdc +41 -73
- package/src/gdskills/bundled/stacks/go/rules/coding-style.mdc +36 -57
- package/src/gdskills/bundled/stacks/go/rules/patterns.mdc +27 -42
- package/src/gdskills/bundled/stacks/go/rules/security.mdc +26 -43
- package/src/gdskills/bundled/stacks/go/rules/testing.mdc +23 -41
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/coding-style.mdc +18 -37
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/patterns.mdc +26 -43
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/security.mdc +25 -37
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/testing.mdc +34 -50
- package/src/gdskills/bundled/stacks/kotlin-android/rules/coding-style.mdc +28 -65
- package/src/gdskills/bundled/stacks/kotlin-android/rules/patterns.mdc +34 -78
- package/src/gdskills/bundled/stacks/kotlin-android/rules/security.mdc +35 -68
- package/src/gdskills/bundled/stacks/kotlin-android/rules/testing.mdc +34 -68
- package/src/gdskills/bundled/stacks/mobx/rules/coding-style.mdc +18 -64
- package/src/gdskills/bundled/stacks/mobx/rules/patterns.mdc +13 -66
- package/src/gdskills/bundled/stacks/mobx/rules/security.mdc +8 -35
- package/src/gdskills/bundled/stacks/mobx/rules/testing.mdc +10 -39
- package/src/gdskills/bundled/stacks/nestjs/rules/coding-style.mdc +32 -45
- package/src/gdskills/bundled/stacks/nestjs/rules/patterns.mdc +40 -60
- package/src/gdskills/bundled/stacks/nestjs/rules/security.mdc +30 -50
- package/src/gdskills/bundled/stacks/nestjs/rules/testing.mdc +27 -47
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/coding-style.mdc +14 -48
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/patterns.mdc +15 -67
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/security.mdc +12 -50
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/testing.mdc +11 -43
- package/src/gdskills/bundled/stacks/php-laravel/rules/coding-style.mdc +17 -58
- package/src/gdskills/bundled/stacks/php-laravel/rules/patterns.mdc +15 -56
- package/src/gdskills/bundled/stacks/php-laravel/rules/security.mdc +13 -50
- package/src/gdskills/bundled/stacks/php-laravel/rules/testing.mdc +14 -55
- package/src/gdskills/bundled/stacks/python/rules/coding-style.mdc +13 -41
- package/src/gdskills/bundled/stacks/python/rules/patterns.mdc +20 -61
- package/src/gdskills/bundled/stacks/python/rules/security.mdc +15 -57
- package/src/gdskills/bundled/stacks/python/rules/testing.mdc +16 -50
- package/src/gdskills/bundled/stacks/react/rules/coding-style.mdc +14 -40
- package/src/gdskills/bundled/stacks/react/rules/patterns.mdc +15 -55
- package/src/gdskills/bundled/stacks/react/rules/security.mdc +13 -49
- package/src/gdskills/bundled/stacks/react/rules/testing.mdc +10 -37
- package/src/gdskills/bundled/stacks/ruby-rails/rules/coding-style.mdc +12 -45
- package/src/gdskills/bundled/stacks/ruby-rails/rules/patterns.mdc +18 -67
- package/src/gdskills/bundled/stacks/ruby-rails/rules/security.mdc +11 -60
- package/src/gdskills/bundled/stacks/ruby-rails/rules/testing.mdc +14 -65
- package/src/gdskills/bundled/stacks/rust/rules/coding-style.mdc +39 -65
- package/src/gdskills/bundled/stacks/rust/rules/patterns.mdc +33 -60
- package/src/gdskills/bundled/stacks/rust/rules/security.mdc +35 -57
- package/src/gdskills/bundled/stacks/rust/rules/testing.mdc +29 -52
- package/src/gdskills/bundled/stacks/sql-db/rules/coding-style.mdc +12 -48
- package/src/gdskills/bundled/stacks/sql-db/rules/patterns.mdc +22 -113
- package/src/gdskills/bundled/stacks/sql-db/rules/security.mdc +11 -56
- package/src/gdskills/bundled/stacks/sql-db/rules/testing.mdc +14 -59
- package/src/gdskills/bundled/stacks/swift-ios/rules/coding-style.mdc +29 -65
- package/src/gdskills/bundled/stacks/swift-ios/rules/patterns.mdc +41 -88
- package/src/gdskills/bundled/stacks/swift-ios/rules/security.mdc +28 -53
- package/src/gdskills/bundled/stacks/swift-ios/rules/testing.mdc +32 -65
- package/src/gdskills/bundled/stacks/vue/rules/coding-style.mdc +17 -52
- package/src/gdskills/bundled/stacks/vue/rules/patterns.mdc +17 -63
- package/src/gdskills/bundled/stacks/vue/rules/security.mdc +11 -41
- package/src/gdskills/bundled/stacks/vue/rules/testing.mdc +16 -48
|
@@ -7,138 +7,70 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# CI workflow security (GitHub Actions & GitLab CI)
|
|
9
9
|
|
|
10
|
-
Narrows `core-common-rules`'
|
|
11
|
-
specific to workflow-YAML configuration. Applies only to GitHub Actions
|
|
12
|
-
workflow files under `.github/workflows/`, GitHub composite `action.yml`/
|
|
13
|
-
`action.yaml` files under `.github/actions/`, `.gitlab-ci.yml`, and GitLab
|
|
14
|
-
CI included config under `.gitlab/`.
|
|
10
|
+
Narrows `core-common-rules`' security rules to workflow-YAML risks, following GitHub's Actions security hardening guide. Applies only to GitHub Actions workflows under `.github/workflows/`, composite `action.yml`/`action.yaml` under `.github/actions/`, `.gitlab-ci.yml` and GitLab CI included config under `.gitlab/`.
|
|
15
11
|
|
|
16
12
|
## `pull_request_target` and untrusted fork code
|
|
17
13
|
|
|
18
|
-
- `pull_request_target` runs in the
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
fork's own code — or route the job through a privilege-separated
|
|
35
|
-
`workflow_run` split (a build job with no secrets, a separate
|
|
36
|
-
privileged job that only consumes safe artifacts) instead.
|
|
37
|
-
- When a job only needs to build/test a contribution and does not need
|
|
38
|
-
base-repo secrets or write access, use `pull_request`, not
|
|
39
|
-
`pull_request_target` — when a `pull_request` run is triggered from a
|
|
40
|
-
fork, `GITHUB_TOKEN` (still the base repo's own token, not a separate
|
|
41
|
-
"fork token") gets read-only permissions, and no other repo secret is
|
|
42
|
-
passed to the runner at all.
|
|
14
|
+
- `pull_request_target` runs in the **base** repository's context: every repo
|
|
15
|
+
secret the workflow references is available and `GITHUB_TOKEN` gets read/write
|
|
16
|
+
permissions by default (unless a `permissions:` block narrows it), even when the
|
|
17
|
+
pull request comes from an untrusted fork.
|
|
18
|
+
- Never check out the pull request's head ref/SHA
|
|
19
|
+
(`actions/checkout` with `ref: ${{ github.event.pull_request.head.sha }}`) inside
|
|
20
|
+
a `pull_request_target` job and then run a build/test/lint command against it
|
|
21
|
+
(the "pwn request" pattern).
|
|
22
|
+
- If a `pull_request_target` job must read fork content (e.g. to comment on a PR),
|
|
23
|
+
check it out read-only and execute nothing from it (no `run:`, build or test
|
|
24
|
+
step against the fork's code), or split through a privilege-separated
|
|
25
|
+
`workflow_run` (a build job with no secrets, a separate privileged job that
|
|
26
|
+
consumes only safe artifacts).
|
|
27
|
+
- When a job only builds/tests a contribution and needs no base-repo secrets or
|
|
28
|
+
write access, use `pull_request`: from a fork, `GITHUB_TOKEN` is read-only and
|
|
29
|
+
no other repo secret is passed.
|
|
43
30
|
|
|
44
31
|
## Pin third-party actions to a commit SHA
|
|
45
32
|
|
|
46
33
|
- Reference a third-party action by its full-length commit SHA
|
|
47
|
-
(`uses: actions/checkout@<40-char-sha>`), not a mutable tag
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
changing.
|
|
52
|
-
- A first-party action from `actions/*` maintained by GitHub itself is
|
|
53
|
-
lower risk than an arbitrary third-party action, but the SHA-pinning
|
|
54
|
-
rule still applies to any action outside the repository's own
|
|
55
|
-
`.github/actions/` — do not special-case a well-known org as exempt.
|
|
56
|
-
- When a comment noting the tag the SHA corresponds to helps readability
|
|
57
|
-
(`uses: actions/checkout@<sha> # v4.2.0`), that comment is documentation
|
|
58
|
-
only — it is the SHA, not the comment, that pins the actual code that
|
|
59
|
-
runs.
|
|
34
|
+
(`uses: actions/checkout@<40-char-sha>`), not a mutable tag (`@v4`) or branch.
|
|
35
|
+
- The SHA-pinning rule applies to every action outside the repository's own
|
|
36
|
+
`.github/actions/`, including `actions/*`; no well-known org is exempt.
|
|
37
|
+
- A trailing tag comment (`# v4.2.0`) is documentation only; the SHA is the pin.
|
|
60
38
|
|
|
61
39
|
## Least-privilege `permissions:`
|
|
62
40
|
|
|
63
|
-
- Default to `permissions: {}` (or `permissions: read-all` only when read
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
on the repository/org setting) hands every job in the file whatever
|
|
70
|
-
scope the most-privileged job needed, even ones that only read.
|
|
71
|
-
- Grant `id-token: write` only to the job that actually performs OIDC
|
|
72
|
-
federation (cloud credential exchange); never at the workflow level
|
|
73
|
-
alongside jobs that do not need it.
|
|
41
|
+
- Default to `permissions: {}` (or `permissions: read-all` only when read access
|
|
42
|
+
is needed everywhere) at the workflow level, then grant specific scopes
|
|
43
|
+
(`contents: write`, `pull-requests: write`, `id-token: write` for OIDC) at the
|
|
44
|
+
**job** level. No workflow-level `permissions: write-all`, and no missing
|
|
45
|
+
`permissions:` key, which can default to broad access per repository/org setting.
|
|
46
|
+
- Grant `id-token: write` only to the job that performs OIDC federation.
|
|
74
47
|
|
|
75
48
|
## Script injection via untrusted expression expansion
|
|
76
49
|
|
|
77
|
-
- Never interpolate an untrusted `${{ github.event.* }}` value (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
variable by the time `script:` runs — there is no separate "before the
|
|
95
|
-
shell sees it" substitution step to intercept the way `${{ }}` needs
|
|
96
|
-
routing through `env:` first. Wrapping an already-a-variable in another
|
|
97
|
-
`variables:` entry changes nothing about how the shell expands it.
|
|
98
|
-
The real risks, and their real fixes:
|
|
99
|
-
- **Unquoted expansion**: `script: - rm -rf $CI_MERGE_REQUEST_TITLE` lets
|
|
100
|
-
the shell word-split and glob-expand an untrusted value; always quote
|
|
101
|
-
(`"$CI_MERGE_REQUEST_TITLE"`), and never build a command string by
|
|
102
|
-
concatenating an untrusted variable into one for `eval`/`sh -c`
|
|
103
|
-
(`eval "some-command $CI_MERGE_REQUEST_TITLE"` is exploitable however
|
|
104
|
-
the variable got its value).
|
|
105
|
-
- **Config-time interpolation via CI/CD inputs** (`$[[ inputs.x ]]`,
|
|
106
|
-
used in a component/included config's `rules:`/`script:`): this IS
|
|
107
|
-
substituted into the YAML/expression text before the job is even
|
|
108
|
-
created, the same class of risk `${{ }}` is in GitHub Actions — do not
|
|
109
|
-
feed an untrusted pipeline or trigger input value (a `spec:inputs`
|
|
110
|
-
value the pipeline/component was invoked with, not a predefined
|
|
111
|
-
variable like an MR title) into an `inputs:`-driven `$[[ ]]`
|
|
112
|
-
interpolation that reaches a `script:` line or a `rules:if:`
|
|
113
|
-
expression without validating/escaping it first.
|
|
50
|
+
- Never interpolate an untrusted `${{ github.event.* }}` value (PR title, issue
|
|
51
|
+
body, commit message, branch name) directly into a `run:` string.
|
|
52
|
+
- Pass it through an intermediate `env:` variable and reference it quoted
|
|
53
|
+
(`"$TITLE"`) inside the script.
|
|
54
|
+
- GitLab CI's mechanism differs and the `env:` indirection does not carry over:
|
|
55
|
+
every GitLab CI/CD variable (predefined like `CI_MERGE_REQUEST_TITLE`, or under
|
|
56
|
+
`variables:`) is already a shell environment variable when `script:` runs;
|
|
57
|
+
wrapping it in another `variables:` entry changes nothing. The real risks:
|
|
58
|
+
- **Unquoted expansion**: `script: - rm -rf $CI_MERGE_REQUEST_TITLE` word-splits
|
|
59
|
+
and globs an untrusted value; always quote (`"$CI_MERGE_REQUEST_TITLE"`), and
|
|
60
|
+
never concatenate an untrusted variable into a string for `eval`/`sh -c`.
|
|
61
|
+
- **Config-time interpolation via CI/CD inputs** (`$[[ inputs.x ]]` in a
|
|
62
|
+
component/included config's `rules:`/`script:`) is substituted into the YAML
|
|
63
|
+
before the job is created, the same class as `${{ }}`: do not feed an untrusted
|
|
64
|
+
pipeline or trigger input (a `spec:inputs` value, not a predefined variable
|
|
65
|
+
like an MR title) into a `$[[ ]]` interpolation that reaches a `script:` line
|
|
66
|
+
or a `rules:if:` expression without validating/escaping it.
|
|
114
67
|
|
|
115
68
|
## GitLab CI protected variables and branches
|
|
116
69
|
|
|
117
|
-
- A CI/CD variable
|
|
118
|
-
on
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
secret both "Protected" and "Masked" and restrict the job that consumes
|
|
125
|
-
it to protected branches/tags via `rules:` — a deploy job reachable from
|
|
126
|
-
an unprotected branch or an open merge request should not have access
|
|
127
|
-
to production credentials at all.
|
|
128
|
-
|
|
129
|
-
## Anti-patterns to flag
|
|
130
|
-
|
|
131
|
-
- A `pull_request_target` job that checks out `github.event.pull_request.head.sha`
|
|
132
|
-
(or `.ref`) and then runs a build/test/lint step against that checkout.
|
|
133
|
-
- Any `uses:` line pinned to a tag (`@v4`, `@main`) instead of a full
|
|
134
|
-
commit SHA for a third-party action.
|
|
135
|
-
- A workflow-level `permissions: write-all`, or no `permissions:` block at
|
|
136
|
-
all, on a workflow with jobs that do not all need write access.
|
|
137
|
-
- `${{ github.event.* }}` interpolated directly inside a `run:` string
|
|
138
|
-
instead of passed through `env:` first.
|
|
139
|
-
- A GitLab CI `script:` unquoted use of a variable holding untrusted input
|
|
140
|
-
(an MR title, a branch name), or that variable concatenated into a
|
|
141
|
-
string handed to `eval`/`sh -c`.
|
|
142
|
-
- An untrusted value reaching a GitLab CI/CD `inputs:`-driven `$[[ ]]`
|
|
143
|
-
interpolation in a `script:` line or a `rules:if:` expression with no
|
|
144
|
-
validation.
|
|
70
|
+
- A "Protected" CI/CD variable is exposed only to pipelines on a protected branch
|
|
71
|
+
or tag; on an unprotected ref (including a fork merge request) it is empty, not
|
|
72
|
+
an error. Do not rely on it in a job whose `rules:`/`only:`/`except:` allow a
|
|
73
|
+
non-protected ref.
|
|
74
|
+
- Mark any variable carrying a credential, deploy token or signing secret both
|
|
75
|
+
"Protected" and "Masked", and restrict the consuming job to protected
|
|
76
|
+
branches/tags via `rules:`.
|
|
@@ -8,93 +8,61 @@ metadata:
|
|
|
8
8
|
# C#/.NET coding style
|
|
9
9
|
|
|
10
10
|
Narrows `core-common-rules`' stack-agnostic style rules to modern C#
|
|
11
|
-
idiom
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Applies only to `*.cs` files — everything not C#-specific still comes
|
|
17
|
-
from the common rules this file `extends`.
|
|
11
|
+
idiom, following the .NET Framework Design Guidelines and C# coding
|
|
12
|
+
conventions: nullable reference types (on by default in new templates since
|
|
13
|
+
.NET 6/C# 10; an existing project needs `<Nullable>enable</Nullable>`
|
|
14
|
+
added), primary constructors and collection expressions (C# 12/.NET 8).
|
|
15
|
+
Applies only to `*.cs` files.
|
|
18
16
|
|
|
19
17
|
## Naming and structure
|
|
20
18
|
|
|
21
|
-
- `
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
-
|
|
26
|
-
|
|
27
|
-
- One public type per file, file named after the type
|
|
28
|
-
(`OrderService.cs` for `class OrderService`), unless the type is a
|
|
29
|
-
small private nested/support type used only inside its owner.
|
|
30
|
-
- File-scoped namespaces (`namespace Orders;`) instead of a nested
|
|
31
|
-
block, unless the project's existing files already use block-scoped
|
|
32
|
-
namespaces — match what is there.
|
|
19
|
+
- A private field is `_camelCase`, not `m_field` or bare `camelCase`.
|
|
20
|
+
- Interfaces are prefixed `I` (`IOrderRepository`), never suffixed.
|
|
21
|
+
- One public type per file, named after the type, unless the type is a small
|
|
22
|
+
private nested/support type.
|
|
23
|
+
- File-scoped namespaces (`namespace Orders;`) unless the project's existing
|
|
24
|
+
files use block-scoped ones.
|
|
33
25
|
|
|
34
26
|
## Records, classes, and immutability
|
|
35
27
|
|
|
36
|
-
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
otherwise.
|
|
44
|
-
- Favor `init`-only properties (or a primary constructor) over a
|
|
45
|
-
public setter on anything that should not change after construction —
|
|
46
|
-
a mutable public setter on a "value" type invites accidental aliasing
|
|
47
|
-
bugs.
|
|
28
|
+
- `record`/`record class` for a type whose identity is its value (DTO,
|
|
29
|
+
command, event, immutable snapshot); `class` for identity beyond data,
|
|
30
|
+
mutable lifetime state, or ORM-tracked entity semantics.
|
|
31
|
+
- `record struct` for a small, frequently-allocated value type where heap
|
|
32
|
+
allocation matters; reference `record` otherwise.
|
|
33
|
+
- `init`-only properties (or a primary constructor) over a public setter on
|
|
34
|
+
anything that should not change after construction.
|
|
48
35
|
|
|
49
36
|
## Pattern matching and modern idiom
|
|
50
37
|
|
|
51
|
-
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
explicit constructor body once it needs validation, logic, or more than
|
|
59
|
-
trivial assignment.
|
|
60
|
-
- Use collection expressions (`int[] xs = [1, 2, 3];`, `List<T> ys = [];`)
|
|
61
|
-
for new collection-literal code instead of `new[] { ... }`/
|
|
62
|
-
`new List<T> { ... }`, unless the project's existing style has not
|
|
63
|
-
adopted them yet — match what is there rather than mixing both styles
|
|
38
|
+
- Pattern matching (`is`, `switch` expressions, property and list patterns)
|
|
39
|
+
over `if`/`else if` type-check chains or cast-then-check.
|
|
40
|
+
- A primary constructor (`class OrderService(IOrderRepository repo)`) for a
|
|
41
|
+
type whose constructor only assigns parameters to fields; an explicit
|
|
42
|
+
constructor once it needs validation or logic.
|
|
43
|
+
- Collection expressions (`int[] xs = [1, 2, 3];`, `List<T> ys = [];`) in
|
|
44
|
+
new code unless the project has not adopted them; do not mix both styles
|
|
64
45
|
in one file.
|
|
65
|
-
- Expression-bodied members
|
|
66
|
-
|
|
67
|
-
body once the member needs more than one statement or local reasoning
|
|
68
|
-
step — do not force multi-statement logic into one expression via `;`
|
|
69
|
-
chaining or nested ternaries.
|
|
46
|
+
- Expression-bodied members for single-expression members; a block body once
|
|
47
|
+
more than one statement is needed, no `;` chaining or nested ternaries.
|
|
70
48
|
|
|
71
49
|
## Nullable reference types
|
|
72
50
|
|
|
73
|
-
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
with a
|
|
81
|
-
|
|
82
|
-
point for a reference-typed parameter that must not be null, instead of
|
|
83
|
-
a hand-rolled `if (value is null) throw new ArgumentNullException(...)`.
|
|
84
|
-
- Resolve a nullability warning by fixing the actual null path (a real
|
|
85
|
-
null check, a non-null initializer, restructuring so the compiler can
|
|
86
|
-
see the invariant) — never with the null-forgiving operator (`value!`)
|
|
87
|
-
as a substitute for an actual guard, except at a boundary where you can
|
|
88
|
-
state in a comment exactly why the value is provably non-null there.
|
|
51
|
+
- Keep `<Nullable>enable</Nullable>` on; no `<Nullable>disable</Nullable>` or
|
|
52
|
+
blanket `#nullable disable` to silence warnings.
|
|
53
|
+
- A parameter, property, or return type that can be absent is annotated `?`;
|
|
54
|
+
a member without `?` never returns null.
|
|
55
|
+
- `ArgumentNullException.ThrowIfNull(value)` at a public API's entry point,
|
|
56
|
+
not a hand-rolled `if (value is null) throw ...`.
|
|
57
|
+
- Resolve a nullability warning by fixing the null path; the null-forgiving
|
|
58
|
+
operator (`value!`) only at a boundary with a comment stating why the value
|
|
59
|
+
is provably non-null.
|
|
89
60
|
|
|
90
61
|
## `var` usage
|
|
91
62
|
|
|
92
|
-
-
|
|
93
|
-
(`var order = new Order()
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
`Parse`'s return type is not obvious from the name) or when the
|
|
99
|
-
declared type differs meaningfully from the initializer's runtime type
|
|
100
|
-
(assigning a derived type but declaring the base).
|
|
63
|
+
- `var` when the right-hand side makes the type obvious
|
|
64
|
+
(`var order = new Order();`) or the type is long/generic and adds no
|
|
65
|
+
clarity.
|
|
66
|
+
- An explicit type when `var` would hide a non-obvious return type
|
|
67
|
+
(`var result = Parse(input);`) or when the declared type differs from the
|
|
68
|
+
initializer's runtime type.
|
|
@@ -7,102 +7,63 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# C#/.NET patterns
|
|
9
9
|
|
|
10
|
-
Narrows `core-common-rules`' stack-agnostic design guidance to
|
|
11
|
-
|
|
12
|
-
dependency injection, LINQ, and disposal. Applies only to
|
|
10
|
+
Narrows `core-common-rules`' stack-agnostic design guidance to idiomatic
|
|
11
|
+
C#/.NET design (Framework Design Guidelines, Microsoft async/DI guidance):
|
|
12
|
+
async/await, dependency injection, LINQ, and disposal. Applies only to
|
|
13
|
+
`*.cs` files.
|
|
13
14
|
|
|
14
15
|
## Async/await
|
|
15
16
|
|
|
16
|
-
- Never
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
(UI thread, ASP.NET classic `SynchronizationContext`), call
|
|
29
|
-
`.ConfigureAwait(false)` on an awaited `Task` to avoid an unnecessary
|
|
30
|
-
context-capture and reduce deadlock risk. Modern ASP.NET Core has no
|
|
31
|
-
such `SynchronizationContext`, so application-level request-handling
|
|
32
|
-
code in an ASP.NET Core project does not need it — match what the
|
|
33
|
-
project's own code already does.
|
|
34
|
-
- Never call `.Result`, `.Wait()`, or `.GetAwaiter().GetResult()` on a
|
|
35
|
-
`Task` from otherwise-synchronous code to "call an async method
|
|
36
|
-
synchronously" — this is sync-over-async and can deadlock when the
|
|
37
|
-
awaited call needs to resume on a context the blocking thread is
|
|
38
|
-
itself occupying. Make the calling method `async` and `await` instead;
|
|
39
|
-
if the call truly cannot become async (a legacy synchronous interface
|
|
40
|
-
boundary), that is a design problem to flag, not to paper over.
|
|
17
|
+
- Never `async void` except for an actual event handler; every other async
|
|
18
|
+
method returns `Task` or `Task<T>`.
|
|
19
|
+
- `Task`/`Task<T>` by default; `ValueTask`/`ValueTask<T>` only for a hot-path
|
|
20
|
+
method that frequently completes synchronously (a cache hit) where the
|
|
21
|
+
allocation is measured to matter; a `ValueTask` is awaited exactly once and
|
|
22
|
+
never stored.
|
|
23
|
+
- `.ConfigureAwait(false)` on awaited tasks in library code that does not
|
|
24
|
+
need a captured context. Application-level request-handling code in
|
|
25
|
+
ASP.NET Core does not need it; match what the project's code does.
|
|
26
|
+
- Never `.Result`, `.Wait()`, or `.GetAwaiter().GetResult()` on a `Task`
|
|
27
|
+
from synchronous code; make the caller `async`. If it truly cannot be
|
|
28
|
+
(a legacy synchronous interface boundary), flag it as a design problem.
|
|
41
29
|
|
|
42
30
|
## Dependency injection
|
|
43
31
|
|
|
44
|
-
- Register
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
-
|
|
53
|
-
|
|
54
|
-
is what makes the type unit-testable without a real database/HTTP call.
|
|
55
|
-
A single internal implementation gets no interface.
|
|
56
|
-
- Prefer constructor injection for required dependencies; reserve
|
|
57
|
-
property injection for genuinely optional dependencies with a working
|
|
58
|
-
default, which is uncommon.
|
|
32
|
+
- Register at the narrowest correct lifetime: `Singleton` for stateless or
|
|
33
|
+
thread-safe shared state, `Scoped` for per-request/unit-of-work state
|
|
34
|
+
(`DbContext`), `Transient` for cheap or intentionally non-shared
|
|
35
|
+
instances. Never capture a `Scoped` service in a `Singleton`; resolve it
|
|
36
|
+
per use via `IServiceScopeFactory`/`IServiceProvider.CreateScope()`.
|
|
37
|
+
- Depend on an interface (`IOrderRepository`) only at an I/O or substitution
|
|
38
|
+
boundary that DI resolves (repository, external client, clock). A single
|
|
39
|
+
internal implementation gets no interface.
|
|
40
|
+
- Constructor injection for required dependencies; property injection only
|
|
41
|
+
for optional dependencies with a working default.
|
|
59
42
|
|
|
60
43
|
## LINQ efficiency
|
|
61
44
|
|
|
62
|
-
- Materialize
|
|
63
|
-
more than once
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
-
|
|
69
|
-
large sequence when one combined pass would do — each pass is a full
|
|
70
|
-
additional enumeration for an in-memory `IEnumerable<T>`.
|
|
71
|
-
- On an `IQueryable<T>` backed by a database (EF Core), keep filtering
|
|
72
|
-
(`.Where`) and projection (`.Select`) in the query itself so they
|
|
73
|
-
translate to SQL, rather than calling `.ToList()` early and then
|
|
74
|
-
filtering in memory — the latter pulls the whole table across the wire
|
|
75
|
-
first.
|
|
76
|
-
- Use `.Any()` to check for existence, never `.Count() > 0`, which forces
|
|
77
|
-
a full count when a short-circuiting existence check would do.
|
|
45
|
+
- Materialize with `.ToList()`/`.ToArray()` before enumerating a deferred
|
|
46
|
+
query more than once.
|
|
47
|
+
- One combined pass instead of stacked `.Where()`/`.Select()` passes over the
|
|
48
|
+
same large in-memory sequence.
|
|
49
|
+
- On an EF Core `IQueryable<T>`, keep `.Where` and `.Select` in the query so
|
|
50
|
+
they translate to SQL; no early `.ToList()` then in-memory filtering.
|
|
51
|
+
- `.Any()` for existence, never `.Count() > 0`.
|
|
78
52
|
|
|
79
53
|
## Disposal patterns
|
|
80
54
|
|
|
81
|
-
- Any local or field
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
streaming/channel types expose one) so disposal itself is awaited
|
|
88
|
-
instead of blocking a thread on synchronous `Dispose()`.
|
|
89
|
-
- A class that owns one or more `IDisposable`/`IAsyncDisposable`
|
|
90
|
-
fields implements `IDisposable`/`IAsyncDisposable` itself and disposes
|
|
91
|
-
them in its own `Dispose`/`DisposeAsync`, rather than requiring every
|
|
92
|
-
caller to know which of its fields need cleanup.
|
|
55
|
+
- Any `IDisposable` local or field is in a `using` statement/declaration
|
|
56
|
+
(`using var conn = new SqlConnection(...);`) or disposed in `Dispose()`/
|
|
57
|
+
`finally`, never left for the finalizer.
|
|
58
|
+
- `await using` for an `IAsyncDisposable`.
|
|
59
|
+
- A class owning `IDisposable`/`IAsyncDisposable` fields implements
|
|
60
|
+
`IDisposable`/`IAsyncDisposable` and disposes them itself.
|
|
93
61
|
|
|
94
62
|
## Anti-patterns to flag
|
|
95
63
|
|
|
96
|
-
- A "god service"/"utils" static class
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
-
|
|
101
|
-
|
|
102
|
-
letting an unexpected one propagate) — see `rules/security.mdc` and
|
|
103
|
-
`dotnet-code-review`'s focus list for the review-time version of this
|
|
104
|
-
check.
|
|
105
|
-
- Exposing a mutable `List<T>`/collection field directly as a public
|
|
106
|
-
property instead of `IReadOnlyList<T>`/`IReadOnlyCollection<T>` (or a
|
|
107
|
-
defensive copy), which lets any caller mutate internal state through a
|
|
108
|
-
reference they were only meant to read.
|
|
64
|
+
- A "god service"/"utils" static class of unrelated helpers.
|
|
65
|
+
- Catching `Exception` broadly and continuing, instead of the specific
|
|
66
|
+
exception type or letting it propagate; see `rules/security.mdc` and
|
|
67
|
+
`dotnet-code-review`'s focus list for the review-time version.
|
|
68
|
+
- A public mutable `List<T>`/collection property instead of
|
|
69
|
+
`IReadOnlyList<T>`/`IReadOnlyCollection<T>` or a defensive copy.
|