# rails-hardening-ruby-v1

> rails-hardening-ruby-v1: 8 Keryn rules for ruby (8 warning).

Source: https://keryn.dev/rules/rails-hardening-ruby-v1/

8 rules · Keryn Software Licence · rules 0.8.0 · [all packs](https://keryn.dev/rules/)

**How to read a rule**

- **Severity**: How bad the finding is if it is real: `error` fails a default gate, `warning` and `info` do not.

- **Confidence**: **P0** exploitable as written; **P1** dangerous in context; **P2** a heuristic or a posture check: confirm by reading the code.

- **False-positive risk**: **low**: an exact API or flag, so hits are almost always real; **medium**: the pattern is legitimate in many programs, so check each hit; **high**: it finds a lead to confirm, so expect benign hits.

- **Edge**: The relationship the finding sits on. **reaches**: content or traffic gets to a thing; **calls**: one thing invokes another; **reads**: one thing reads from another; **writes**: one thing writes to another; **sends**: data leaves for another party; **grants**: authority is given or left unchecked; **depends_on**: something relies on a component it does not verify; **trusts**: one party believes another without evidence. The boundary it crosses, when there is one: **user->prompt** (untrusted input reaching a prompt); **model->code** (model output reaching code, a shell, a query or a page); **secret->outside** (a secret leaving the trusted side); **supply-chain** (code, models or prompts pulled in from outside).

- **Taxonomy chips**: References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

## keryn.rails.ruby.cookies-serializer-marshal

`warning` ruby ruby, rails

The cookie serializer is :marshal or :hybrid. Marshal deserializes arbitrary Ruby objects, so anyone who obtains the secret key base (or finds a verifier bypass) turns a forged cookie into code execution. Use :json.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [depends_on](https://keryn.dev/docs/edges/edge-kinds/#depends-on) something relies on a component it does not verify; it crosses the [`supply-chain`](https://keryn.dev/docs/edges/trust-boundaries/#supply-chain) boundary (code, models or prompts pulled in from outside).

[CWE-502](https://keryn.dev/taxonomies/cwe/#cwe-502-deserialization-of-untrusted-data) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Set config.action\_dispatch.cookies\_serializer = :json. Migrate existing sessions with :hybrid for one deploy only, then switch to :json.

**References:** [https://guides.rubyonrails.org/configuring.html#config-action-dispatch-cookies-serializer](https://guides.rubyonrails.org/configuring.html#config-action-dispatch-cookies-serializer)

## keryn.rails.ruby.tls-verify-none

`warning` ruby ruby, rails

Certificate verification is switched off (OpenSSL::SSL::VERIFY\_NONE). Any host on the network path can impersonate the server and read or change the traffic. Keep VERIFY\_PEER and fix the trust store instead.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [trusts](https://keryn.dev/docs/edges/edge-kinds/#trusts) one party believes another without evidence.

[CWE-295](https://keryn.dev/rules/?cwe=CWE-295) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Use verify\_mode = OpenSSL::SSL::VERIFY\_PEER (the default) and point ca\_file at the private CA when needed.

**References:** [https://docs.ruby-lang.org/en/master/OpenSSL/SSL.html](https://docs.ruby-lang.org/en/master/OpenSSL/SSL.html)

## keryn.rails.ruby.json-html-entities-unescaped

`warning` ruby ruby, rails

escape\_html\_entities\_in\_json is set to false. JSON embedded in an HTML page can then close a script tag or open markup (&lt;, &gt;, &amp;). Leave the default (true) unless every JSON response is served as application/json only.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Delete the line or set ActiveSupport::JSON::Encoding.escape\_html\_entities\_in\_json = true.

**References:** [https://api.rubyonrails.org/classes/ActiveSupport/JSON.html](https://api.rubyonrails.org/classes/ActiveSupport/JSON.html)

## keryn.rails.ruby.validation-format-line-anchors

`warning` ruby ruby, rails

A format validation regex is not anchored to the whole string with \\A ... \\z. ^ and $ match at every line in Ruby, so "ok\\n&lt;script&gt;x&lt;/script&gt;" passes a regex meant to accept one line, and an unanchored or half-anchored regex accepts any value that merely contains (or starts or ends with) a valid part. Anchor with \\A and \\z.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-185](https://keryn.dev/rules/?cwe=CWE-185) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Write the expression with \\A at the start and \\z at the end. Rails raises when ^ or $ is used without multiline: true; do not set that flag to silence the error.

**References:** [https://guides.rubyonrails.org/security.html#regular-expressions](https://guides.rubyonrails.org/security.html#regular-expressions)

## keryn.rails.ruby.weak-rsa-key-size

`warning` ruby ruby, rails

An RSA key shorter than 2048 bits is generated. Keys under 2048 bits can be factored with moderate resources. Use 2048 bits at least; 3072 for keys that outlive 2030.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another.

[CWE-326](https://keryn.dev/rules/?cwe=CWE-326) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** OpenSSL::PKey::RSA.generate(3072).

**References:** [https://docs.ruby-lang.org/en/master/OpenSSL/PKey/RSA.html](https://docs.ruby-lang.org/en/master/OpenSSL/PKey/RSA.html)

## keryn.rails.ruby.csrf-protection-disabled

`warning` ruby ruby, rails

CSRF token verification is skipped or turned off (skip\_before\_action :verify\_authenticity\_token, allow\_forgery\_protection = false). A third-party page can then submit state-changing requests with the visitor's cookies. Skip it only for token-authenticated API endpoints, and name the actions.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another.

[CWE-352](https://keryn.dev/rules/?cwe=CWE-352) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Delete the skip, or limit it to the API actions that authenticate with a bearer token (skip\_before\_action :verify\_authenticity\_token, only: :webhook). Keep protect\_from\_forgery with: :exception.

**References:** [https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html](https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html)

## keryn.rails.ruby.permit-privileged-attribute

`warning` ruby ruby, rails

Strong parameters permit a privileged attribute (admin, role, banned, account\_id, owner\_id, ...). Any client that can post the form can set it. Keep privileged attributes out of the permitted list and set them in code after an authorization check.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-915](https://keryn.dev/taxonomies/cwe/#cwe-915) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Remove the key from permit and assign it in the controller or service after checking the caller's role.

**References:** [https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters](https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters)

## keryn.rails.ruby.secret-key-literal

`warning` ruby ruby, rails

The session signing secret (secret\_token or secret\_key\_base) is a string literal in the source. Anyone with repository access can forge session cookies, and with the Marshal serializer, execute code. Read it from credentials or the environment.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [grants](https://keryn.dev/docs/edges/edge-kinds/#grants) authority is given or left unchecked.

[CWE-798](https://keryn.dev/taxonomies/cwe/#cwe-798-use-of-hard-coded-credentials) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Use Rails.application.credentials.secret\_key\_base or ENV.fetch("SECRET\_KEY\_BASE"). Rotate the value that was committed.

**References:** [https://guides.rubyonrails.org/security.html#custom-credentials](https://guides.rubyonrails.org/security.html#custom-credentials)
