kemalcr/kemal/kemal-middleware
Claude Code and agent skill by kemalcr/kemal
- community
Checking that the SKILL.md file is still in the repository.
Creating and using custom middleware in Kemal using modern `use` keyword and handler classes.
Install
npx vskill@latest install kemalcr/kemal/kemal-middlewareSKILL.md
The same SKILL.md was published earlier as kemalcr/kemal/kemal-middleware.
Kemal Middleware & Handlers
This skill provides expert guidance on creating and using custom middleware in Kemal, leveraging modern use registration and filter macros, strictly following patterns from `kemal-by-example/webhook-inbox`.
Version Notes
useregistration (global and path-scoped): since Kemal 1.10.only/excludewith a single method and exact paths: all supported versions.only/excludewith"*"methods and"/*"path globs: since Kemal 1.13.0 — do not use the glob syntax on 1.12.0 or earlier, where it matches every path (see the warning below).HEADfallback route-scoped dispatch: since Kemal 1.13.0. On Kemal 1.12.0 and earlier,HEADrequests falling back toGETroutes bypassedGET-scoped filters (before_get) andGET-scopedonly/excludemiddleware because dispatch checked the literal request method (HEAD) rather than the serving route method, creating an authentication bypass risk (GHSA-jf9q-62h3-924j). Kemal 1.13.0+ evaluates both the request method and the serving route's effective method.Kemal::Routerfilters registered with trailing/*match path subtrees: since Kemal 1.13.0 (on 1.12.0 and earlier, trailing*was treated as a literal path segment and silently failed to register/match routes).
Core Mandates
- Middleware Registration (`use` Keyword in Kemal 1.10+): Prefer
usefor clean global or path-scoped middleware registration:
```crystal # Path-scoped middleware (applies only to /api routes) use "/api", [CORSHandler.new, AuthHandler.new]
# Global middleware (applies to all routes) use CustomHandler.new ```
- Configuration-based Registration: Alternatively, use
Kemal.config.add_handler(CustomHandler.new). (The bare top-leveladd_handleris deprecated in favor ofuseandKemal.config.add_handler).
- Handlers: Create custom middleware by inheriting from
Kemal::Handler:
``crystal class MyHandler < Kemal::Handler def call(context) # Execution prior to next handler call_next(context) # Execution after next handler returns end end ``
- Selective Filtering in Handlers: Use
onlyorexcludemacros to restrict execution within the handler class. On Kemal 1.12.0 they support a single HTTP method and exact paths only:
```crystal class MyHandler < Kemal::Handler only %w[/admin], "POST"
def call(context) return call_next(context) unless only_match?(context) # Custom logic call_next(context) end end ```
On Kemal 1.13.0+, "*" matches all methods and paths ending in "/*" match a prefix:
``crystal # Kemal 1.13.0+: class MyHandler < Kemal::Handler # Matches all HTTP methods on /admin and sub-paths only %w[/admin/*], "*" # ... end ``
WARNING: Do not use "*" or "/*" globs on Kemal 1.12.0. The 1.12.0 route matcher treats * as a radix glob, so a rule like only %w[/admin/*], "*" matches every path on every method — an auth handler scoped this way locks the whole site. For middleware that should cover an entire path subtree on any version, prefer use "/admin", MyHandler.new instead.
- Legacy Registration (`add_handler`):
add_handler MyHandler.newremains supported for backward compatibility.
- Specialized Handlers (HMAC): For HMAC signatures,
require "kemal-hmac"and inherit fromKemal::Hmac::Handler:
```crystal class InboxHmacHandler < Kemal::Hmac::Handler only %w[/hooks/inbox], "POST"
def call(context) return call_next(context) unless only_match?(context) super end end ```
Patterns from Source Code
HMAC Handler (webhook-inbox/src/middleware/inbox_hmac_handler.cr)
Main Application Setup (webhook-inbox/src/webhook_inbox.cr)
Best Practices
- Use Keyword: Prefer
use "/path", Handler.newfor path-scoped middleware instead of checking path conditions inside route blocks. - Minimalist Design: Keep handlers focused on a single responsibility (e.g., CORS, logging, authorization).
- Execution Order: Order middleware intentionally (e.g., authentication handlers must run before route handlers that depend on auth context).
- HMAC Setup: For
Kemal::Hmac::Handler, pass key/secret maps as{ "client_id" => ["secret_key"] }.
When to Use
- When implementing cross-cutting concerns (authentication, CORS, security headers, logging).
- When protecting specific API routes using HMAC or token authorization.
- When organizing global or path-scoped request processing stacks.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryChecking.github/skills/kemal-middleware/SKILL.md
Popularity
- Repository stars
- 3,918
- Repository forks
- 205
- 7-day trend score
- 41
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/kemalcr/kemal/kemal-middleware)