@mrciphersmith/keryx 0.4.28 → 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.
- package/dist/cli.js +4916 -3655
- package/dist/core.js +91 -28
- package/dist/keryx-general-worker-entry.js +413 -240
- package/package.json +1 -1
- package/src/gdskills/bundled/install-manifest.json +3 -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 +40 -150
- package/src/gdskills/bundled/rules/core/cli-interface-design.mdc +63 -194
- package/src/gdskills/bundled/rules/core/code-style-patterns.mdc +36 -48
- 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 +57 -109
- 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/skill-authoring.mdc +190 -0
- package/src/gdskills/bundled/rules/core/skills-storage-workflow.mdc +5 -0
- package/src/gdskills/bundled/rules/core/solid-principles.mdc +70 -121
- 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/orchestration/task-implementer/SKILL.md +2 -2
- package/src/gdskills/bundled/skills/quality/deprecation-path/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/quality/tests-creator/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-architecture/SKILL.md +25 -61
- package/src/gdskills/bundled/skills/review/review-backend/SKILL.md +15 -69
- package/src/gdskills/bundled/skills/review/review-clean-code/SKILL.md +84 -418
- package/src/gdskills/bundled/skills/review/review-core-boundaries/SKILL.md +2 -2
- package/src/gdskills/bundled/skills/review/review-degradation/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-flow-graph/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-frontend/SKILL.md +17 -311
- package/src/gdskills/bundled/skills/review/review-frontend-conventions/SKILL.md +8 -22
- package/src/gdskills/bundled/skills/review/review-highload/SKILL.md +84 -228
- package/src/gdskills/bundled/skills/review/review-layout/SKILL.md +22 -28
- package/src/gdskills/bundled/skills/review/review-logic/SKILL.md +9 -10
- package/src/gdskills/bundled/skills/review/review-performance/SKILL.md +16 -38
- package/src/gdskills/bundled/skills/review/review-premise/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-regression/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-security-code/SKILL.md +8 -8
- package/src/gdskills/bundled/skills/review/review-style/SKILL.md +49 -88
- package/src/gdskills/bundled/skills/review/review-testing-practices/SKILL.md +22 -40
- 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 -83
- 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 +21 -40
- 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 +15 -48
- 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,84 +7,35 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# Ruby on Rails security
|
|
9
9
|
|
|
10
|
-
Narrows `core-common-rules`'
|
|
11
|
-
Rails-specific, OWASP-relevant risks and the safe framework API to use
|
|
12
|
-
instead. Applies to `*.rb` and `*.erb` files.
|
|
10
|
+
Narrows `core-common-rules`' security rules to Rails-specific, OWASP-relevant risks and the safe framework API. Applies to `*.rb` and `*.erb` files.
|
|
13
11
|
|
|
14
12
|
## Mass assignment and strong parameters
|
|
15
13
|
|
|
16
|
-
- Never build or update a model from a raw `params` hash
|
|
17
|
-
|
|
18
|
-
permitted shape first with `params.expect(model: [:field, ...])`
|
|
19
|
-
(current Rails idiom) or `params.require(:model).permit(:field, ...)`,
|
|
20
|
-
and never call `permit!` on user-controlled params — it disables
|
|
21
|
-
strong parameters entirely and lets any attacker-supplied key through,
|
|
22
|
-
including ones like `admin`/`role` that were never meant to be
|
|
23
|
-
user-settable.
|
|
24
|
-
- List every permitted attribute explicitly; do not permit a whole
|
|
25
|
-
nested hash with a bare `permit(:nested)` when only specific
|
|
26
|
-
sub-keys should be writable.
|
|
14
|
+
- Never build or update a model from a raw `params` hash; require the permitted shape with `params.expect(model: [:field, ...])` or `params.require(:model).permit(:field, ...)`, and never call `permit!` on user-controlled params.
|
|
15
|
+
- List every permitted attribute explicitly; no bare `permit(:nested)` for a whole nested hash when only some sub-keys are writable.
|
|
27
16
|
|
|
28
17
|
## SQL and data access
|
|
29
18
|
|
|
30
|
-
- Use
|
|
31
|
-
|
|
32
|
-
string-interpolate a user-influenced value into `where`,
|
|
33
|
-
`find_by_sql`, `order`, or any other query method
|
|
34
|
-
(`where("name = '#{params[:name]}'")` is SQL injection).
|
|
35
|
-
- When raw SQL genuinely cannot be avoided (a fragment passed to
|
|
36
|
-
`order`/`pluck` built from a dynamic column list, for instance), pass
|
|
37
|
-
untrusted values through `ActiveRecord::Base.sanitize_sql`/
|
|
38
|
-
`sanitize_sql_array`, never through direct string interpolation.
|
|
19
|
+
- Use parameterized conditions (`where("age > ?", age)`, `where(age: age)`, named placeholders); never interpolate a user-influenced value into `where`, `find_by_sql`, `order` or any other query method.
|
|
20
|
+
- When raw SQL is unavoidable, pass untrusted values through `ActiveRecord::Base.sanitize_sql`/`sanitize_sql_array`.
|
|
39
21
|
|
|
40
22
|
## Output encoding and XSS
|
|
41
23
|
|
|
42
|
-
- Rely on ERB's automatic
|
|
43
|
-
user input; using `raw(value)` or `value.html_safe` on
|
|
44
|
-
user-controlled or user-influenced content reintroduces XSS by
|
|
45
|
-
marking untrusted text as pre-escaped. Reserve `raw`/`html_safe` for
|
|
46
|
-
genuinely static, developer-authored content, and prefer a sanitizer
|
|
47
|
-
(`sanitize`) over `html_safe` when some user-supplied markup must be
|
|
48
|
-
allowed through.
|
|
24
|
+
- Rely on ERB's automatic escaping; never use `raw(value)` or `value.html_safe` on user-influenced content. Reserve them for static, developer-authored content, and prefer `sanitize` when some user markup must be allowed.
|
|
49
25
|
|
|
50
26
|
## Authentication and authorization
|
|
51
27
|
|
|
52
|
-
- Every
|
|
53
|
-
|
|
54
|
-
equivalent) — an action added to an already-protected controller
|
|
55
|
-
inherits the filter only if it is not explicitly skipped
|
|
56
|
-
(`skip_before_action`); check that a new action was not accidentally
|
|
57
|
-
excluded.
|
|
58
|
-
- Authentication (who is this) is not authorization (what can they do):
|
|
59
|
-
confirm the current user is allowed to act on *this specific* record
|
|
60
|
-
(ownership/role check), not just that they are logged in at all —
|
|
61
|
-
an unauthorized action on someone else's resource (IDOR) is a
|
|
62
|
-
distinct bug from a missing login check.
|
|
28
|
+
- Every action requiring a logged-in user runs a `before_action` (e.g. `:authenticate_user!`); check that a new action is not excluded by `skip_before_action`.
|
|
29
|
+
- Check that the current user may act on this specific record (ownership/role), not just that they are logged in (IDOR).
|
|
63
30
|
|
|
64
31
|
## CSRF
|
|
65
32
|
|
|
66
|
-
- Keep `protect_from_forgery with: :exception` (or the
|
|
67
|
-
`ActionController::Base` default) enabled for any controller serving
|
|
68
|
-
browser-submitted forms; do not disable CSRF protection
|
|
69
|
-
(`skip_before_action :verify_authenticity_token`,
|
|
70
|
-
`protect_from_forgery with: :null_session`) on an action reachable
|
|
71
|
-
from a standard HTML form just to make a request easier to test or
|
|
72
|
-
call from a script — scope any real exception (a webhook endpoint
|
|
73
|
-
verified by its own signature, for instance) as narrowly as possible
|
|
74
|
-
and say why in the change.
|
|
33
|
+
- Keep `protect_from_forgery with: :exception` (or the `ActionController::Base` default) for any controller serving browser forms; do not use `skip_before_action :verify_authenticity_token` or `protect_from_forgery with: :null_session` on an action reachable from an HTML form. Scope a real exception (a webhook verified by its own signature) as narrowly as possible and say why in the change.
|
|
75
34
|
|
|
76
35
|
## Background jobs
|
|
77
36
|
|
|
78
|
-
-
|
|
79
|
-
logical unit of work (most production queue adapters are at-least-once) —
|
|
80
|
-
guard non-idempotent side effects (charging a card, sending an email)
|
|
81
|
-
with a uniqueness check or an idempotency key rather than assuming
|
|
82
|
-
single delivery.
|
|
37
|
+
- Make an `ActiveJob` tolerate running more than once (adapters are at-least-once); guard non-idempotent side effects (charging a card, sending an email) with a uniqueness check or idempotency key.
|
|
83
38
|
|
|
84
39
|
## Secrets
|
|
85
40
|
|
|
86
|
-
- Never commit a credential, API key
|
|
87
|
-
store it in Rails encrypted credentials (`bin/rails credentials:edit`)
|
|
88
|
-
or an environment variable the deployment already manages, and load it
|
|
89
|
-
through `Rails.application.credentials`/`ENV`, not a literal in a
|
|
90
|
-
`.rb` file.
|
|
41
|
+
- Never commit a credential, API key or signing secret; use Rails encrypted credentials (`bin/rails credentials:edit`) or a deployment-managed environment variable, loaded via `Rails.application.credentials`/`ENV`.
|
|
@@ -7,83 +7,32 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# Ruby on Rails testing
|
|
9
9
|
|
|
10
|
-
Narrows `core-common-rules`'
|
|
11
|
-
testing idiom. Covers RSpec (the more common choice in current Rails
|
|
12
|
-
apps) and notes the Minitest equivalent where it differs meaningfully.
|
|
13
|
-
Applies only to `*.rb` files.
|
|
10
|
+
Narrows `core-common-rules`' testing rules to Rails: RSpec, with the Minitest equivalent where it differs. Applies only to `*.rb` files.
|
|
14
11
|
|
|
15
12
|
## Layout and framework choice
|
|
16
13
|
|
|
17
|
-
- Match
|
|
18
|
-
|
|
19
|
-
introduce the other framework into a project that has standardized on
|
|
20
|
-
one.
|
|
21
|
-
- Mirror `app/`'s structure under `spec/`/`test/`: a model at
|
|
22
|
-
`app/models/order.rb` gets a spec at `spec/models/order_spec.rb`, a
|
|
23
|
-
job at `app/jobs/charge_job.rb` gets
|
|
24
|
-
`spec/jobs/charge_job_spec.rb`, and so on.
|
|
14
|
+
- Match the project's framework (`spec/` with RSpec `*_spec.rb`, or `test/` with Minitest `*_test.rb`); do not introduce the other.
|
|
15
|
+
- Mirror `app/` under `spec/`/`test/` (`app/models/order.rb` gets `spec/models/order_spec.rb`).
|
|
25
16
|
|
|
26
17
|
## RSpec conventions
|
|
27
18
|
|
|
28
|
-
- `
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
- Use `let`/`let!` for lazily/eagerly built fixtures shared across
|
|
33
|
-
examples in a block instead of repeating setup in every `it`;
|
|
34
|
-
`before` blocks for side effects that must run before each example.
|
|
35
|
-
- Request specs (`spec/requests`) exercise a controller action through
|
|
36
|
-
the full stack (routing, before_actions, rendering) and are generally
|
|
37
|
-
preferred over controller specs for new Rails apps; system specs
|
|
38
|
-
(`spec/system`, via Capybara) exercise a full browser flow — pick the
|
|
39
|
-
narrowest layer that actually covers what changed.
|
|
40
|
-
- Use FactoryBot (`create`/`build`, `build_stubbed` when persistence
|
|
41
|
-
isn't needed) if the project already has it configured, matching its
|
|
42
|
-
existing factory definitions rather than hand-building fixtures a
|
|
43
|
-
different way.
|
|
19
|
+
- Use `context` for a precondition and `it` for the observable behavior; no vague `it "works"`.
|
|
20
|
+
- Use `let`/`let!` for shared fixtures and `before` for side effects, not repeated setup per `it`.
|
|
21
|
+
- Prefer request specs (`spec/requests`) over controller specs for new Rails apps; use system specs (`spec/system`, Capybara) for browser flows; pick the narrowest layer that covers the change.
|
|
22
|
+
- Use FactoryBot (`create`/`build`, `build_stubbed` when persistence is not needed) if configured, matching existing factories.
|
|
44
23
|
|
|
45
24
|
## Minitest conventions (when the project uses it instead)
|
|
46
25
|
|
|
47
|
-
- `test "does something" do ... end`
|
|
48
|
-
|
|
49
|
-
request tests); fixtures come from `test/fixtures/*.yml` or the
|
|
50
|
-
project's own factory setup.
|
|
51
|
-
- Assertions read `assert_equal expected, actual` (expected first) and
|
|
52
|
-
`assert_predicate`/`assert_raises` over a bare `assert` on a boolean
|
|
53
|
-
expression, for a clearer failure message.
|
|
26
|
+
- Write `test "does something" do ... end` in `ActiveSupport::TestCase` (or `ActionDispatch::IntegrationTest` for request tests); fixtures come from `test/fixtures/*.yml` or the project's factory setup.
|
|
27
|
+
- Use `assert_equal expected, actual` (expected first) and `assert_predicate`/`assert_raises` over a bare `assert` on a boolean.
|
|
54
28
|
|
|
55
29
|
## Database and external boundaries
|
|
56
30
|
|
|
57
|
-
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
in a test unless the app has opted out of transactional tests for a
|
|
61
|
-
specific reason (e.g. system specs across threads), in which case
|
|
62
|
-
match whatever cleanup strategy (DatabaseCleaner, truncation) it
|
|
63
|
-
already uses.
|
|
64
|
-
- Stub/mock an external HTTP call (a third-party API, a webhook) at the
|
|
65
|
-
boundary (WebMock, VCR cassettes, or the project's existing tooling)
|
|
66
|
-
— a test must not make a real network call.
|
|
67
|
-
- Assert an `ActiveJob` was enqueued with `have_enqueued_job`
|
|
68
|
-
(RSpec) / `assert_enqueued_with` (Minitest) rather than asserting on
|
|
69
|
-
its side effects by actually running it, unless the test's purpose is
|
|
70
|
-
specifically to exercise the job's `perform` behavior — in which case
|
|
71
|
-
drain the queue explicitly (`perform_enqueued_jobs`) instead of
|
|
72
|
-
sleeping or polling for it to run.
|
|
31
|
+
- Rely on per-example transactional rollback; do not manually clean up records unless the app has opted out (e.g. system specs across threads), then match its cleanup strategy (DatabaseCleaner, truncation).
|
|
32
|
+
- Stub external HTTP calls at the boundary (WebMock, VCR cassettes, or the project's tooling); no real network calls.
|
|
33
|
+
- Assert enqueuing with `have_enqueued_job` (RSpec) / `assert_enqueued_with` (Minitest); to exercise `perform`, drain with `perform_enqueued_jobs`.
|
|
73
34
|
|
|
74
35
|
## Determinism
|
|
75
36
|
|
|
76
|
-
- Never synchronize with `sleep`
|
|
77
|
-
|
|
78
|
-
`perform_enqueued_jobs`, a Capybara-provided wait matcher
|
|
79
|
-
(`have_content` retries automatically), or an explicit polling helper
|
|
80
|
-
with a timeout.
|
|
81
|
-
- Freeze time (`travel_to`/`Timecop`, matching whatever the project
|
|
82
|
-
already uses) for any test asserting on a timestamp or a
|
|
83
|
-
time-dependent calculation, instead of asserting against
|
|
84
|
-
`Time.current` computed twice and racing a clock tick.
|
|
85
|
-
|
|
86
|
-
## Regression discipline
|
|
87
|
-
|
|
88
|
-
- A bug fix gets a regression test that fails before the fix and passes
|
|
89
|
-
after; new behavior gets new coverage in the same change.
|
|
37
|
+
- Never synchronize with `sleep`; use `perform_enqueued_jobs`, a Capybara wait matcher (`have_content`), or a polling helper with a timeout.
|
|
38
|
+
- Freeze time (`travel_to`/`Timecop`, matching the project) for any timestamp or time-dependent assertion.
|
|
@@ -8,86 +8,60 @@ metadata:
|
|
|
8
8
|
# Rust coding style
|
|
9
9
|
|
|
10
10
|
Narrows `core-common-rules`' stack-agnostic style rules to modern Rust
|
|
11
|
-
(2021/2024 edition) idiom
|
|
12
|
-
|
|
11
|
+
(2021/2024 edition) idiom, following the Rust API Guidelines. Applies only
|
|
12
|
+
to `*.rs` files.
|
|
13
13
|
|
|
14
14
|
## Naming and structure
|
|
15
15
|
|
|
16
|
-
-
|
|
17
|
-
`
|
|
18
|
-
for constants and statics — `rustfmt`/`rustc` will warn on a mismatch.
|
|
19
|
-
- A crate's public surface lives at the paths re-exported from `lib.rs`;
|
|
20
|
-
keep implementation details `pub(crate)` or private and re-export
|
|
21
|
-
deliberately, rather than making every module `pub` by default.
|
|
22
|
-
- An identifier's name shrinks with its scope: a short-lived closure
|
|
23
|
-
parameter can be `x`, a `pub` function gets a full descriptive name.
|
|
16
|
+
- A crate's public surface is what `lib.rs` re-exports; keep details
|
|
17
|
+
`pub(crate)` or private rather than every module `pub`.
|
|
24
18
|
|
|
25
19
|
## Ownership, borrowing, and errors
|
|
26
20
|
|
|
27
|
-
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
- `
|
|
36
|
-
|
|
37
|
-
`
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
file read, a map lookup on external data) in production code without a
|
|
42
|
-
stated invariant that makes it truly infallible; prefer `?`, a `match`,
|
|
43
|
-
or a combinator (`unwrap_or`, `unwrap_or_else`, `ok_or`) that handles the
|
|
44
|
-
failure path.
|
|
45
|
-
- Use `thiserror` to derive `std::error::Error` for a library's own error
|
|
46
|
-
enum (`#[error("...")]` per variant, `#[source]`/`#[from]` to preserve
|
|
47
|
-
the underlying cause); use `anyhow::Result`/`.context()`/`.with_context()`
|
|
48
|
-
in application/binary code that just needs to propagate and annotate
|
|
49
|
-
errors up to `main`, not to dictate a precise error type to its own
|
|
50
|
-
callers.
|
|
21
|
+
- Borrow (`&T`/`&mut T`) unless the callee keeps the value; `&str`/`&[T]`
|
|
22
|
+
parameters over `&String`/`&Vec<T>`.
|
|
23
|
+
- Resolve a borrow-checker conflict by restructuring ownership, splitting the
|
|
24
|
+
borrow, or shortening a lifetime; `.clone()` only when a duplicate is
|
|
25
|
+
needed, with a comment when non-obvious.
|
|
26
|
+
- `Result<T, E>` for anything that can fail, propagated with `?` rather than
|
|
27
|
+
a `match` that re-returns `Err`; `Option<T>` for genuine absence, not
|
|
28
|
+
instead of `Result`.
|
|
29
|
+
- No `.unwrap()`/`.expect()` on a `Result`/`Option` built from fallible or
|
|
30
|
+
untrusted input in production code without a stated invariant; use `?`, a
|
|
31
|
+
`match`, or `unwrap_or`/`unwrap_or_else`/`ok_or`.
|
|
32
|
+
- `thiserror` for a library's own error enum (`#[error("...")]`,
|
|
33
|
+
`#[source]`/`#[from]`); `anyhow::Result` with `.context()`/`.with_context()`
|
|
34
|
+
in application/binary code.
|
|
51
35
|
|
|
52
36
|
## Traits and generics
|
|
53
37
|
|
|
54
|
-
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
needed — `impl Trait` avoids the allocation and indirection.
|
|
61
|
-
- Derive `Debug`, `Clone`, `PartialEq`, etc. instead of hand-writing them
|
|
62
|
-
when the type's fields support it; hand-write only when the derived
|
|
63
|
-
behavior would be wrong for the type's invariants.
|
|
38
|
+
- Narrowest trait bound the function needs (`AsRef<str>` over `&String`,
|
|
39
|
+
`Iterator<Item = T>` over `Vec<T>`).
|
|
40
|
+
- Return `impl Trait` over `Box<dyn Trait>` unless runtime polymorphism is
|
|
41
|
+
needed.
|
|
42
|
+
- Derive `Debug`, `Clone`, `PartialEq`, etc.; hand-write only when the
|
|
43
|
+
derived behavior would be wrong for the type's invariants.
|
|
64
44
|
|
|
65
45
|
## Iterators and control flow
|
|
66
46
|
|
|
67
|
-
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
- Use pattern matching (`match`, `if let`, `let else`) to handle every
|
|
72
|
-
variant of an enum explicitly; avoid a catch-all `_ =>` arm on a `match`
|
|
73
|
-
over your own enum where a new variant should force every call site to
|
|
74
|
-
be revisited.
|
|
47
|
+
- An iterator chain over an indexed loop when it reads as clearly; do not
|
|
48
|
+
force a chain that reads worse than a `for` loop.
|
|
49
|
+
- Match every variant of your own enum explicitly; no catch-all `_ =>` arm
|
|
50
|
+
where a new variant should force call sites to be revisited.
|
|
75
51
|
|
|
76
52
|
## Async conventions (when the crate uses async)
|
|
77
53
|
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
across an `.await`; prefer `tokio::sync::Mutex` when the lock itself
|
|
86
|
-
must be held across an await point, `std::sync::Mutex` when it never is.
|
|
54
|
+
- No blocking operation (`std::thread::sleep`, synchronous file/socket read,
|
|
55
|
+
a `std::sync` primitive held across `.await`) inside an `async fn` on a
|
|
56
|
+
multi-threaded executor; use `tokio::time::sleep`, `tokio::fs`,
|
|
57
|
+
`tokio::sync::Mutex`, or `tokio::task::spawn_blocking`.
|
|
58
|
+
- Keep a `Mutex` guard's scope short, never across an `.await`;
|
|
59
|
+
`tokio::sync::Mutex` when the lock must be held across an await,
|
|
60
|
+
`std::sync::Mutex` when it never is.
|
|
87
61
|
|
|
88
62
|
## Formatting and lints
|
|
89
63
|
|
|
90
|
-
- Format with `rustfmt` (`cargo fmt`)
|
|
91
|
-
|
|
92
|
-
-
|
|
93
|
-
|
|
64
|
+
- Format with `rustfmt` (`cargo fmt`); do not hand-format around a
|
|
65
|
+
configured formatter.
|
|
66
|
+
- `cargo clippy -- -D warnings`; fix at the root cause, not with a blanket
|
|
67
|
+
`#[allow(...)]`.
|
|
@@ -8,78 +8,51 @@ metadata:
|
|
|
8
8
|
# Rust patterns
|
|
9
9
|
|
|
10
10
|
Narrows `core-common-rules`' stack-agnostic design guidance to idiomatic
|
|
11
|
-
Rust design
|
|
11
|
+
Rust design (Rust API Guidelines, the Rust Design Patterns book). Applies
|
|
12
|
+
only to `*.rs` files.
|
|
12
13
|
|
|
13
14
|
## Crate and module layout
|
|
14
15
|
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
- A workspace (`[workspace]` in the root `Cargo.toml`) splits genuinely
|
|
21
|
-
independent crates (a library plus a CLI that depends on it, or several
|
|
22
|
-
services sharing a core crate) — do not default to a workspace for a
|
|
23
|
-
single-binary project that has no reason to publish separate crates.
|
|
24
|
-
- Keep `pub` surface intentional: re-export the types callers need from
|
|
25
|
-
`lib.rs`, keep internal modules `pub(crate)` or private so the compiler
|
|
26
|
-
(not convention) enforces the boundary.
|
|
16
|
+
- No shared-types-only modules ("models", "types", "common").
|
|
17
|
+
- A `[workspace]` only for genuinely independent crates (a library plus a
|
|
18
|
+
CLI, services sharing a core crate), not a single-binary project.
|
|
19
|
+
- Re-export the types callers need from `lib.rs`; keep internal modules
|
|
20
|
+
`pub(crate)` or private.
|
|
27
21
|
|
|
28
22
|
## Error handling design
|
|
29
23
|
|
|
30
|
-
- A library
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
reads as a story, not a single opaque message.
|
|
39
|
-
- Never use a `Result<T, String>`/`Result<T, Box<dyn Error>>` as a
|
|
40
|
-
permanent design in a library — `Box<dyn Error>` is a reasonable
|
|
41
|
-
quick-prototype return type but forces every caller to string-match or
|
|
42
|
-
downcast; graduate to a concrete `thiserror` enum once the error shapes
|
|
43
|
-
stabilize.
|
|
24
|
+
- A library defines its own `thiserror` enum, one variant per failure mode,
|
|
25
|
+
`#[source]`/`#[from]` on variants wrapping another error; no single
|
|
26
|
+
`Other(String)` variant.
|
|
27
|
+
- An application/binary crate uses `anyhow::Result` at its outer layers and
|
|
28
|
+
`.context("doing x")`/`.with_context(|| ...)` at each `?` that crosses a
|
|
29
|
+
meaningful boundary.
|
|
30
|
+
- No `Result<T, String>`/`Result<T, Box<dyn Error>>` as a permanent library
|
|
31
|
+
design; `Box<dyn Error>` is for prototypes, graduate to a `thiserror` enum.
|
|
44
32
|
|
|
45
33
|
## Ownership-driven API design
|
|
46
34
|
|
|
47
|
-
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
positional signature.
|
|
55
|
-
- Prefer newtypes (`struct UserId(u64);`) over a bare primitive when a
|
|
56
|
-
value has domain meaning distinct from its representation — the
|
|
57
|
-
compiler then rejects passing a `ProductId` where a `UserId` is
|
|
58
|
-
expected, which a bare `u64` parameter cannot.
|
|
35
|
+
- `self` by value only when the method consumes the value (a builder's
|
|
36
|
+
`.build()`), `&mut self` to mutate, `&self` otherwise.
|
|
37
|
+
- Builder (`FooBuilder`, chained `with_x` returning `Self`, terminal
|
|
38
|
+
`.build() -> Result<Foo, ...>`) for a constructor with many optional
|
|
39
|
+
parameters.
|
|
40
|
+
- Newtypes (`struct UserId(u64);`) over a bare primitive when the value has
|
|
41
|
+
domain meaning.
|
|
59
42
|
|
|
60
43
|
## Trait design
|
|
61
44
|
|
|
62
|
-
- Define a trait at the
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
monomorphization's code-size cost is acceptable and static dispatch
|
|
68
|
-
matters; use `Box<dyn Trait>`/`&dyn Trait` when you need a heterogeneous
|
|
69
|
-
collection or a boundary (a plugin registry, a callback list) where the
|
|
70
|
-
concrete type varies at runtime.
|
|
45
|
+
- Define a trait at the abstraction a consumer needs, sized to the methods
|
|
46
|
+
that consumer calls, not every method the concrete type has.
|
|
47
|
+
- Generics (`fn process<T: Processor>(p: &T)`) for hot paths where static
|
|
48
|
+
dispatch matters; `Box<dyn Trait>`/`&dyn Trait` for heterogeneous
|
|
49
|
+
collections or boundaries where the type varies at runtime.
|
|
71
50
|
|
|
72
51
|
## Anti-patterns to flag
|
|
73
52
|
|
|
74
|
-
- A `.clone()` inserted to
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
`thiserror`-derived enum a caller can match on.
|
|
81
|
-
- A "god module" (`utils`, `helpers`, `common`) accumulating unrelated
|
|
82
|
-
free functions — split by what the code actually does, the same
|
|
83
|
-
anti-pattern flagged in Go/Python's own pattern rules.
|
|
84
|
-
- `unwrap()`/`expect()` reached for as a shortcut past a type error during
|
|
85
|
-
development and never revisited before the change ships.
|
|
53
|
+
- A `.clone()` inserted to pass the borrow checker; flag any clone in a hot
|
|
54
|
+
path with no comment explaining it.
|
|
55
|
+
- Stringly-typed errors (`Result<T, String>`) in a library crate.
|
|
56
|
+
- A "god module" (`utils`, `helpers`, `common`).
|
|
57
|
+
- `unwrap()`/`expect()` left in as a development shortcut when the change
|
|
58
|
+
ships.
|
|
@@ -7,79 +7,57 @@ metadata:
|
|
|
7
7
|
|
|
8
8
|
# Rust security
|
|
9
9
|
|
|
10
|
-
Narrows `core-common-rules`' stack-agnostic security rules to Rust-specific
|
|
11
|
-
OWASP
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
Narrows `core-common-rules`' stack-agnostic security rules to Rust-specific
|
|
11
|
+
risks (OWASP) and the safe standard-library/ecosystem API. This file stays
|
|
12
|
+
broad rather than narrowing to a subdirectory, since `unsafe` and
|
|
13
|
+
panic-on-untrusted-input risks appear wherever a crate touches
|
|
14
|
+
attacker-influenced data.
|
|
15
15
|
|
|
16
16
|
## `unsafe` blocks
|
|
17
17
|
|
|
18
|
-
- Every `unsafe` block carries a `# Safety` doc comment
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
`mem::transmute`), not the surrounding safe code, so the audited surface
|
|
26
|
-
stays minimal.
|
|
27
|
-
- Never widen an `unsafe` block or drop its `# Safety` comment to silence
|
|
28
|
-
a compiler or clippy complaint; if the invariant cannot be stated, the
|
|
29
|
-
code is not actually sound and needs a different approach.
|
|
18
|
+
- Every `unsafe` block carries a `# Safety` doc comment stating the specific
|
|
19
|
+
invariants (pointer validity, alignment, lifetime, no concurrent mutation)
|
|
20
|
+
that make it sound.
|
|
21
|
+
- Keep an `unsafe` block as small as possible: wrap only the operation that
|
|
22
|
+
requires it (a raw pointer deref, an FFI call, `mem::transmute`).
|
|
23
|
+
- Never widen an `unsafe` block or drop its `# Safety` comment to silence the
|
|
24
|
+
compiler or clippy.
|
|
30
25
|
|
|
31
26
|
## Panics on untrusted input
|
|
32
27
|
|
|
33
|
-
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
(returns `Option`) instead of `arr[i]` (panics out of bounds); guard
|
|
40
|
-
integer arithmetic on untrusted values with `checked_add`/`checked_mul`/
|
|
41
|
-
`saturating_*`/`wrapping_*` as the operation's semantics require, instead
|
|
42
|
-
of a bare `+`/`*` that panics on overflow in debug builds and silently
|
|
43
|
-
wraps in release builds — both are wrong for a value an attacker
|
|
44
|
-
influences.
|
|
28
|
+
- No `.unwrap()`/`.expect()` on a `Result`/`Option` derived from network
|
|
29
|
+
input, file contents, CLI arguments, or any attacker-influenced source;
|
|
30
|
+
propagate with `?` or handle explicitly.
|
|
31
|
+
- `.get(i)` instead of `arr[i]` for untrusted-derived indices;
|
|
32
|
+
`checked_add`/`checked_mul`/`saturating_*`/`wrapping_*` as the semantics
|
|
33
|
+
require instead of bare `+`/`*` on untrusted values.
|
|
45
34
|
|
|
46
35
|
## FFI boundaries
|
|
47
36
|
|
|
48
|
-
- Validate every value crossing an `extern "C"` boundary
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
- Never let a Rust panic unwind across an `extern "C"` boundary — undefined
|
|
54
|
-
behavior in most toolchains; catch it with `std::panic::catch_unwind` at
|
|
55
|
-
the FFI edge and convert it to an error code instead.
|
|
37
|
+
- Validate every value crossing an `extern "C"` boundary (null, validity,
|
|
38
|
+
alignment before dereferencing); document the ownership/lifetime contract
|
|
39
|
+
(who frees it, with which allocator) in the `# Safety` comment.
|
|
40
|
+
- Never let a panic unwind across an `extern "C"` boundary; catch it with
|
|
41
|
+
`std::panic::catch_unwind` at the FFI edge and return an error code.
|
|
56
42
|
|
|
57
43
|
## Dependency and supply-chain hygiene
|
|
58
44
|
|
|
59
|
-
- Run `cargo audit` (or the project's
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
- Pin dependency versions deliberately in `Cargo.toml`; avoid a bare `*`
|
|
64
|
-
or an overly wide range that lets an unreviewed major/minor bump in.
|
|
45
|
+
- Run `cargo audit` (or the project's equivalent) when introducing or
|
|
46
|
+
updating a dependency, or when asked to check for known vulnerabilities.
|
|
47
|
+
- Pin dependency versions deliberately in `Cargo.toml`; no bare `*` or overly
|
|
48
|
+
wide range.
|
|
65
49
|
|
|
66
50
|
## Deserialization
|
|
67
51
|
|
|
68
52
|
- When deserializing untrusted input with `serde`, apply size/depth limits
|
|
69
|
-
appropriate to the format
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
(deeply-nested structures) vector.
|
|
74
|
-
- Never derive `Deserialize` for a type and then trust its field values
|
|
75
|
-
as pre-validated — validate ranges/formats/invariants after
|
|
76
|
-
deserializing, the same as any other externally-sourced value.
|
|
53
|
+
appropriate to the format (bounded reader, recursion-depth guard, the
|
|
54
|
+
deserializer's own limit configuration).
|
|
55
|
+
- Do not trust a `Deserialize`d type's field values as pre-validated;
|
|
56
|
+
validate ranges/formats/invariants afterwards.
|
|
77
57
|
|
|
78
58
|
## Randomness and secrets
|
|
79
59
|
|
|
80
|
-
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
- Never hard-code a credential, API key, or signing secret in source; load
|
|
85
|
-
it from environment/secret storage the project already uses.
|
|
60
|
+
- A CSPRNG (`rand`'s `ThreadRng`/`OsRng`, `getrandom`, `ring`) for tokens,
|
|
61
|
+
session IDs, keys, nonces.
|
|
62
|
+
- No hard-coded credential, API key, or signing secret; load from the
|
|
63
|
+
environment/secret storage the project already uses.
|