@mrciphersmith/keryx 0.4.29 → 0.4.30

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.
Files changed (123) hide show
  1. package/dist/cli.js +2408 -1369
  2. package/dist/core.js +90 -27
  3. package/dist/keryx-general-worker-entry.js +82 -25
  4. package/package.json +1 -1
  5. package/src/gdskills/bundled/install-manifest.json +2 -0
  6. package/src/gdskills/bundled/rules/core/api-contracts.mdc +21 -136
  7. package/src/gdskills/bundled/rules/core/async-patterns.mdc +18 -171
  8. package/src/gdskills/bundled/rules/core/clean-architecture.mdc +21 -3
  9. package/src/gdskills/bundled/rules/core/cli-interface-design.mdc +63 -194
  10. package/src/gdskills/bundled/rules/core/code-style-patterns.mdc +36 -49
  11. package/src/gdskills/bundled/rules/core/concurrency-patterns.mdc +32 -0
  12. package/src/gdskills/bundled/rules/core/database-patterns.mdc +26 -148
  13. package/src/gdskills/bundled/rules/core/error-handling.mdc +7 -8
  14. package/src/gdskills/bundled/rules/core/frontend-assistant.mdc +14 -29
  15. package/src/gdskills/bundled/rules/core/layout-rendering.mdc +51 -0
  16. package/src/gdskills/bundled/rules/core/mobx-store-template.mdc +69 -511
  17. package/src/gdskills/bundled/rules/core/nestjs-dto.mdc +18 -36
  18. package/src/gdskills/bundled/rules/core/playwright-testing.mdc +35 -308
  19. package/src/gdskills/bundled/rules/core/security-baseline.mdc +18 -140
  20. package/src/gdskills/bundled/rules/core/solid-principles.mdc +31 -7
  21. package/src/gdskills/bundled/rules/core/storybook-guidelines.mdc +20 -22
  22. package/src/gdskills/bundled/rules/core/tdd-workflow.mdc +37 -74
  23. package/src/gdskills/bundled/rules/core/ui-anti-slop.mdc +48 -37
  24. package/src/gdskills/bundled/skills/quality/deprecation-path/SKILL.md +1 -1
  25. package/src/gdskills/bundled/skills/review/review-architecture/SKILL.md +24 -60
  26. package/src/gdskills/bundled/skills/review/review-backend/SKILL.md +13 -65
  27. package/src/gdskills/bundled/skills/review/review-clean-code/SKILL.md +22 -35
  28. package/src/gdskills/bundled/skills/review/review-frontend/SKILL.md +16 -309
  29. package/src/gdskills/bundled/skills/review/review-frontend-conventions/SKILL.md +7 -21
  30. package/src/gdskills/bundled/skills/review/review-highload/SKILL.md +83 -227
  31. package/src/gdskills/bundled/skills/review/review-layout/SKILL.md +22 -28
  32. package/src/gdskills/bundled/skills/review/review-logic/SKILL.md +8 -8
  33. package/src/gdskills/bundled/skills/review/review-performance/SKILL.md +15 -37
  34. package/src/gdskills/bundled/skills/review/review-security-code/SKILL.md +7 -7
  35. package/src/gdskills/bundled/skills/review/review-style/SKILL.md +31 -66
  36. package/src/gdskills/bundled/skills/review/review-testing-practices/SKILL.md +21 -39
  37. package/src/gdskills/bundled/skills/review/review-ui-ux/SKILL.md +42 -82
  38. package/src/gdskills/bundled/stacks/angular/rules/coding-style.mdc +15 -57
  39. package/src/gdskills/bundled/stacks/angular/rules/patterns.mdc +16 -60
  40. package/src/gdskills/bundled/stacks/angular/rules/security.mdc +11 -49
  41. package/src/gdskills/bundled/stacks/angular/rules/testing.mdc +13 -52
  42. package/src/gdskills/bundled/stacks/c-cpp/rules/coding-style.mdc +33 -57
  43. package/src/gdskills/bundled/stacks/c-cpp/rules/patterns.mdc +35 -62
  44. package/src/gdskills/bundled/stacks/c-cpp/rules/security.mdc +35 -62
  45. package/src/gdskills/bundled/stacks/c-cpp/rules/testing.mdc +34 -54
  46. package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/patterns.mdc +27 -56
  47. package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/security.mdc +51 -119
  48. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/coding-style.mdc +42 -74
  49. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/patterns.mdc +45 -84
  50. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/security.mdc +34 -59
  51. package/src/gdskills/bundled/stacks/csharp-dotnet/rules/testing.mdc +33 -62
  52. package/src/gdskills/bundled/stacks/django/rules/coding-style.mdc +15 -56
  53. package/src/gdskills/bundled/stacks/django/rules/patterns.mdc +16 -68
  54. package/src/gdskills/bundled/stacks/django/rules/security.mdc +13 -71
  55. package/src/gdskills/bundled/stacks/django/rules/testing.mdc +16 -62
  56. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/coding-style.mdc +30 -53
  57. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/patterns.mdc +20 -56
  58. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/security.mdc +52 -122
  59. package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/testing.mdc +23 -42
  60. package/src/gdskills/bundled/stacks/fastapi/rules/coding-style.mdc +13 -47
  61. package/src/gdskills/bundled/stacks/fastapi/rules/patterns.mdc +18 -81
  62. package/src/gdskills/bundled/stacks/fastapi/rules/security.mdc +13 -75
  63. package/src/gdskills/bundled/stacks/fastapi/rules/testing.mdc +14 -58
  64. package/src/gdskills/bundled/stacks/flutter-dart/rules/coding-style.mdc +36 -74
  65. package/src/gdskills/bundled/stacks/flutter-dart/rules/patterns.mdc +30 -66
  66. package/src/gdskills/bundled/stacks/flutter-dart/rules/security.mdc +33 -67
  67. package/src/gdskills/bundled/stacks/flutter-dart/rules/testing.mdc +41 -73
  68. package/src/gdskills/bundled/stacks/go/rules/coding-style.mdc +36 -57
  69. package/src/gdskills/bundled/stacks/go/rules/patterns.mdc +27 -42
  70. package/src/gdskills/bundled/stacks/go/rules/security.mdc +26 -43
  71. package/src/gdskills/bundled/stacks/go/rules/testing.mdc +23 -41
  72. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/coding-style.mdc +18 -37
  73. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/patterns.mdc +26 -43
  74. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/security.mdc +25 -37
  75. package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/testing.mdc +34 -50
  76. package/src/gdskills/bundled/stacks/kotlin-android/rules/coding-style.mdc +28 -65
  77. package/src/gdskills/bundled/stacks/kotlin-android/rules/patterns.mdc +34 -78
  78. package/src/gdskills/bundled/stacks/kotlin-android/rules/security.mdc +35 -68
  79. package/src/gdskills/bundled/stacks/kotlin-android/rules/testing.mdc +34 -68
  80. package/src/gdskills/bundled/stacks/mobx/rules/coding-style.mdc +18 -64
  81. package/src/gdskills/bundled/stacks/mobx/rules/patterns.mdc +13 -66
  82. package/src/gdskills/bundled/stacks/mobx/rules/security.mdc +8 -35
  83. package/src/gdskills/bundled/stacks/mobx/rules/testing.mdc +10 -39
  84. package/src/gdskills/bundled/stacks/nestjs/rules/coding-style.mdc +32 -45
  85. package/src/gdskills/bundled/stacks/nestjs/rules/patterns.mdc +40 -60
  86. package/src/gdskills/bundled/stacks/nestjs/rules/security.mdc +30 -50
  87. package/src/gdskills/bundled/stacks/nestjs/rules/testing.mdc +27 -47
  88. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/coding-style.mdc +14 -48
  89. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/patterns.mdc +15 -67
  90. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/security.mdc +12 -50
  91. package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/testing.mdc +11 -43
  92. package/src/gdskills/bundled/stacks/php-laravel/rules/coding-style.mdc +17 -58
  93. package/src/gdskills/bundled/stacks/php-laravel/rules/patterns.mdc +15 -56
  94. package/src/gdskills/bundled/stacks/php-laravel/rules/security.mdc +13 -50
  95. package/src/gdskills/bundled/stacks/php-laravel/rules/testing.mdc +14 -55
  96. package/src/gdskills/bundled/stacks/python/rules/coding-style.mdc +13 -41
  97. package/src/gdskills/bundled/stacks/python/rules/patterns.mdc +20 -61
  98. package/src/gdskills/bundled/stacks/python/rules/security.mdc +15 -57
  99. package/src/gdskills/bundled/stacks/python/rules/testing.mdc +16 -50
  100. package/src/gdskills/bundled/stacks/react/rules/coding-style.mdc +14 -40
  101. package/src/gdskills/bundled/stacks/react/rules/patterns.mdc +15 -55
  102. package/src/gdskills/bundled/stacks/react/rules/security.mdc +13 -49
  103. package/src/gdskills/bundled/stacks/react/rules/testing.mdc +10 -37
  104. package/src/gdskills/bundled/stacks/ruby-rails/rules/coding-style.mdc +12 -45
  105. package/src/gdskills/bundled/stacks/ruby-rails/rules/patterns.mdc +18 -67
  106. package/src/gdskills/bundled/stacks/ruby-rails/rules/security.mdc +11 -60
  107. package/src/gdskills/bundled/stacks/ruby-rails/rules/testing.mdc +14 -65
  108. package/src/gdskills/bundled/stacks/rust/rules/coding-style.mdc +39 -65
  109. package/src/gdskills/bundled/stacks/rust/rules/patterns.mdc +33 -60
  110. package/src/gdskills/bundled/stacks/rust/rules/security.mdc +35 -57
  111. package/src/gdskills/bundled/stacks/rust/rules/testing.mdc +29 -52
  112. package/src/gdskills/bundled/stacks/sql-db/rules/coding-style.mdc +12 -48
  113. package/src/gdskills/bundled/stacks/sql-db/rules/patterns.mdc +22 -113
  114. package/src/gdskills/bundled/stacks/sql-db/rules/security.mdc +11 -56
  115. package/src/gdskills/bundled/stacks/sql-db/rules/testing.mdc +14 -59
  116. package/src/gdskills/bundled/stacks/swift-ios/rules/coding-style.mdc +29 -65
  117. package/src/gdskills/bundled/stacks/swift-ios/rules/patterns.mdc +41 -88
  118. package/src/gdskills/bundled/stacks/swift-ios/rules/security.mdc +28 -53
  119. package/src/gdskills/bundled/stacks/swift-ios/rules/testing.mdc +32 -65
  120. package/src/gdskills/bundled/stacks/vue/rules/coding-style.mdc +17 -52
  121. package/src/gdskills/bundled/stacks/vue/rules/patterns.mdc +17 -63
  122. package/src/gdskills/bundled/stacks/vue/rules/security.mdc +11 -41
  123. 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`' stack-agnostic security rules to the risks
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 context of the **base** repository —
19
- every repo secret the workflow references is available to it (secrets
20
- are consumed, not read/write-scoped, so "access" is simply yes/no here),
21
- and `GITHUB_TOKEN` gets read/write repository permissions by default
22
- (unless a `permissions:` block explicitly narrows it) — even though the
23
- triggering pull request can come from an untrusted fork.
24
- - Never check out and then execute the pull request's own head ref/SHA
25
- (`actions/checkout` with `ref: ${{ github.event.pull_request.head.sha }}`)
26
- inside a `pull_request_target` job and then run a build/test/lint
27
- command against it — that combination (checkout untrusted code + run it
28
- with base-repo secrets) is the "pwn request" pattern: it hands an
29
- attacker-controlled pull request everything it needs to exfiltrate
30
- secrets or push with the workflow's own write access.
31
- - If a `pull_request_target` job genuinely needs to read fork content
32
- (e.g. to comment on a PR), check it out read-only and do not execute
33
- anything from it — no `run:`, no build step, no test step against the
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 like `@v4`
48
- or a branch name — a tag can be moved by the action's own maintainer
49
- (or, if the account is compromised, by an attacker) to point at a
50
- different, malicious commit without the reference in your workflow ever
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
- access is genuinely needed everywhere) at the workflow level, then grant
65
- the specific scopes a job needs (`contents: write`, `pull-requests:
66
- write`, `id-token: write` for OIDC, etc.) at the **job** level, not the
67
- workflow level — a workflow-level `permissions: write-all` (or no
68
- `permissions:` key at all, which can default to broad access depending
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 (a PR
78
- title, issue body, commit message, branch name, or any other
79
- attacker-influenced field) directly into a `run:` shell string — the
80
- expression is substituted into the generated script **before** the
81
- shell runs, so a value containing shell metacharacters (`"`, `` ` ``,
82
- `$(...)`) can break out of the intended string and execute arbitrary
83
- commands on the runner with whatever permissions that job has.
84
- - Pass the untrusted value through an intermediate `env:` variable first,
85
- then reference the environment variable (`"$TITLE"`, quoted) inside the
86
- script, instead of interpolating `${{ github.event.pull_request.title
87
- }}` directly into the `run:` string — the environment variable is set
88
- in memory and never re-enters shell-script generation, which is what
89
- actually neutralizes the injection.
90
- - GitLab CI's own script-injection risk is NOT the same mechanism, and the
91
- `env:`-indirection fix above does not carry over: every GitLab CI/CD
92
- variable (a predefined one like `CI_MERGE_REQUEST_TITLE`, or one you
93
- declare under `variables:`) is already an ordinary shell environment
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 marked "Protected" is only exposed to pipelines running
118
- on a protected branch or a protected tag — an unprotected branch's
119
- pipeline (including one from a fork merge request, when forks are
120
- allowed) never receives it. Do not rely on a protected variable being
121
- present in a job whose `rules:`/`only:`/`except:` allow it to run on a
122
- non-protected ref; the value will simply be empty there, not an error.
123
- - Mark any variable carrying a credential, deploy token, or signing
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 (new project templates enable nullable reference types by
12
- default since .NET 6/C# 10 — the language itself still defaults
13
- annotations/warnings off, so an existing project may need `<Nullable>
14
- enable</Nullable>` added explicitly; primary constructors and
15
- collection expressions since C# 12/.NET 8).
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
- - `PascalCase` for types, methods, properties, and public/protected
22
- members; `camelCase` for locals and parameters; a private field is
23
- `_camelCase` with a leading underscore, not `m_field` or bare
24
- `camelCase` that can collide with a parameter name.
25
- - Interfaces are prefixed `I` (`IOrderRepository`), never suffixed
26
- (`OrderRepositoryInterface`).
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
- - Prefer a `record`/`record class` for a type whose identity is its
37
- value (a DTO, a command, an event, an immutable snapshot) — it gets
38
- structural equality and a `with` expression for free. Reach for `class`
39
- when the type has identity beyond its data, mutable state that changes
40
- over its lifetime, or entity semantics an ORM tracks by reference/key.
41
- - Use `record struct` for a small, frequently-allocated value type where
42
- avoiding heap allocation matters; default to a reference `record`
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
- - Prefer pattern matching (`is`, `switch` expressions, property patterns,
52
- list patterns) over a chain of `if`/`else if` type checks or casts —
53
- `if (shape is Circle { Radius: > 0 } c)` reads its own intent; a cast
54
- followed by a separate null/type check does not.
55
- - Use a primary constructor (`class OrderService(IOrderRepository repo)`)
56
- for a type whose constructor only assigns its parameters to fields —
57
- it removes the boilerplate field-and-assignment block. Fall back to an
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 (`public int Total => Items.Sum(i => i.Price);`)
66
- for a single-expression method/property/operator; keep a full block
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
- - New projects have the nullable annotation and warning contexts on by
74
- default (`<Nullable>enable</Nullable>` in the project file) — keep it
75
- on; do not add `<Nullable>disable</Nullable>` or a blanket
76
- `#nullable disable` to make warnings go away.
77
- - A parameter, property, or return type that can genuinely be absent is
78
- annotated `?` (`string? middleName`); a public member with no `?` is a
79
- promise to callers that it is never null — do not violate that promise
80
- with a hidden `null` return "just this once."
81
- - Use `ArgumentNullException.ThrowIfNull(value)` at a public API's entry
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
- - Use `var` when the right-hand side already makes the type obvious
93
- (`var order = new Order();`, `var items = order.Items.ToList();`) or
94
- when the concrete type is long/generic and adds no clarity
95
- (`var lookup = new Dictionary<string, List<OrderLine>>();`).
96
- - Use an explicit type when `var` would hide the type at a call site that
97
- matters for reading correctness (`var result = Parse(input);` where
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
- idiomatic C#/.NET design and its common anti-patterns: async/await,
12
- dependency injection, LINQ, and disposal. Applies only to `*.cs` files.
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 use `async void` except for an actual event handler
17
- (`private async void Button_Click(object sender, EventArgs e)`) — any
18
- exception an `async void` method throws cannot be awaited or caught by
19
- its caller and crashes the process instead. Every other async method
20
- returns `Task` or `Task<T>`.
21
- - Return `Task`/`Task<T>` for a method whose async work is on the common,
22
- usually-awaited path; return `ValueTask`/`ValueTask<T>` only for a
23
- hot-path method that frequently completes synchronously (a cache hit),
24
- where avoiding the `Task` allocation is measured to matter — a
25
- `ValueTask` must be awaited (or converted) exactly once and never
26
- stored, unlike a `Task`.
27
- - In library code that does not need to resume on a captured context
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 a service at the narrowest lifetime that is correct:
45
- `Singleton` for stateless or thread-safe shared state, `Scoped` for
46
- per-request/per-unit-of-work state (a `DbContext`, a request-scoped
47
- cache), `Transient` for cheap, stateless, or intentionally
48
- non-shared instances. Never inject a `Scoped` service into a
49
- `Singleton` by capturing it directly — that captures the first
50
- request's instance for the app's lifetime; resolve it per-use via
51
- `IServiceScopeFactory`/`IServiceProvider.CreateScope()` instead.
52
- - Depend on an interface (`IOrderRepository`) at an I/O or substitution
53
- boundary that DI resolves (repository, external client, clock) — this
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 a query with `.ToList()`/`.ToArray()` before enumerating it
63
- more than once (in a loop that also iterates it, or across a
64
- `.Count()` followed by a `foreach`) — an `IEnumerable<T>` built from a
65
- `IQueryable<T>`/deferred LINQ chain re-runs the whole query (or, worse,
66
- a whole database round trip) on every enumeration unless it is
67
- materialized once.
68
- - Avoid stacking multiple `.Where()`/`.Select()` passes over the same
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 holding an `IDisposable` is wrapped in a `using`
82
- statement/declaration (`using var conn = new SqlConnection(...);`) or
83
- disposed in a `Dispose()`/`finally` block — never left for the
84
- finalizer, which is non-deterministic and, for something like an open
85
- connection or file handle, holds the resource far longer than needed.
86
- - Use `await using` for an `IAsyncDisposable` (most modern EF Core/
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 that accumulates unrelated
97
- helper methods instead of being split along what the code actually
98
- does — the same smell as a Go "god package," expressed as a static
99
- class here.
100
- - Catching `Exception` broadly and continuing as if nothing happened,
101
- instead of catching the specific exception type a call can throw (or
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.