sinatra-security

Secure Sinatra applications against CSRF, XSS, and SQL injection.

8|2|Updated Oct 17, 2025
One-click install
npx skills add https://github.com/geoffjay/claude-plugins --skill sinatra-security
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sinatra-security
Source: https://github.com/geoffjay/claude-plugins/tree/main/plugins/ruby-sinatra-advanced/skills/sinatra-security
Command: npx skills add https://github.com/geoffjay/claude-plugins --skill sinatra-security

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires rack-protection, bcrypt, jwt, sanitize, rack-attack, and includes assets (resource) and references (resource) components.

What problem does it solve?

Sinatra applications, like any web application, are vulnerable to common security threats such as CSRF, XSS, and SQL injection if not properly secured. This Skill provides essential best practices and implementation details to protect your application from these attacks.

Core Features & Use Cases

  • CSRF Protection: Implement Rack::Protection and manual token generation for robust cross-site request forgery defense.
  • XSS Prevention: Strategies for template escaping, secure JSON encoding, and Content Security Policy to mitigate cross-site scripting attacks.
  • SQL Injection Prevention: Utilize parameterized queries and strong input validation to guard against database injection attacks.
  • Authentication & Authorization: Patterns for password, token, and API key authentication, plus role-based and permission-based authorization for secure access control.
  • Use Case: A developer is building a new Sinatra API and needs to ensure it's secure against common web attacks. This Skill guides them through implementing CSRF tokens, sanitizing user input, using secure authentication methods, and setting up robust authorization checks, ensuring the application is protected from day one.

Quick Start

Enable Rack::Protection for CSRF and other protections

use Rack::Protection

Secure sessions with a strong secret

use Rack::Session::Cookie, secret: ENV['SESSION_SECRET'], same_site: :strict, httponly: true, secure: production?

Parameterized query to prevent SQL Injection

DB["SELECT * FROM users WHERE email = ?", email]

Frequently Asked Questions about sinatra-security

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent CSRF attacks in Sinatra applications?

CSRF protection in Sinatra uses Rack::Protection middleware to generate and validate tokens on state-changing requests. Enable it with `use Rack::Protection` and include token validation in forms and API endpoints to block forged requests from unauthorized origins.

What's the best way to handle authentication and authorization in a Sinatra API?

Sinatra APIs benefit from JWT or API key authentication combined with role-based authorization checks. Hash passwords with bcrypt, validate tokens on each request, and enforce permission checks before granting access to protected resources.

How do I prevent SQL injection in Sinatra database queries?

SQL injection prevention requires parameterized queries where user input is passed as separate arguments rather than concatenated strings. Use your database library's prepared statement syntax to ensure input is safely escaped.

Can I use Rack::Protection to defend against XSS attacks?

Rack::Protection helps mitigate XSS through Content Security Policy headers and other defensive measures, but should be combined with template escaping and safe JSON encoding to fully prevent cross-site scripting.

What input validation strategies work best for Sinatra applications?

Input validation in Sinatra combines whitelisting acceptable formats, sanitizing user-supplied content with libraries like sanitize, and rejecting malformed data before it reaches your business logic or database.

How do I configure secure session handling in Sinatra?

Secure sessions use Rack::Session::Cookie with a strong secret from environment variables, httponly flag to prevent JavaScript access, secure flag for HTTPS-only transmission, and same_site policy to block cross-site cookie leakage.