gritshield 0.1.1

A security-first, high-performance micro-framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
## What is GritShield?


GritShield is an **async-first, security-hardened** web framework for Rust that eliminates the majority of OWASP Top 10 vulnerabilities by design.

## Key Features


- **Native Default Asynchrony** – Built entirely from the ground up on non-blocking `async/await` design patterns, leveraging a multi-threaded Tokio runtime context to sustain massive concurrent execution throughput without thread starvation.
- **XSS-Safe Templating** – Untrusted data cannot reach HTML without explicit sanitisation.
- **CSRF Protection** – Automatic token validation for state-changing requests.
- **Signed Cookies** – Cryptographic HMAC-SHA256 signatures prevent client-side cookie tampering.
- **Session Management** – Thread-safe, in-memory store protected by asynchronous synchronization locks with automatic expiration hooks.
- **JWT Support** – Stateless authentication via securely managed HS256 tokens.
- **Rate Limiting** – Sliding-window rate tracking per IP address, backed by low-overhead atomic counters.
- **IP Blacklisting** – Instantly drops connection sockets for malicious clients directly at the early middleware layer.
- **File-Based Routing** – Auto-discovery of structural endpoint handlers mapped straight to your local filesystem hierarchy.

## Quick Navigation


- [Getting Started]/docs/getting-started - First steps with GritShield
- [Architecture Overview]/docs/architecture/overview - Understand the kernel design
- [Security Guide]/docs/security - Learn about security features
- [Routing Guide]/docs/routing - Master the routing system

# GritShield Documentation


## Installation


Add to your `Cargo.toml`:

```toml
[dependencies]
gritshield = "0.1.0"
tokio = { version = "1", features = ["full"] }
```

---

## Quick Start – Hello World


Create `src/main.rs`:

```rust
use gritshield::prelude::*;

#[get("/")]

async fn hello(_: RequestContext) -> &'static str {
    "Hello, GritShield!"
}

#[tokio::main]

async fn main() {
    let router = Router::new().mount_logger();

    run_server("127.0.0.1", "8080", router, false).await;
}
```

Run with `cargo run` and open `http://localhost:8080`.

---

## Core Concepts


## 1. Request Context


There is no more importing this::that or constantly checking the docs just to find out where a feature lives.

Every handler receives a `RequestContext` that holds the HTTP request, dynamic parameters, session, cookies, parsed form data, JSON body, and security helpers all in one place.

```rust
async fn profile(ctx: RequestContext) -> String {
    let user_id = ctx.params.get("id").unwrap().as_str();
    let session_user = ctx.get_session_data("user_id").unwrap_or_default();

    format!("Viewing profile of {}", user_id)
}
```

---

## 2. Routing


- ### a) Attribute Macros

  ```rust
  #[get("/users/:id")]
  async fn get_user(ctx: RequestContext) -> Response {
      let id = ctx.params.get("id").unwrap().as_str();
      Response::new(Sanitizer::trust(format!("User {}", id)))
  }

  #[post("/users")]
  async fn create_user(ctx: RequestContext) -> Result<Response, ShieldError> {
      let new_user: CreateUserDto = ctx.json()?;
      // ...
      Ok(Response::redirect(303, "/users"))
  }
  ```

  Supported methods:
  - `#[get]`
  - `#[post]`
  - `#[put]`
  - `#[patch]`
  - `#[delete]`

- ### b) Manual Registration

  ```rust
  router.add_route(HttpMethod::GET, "/health", |_: RequestContext| async move { "OK" });

  ```

- ### c) File‑Based Routing (Next.js style)

  Place your handlers in `src/pages/`:
  - `src/pages/index.rs` → route `/`
  - `src/pages/users/[id].rs` → route `/users/:id`
  - `src/pages/api/[..path].rs` → route `/api/**` (catch‑all)

  Inside the file, use the `register_page!` macro:

  ```rust
  use gritshield::prelude::*;

  register_page!(HttpMethod::GET, |_| async { "Hello from file route" });
  ```

  GritShield automatically discovers `.rs` files under `src/pages` and mounts them.

---

## 3. Middleware Stack


- Middleware implements the `Middleware` trait. They run in the order they are added.

  ```rust
  pub trait Middleware: Send + Sync {
      fn execute(&self, ctx: &mut RequestContext) -> MiddlewareResult;
  }
  ```

  ### Built‑in middleware
  - `LoggerMiddleware` – logs method, path, status, duration, authentication state.
  - `RateLimitMiddleware` – sliding‑window rate limiting.
  - `IPBlacklistMiddleware` – blocks requests from configured IP addresses.
  - `AuthMiddleware` – session + JWT + CSRF gatekeeper.

  ### Adding custom middleware

  ```rust
  struct MyMiddleware;

  impl Middleware for MyMiddleware {
      fn execute(&self, ctx: &mut RequestContext) -> MiddlewareResult {
          // modify context or reject
          MiddlewareResult::Next(None)
      }
  }

  router = router.add_middleware(MyMiddleware);
  ```

---

## 4. Responses


Handlers can return any type that implements `IntoResponse`:

- `Response` – full control over status, headers, cookies.
- `&'static str / String` – automatically wrapped as HTML.
- `Result<T, ShieldError>` – maps errors to a safe error page.
- `SafeHtml` – pre‑sanitised HTML content.

### Constructors


```rust
Response::new(200, Sanitizer::trust("<h1>Welcome</h1>"))
Response::new(200, );
Response::redirect(303, "/login");
Response::json(200, &my_struct);
Response::static_file("static/style.css");
```

---

## 5. Static Files


Place files in the `static/` folder.

Serve them with:

```rust
Response::static_file("static/logo.png")
```

Serve static files in one go

```rust
#[get("/static/:*path")]
async fn static_assets(ctx: RequestContext) -> Response {
    let path = ctx.params.get("*path").unwrap().as_str();


    Response::static_file(&format!("/static/{}", path).as_str())
}
```

Directory traversal is prevented automatically.

---

## AuthMiddleware – Session vs JWT


By using AuthMiddleware you have a full authentication system that exposes only `/login`&`/register` routes, set signed `hmac cookies`, generate a new `CSRF` token immediately, `logs out` user automatically and redirects unauthenticated users to `/login`

### Session mode (default)


```rust
let auth = AuthMiddleware::new_session(
    vec!["/login".to_string(), "/register".to_string()],
    Some("/login")
);

router = router.add_middleware(auth);
```

Features:

- Creates a signed `GSESSION_ID` cookie.
- Stores user data in an in‑memory `SessionStore`.
- Use `ctx.login_user_id("123")` to authenticate.
- `ctx.is_user_authenticated()` checks login state (expects to set `user_id` in session once user login).

### JWT stateless mode


```rust
let jwt_handler = JwtHandler::new(&std::env::var("JWT_SECRET").unwrap());

let auth = AuthMiddleware::new_jwt(
    jwt_handler,
    vec!["/public".into()],
    None
);
```

Features:

- Expects `Authorization: Bearer <token>`.
- Stateless authentication.
- Claims available through `ctx.claims`.

---

## CSRF Protection


Enabled automatically in session mode.

HTML forms must include csrf_token value:

Retrieve the token:

```rust
let token = ctx.get_csrf_token();

// using maud
render!(ctx, "title", html! {
    input type="hidden" id="global-csrf-token" value=(csrf_token);
})
```

---

## Cookies – Signed & Secure


```rust
// Read a signed cookie
if let Some(val) = ctx.get_signed_cookie("user_pref") {
    // ...
}

// Set a signed cookie
let cookie = Cookie::new("pref", "dark_mode")
    .set_secure(cfg!(production))
    .set_same_site(SameSite::Lax);

ctx.set_signed_cookie(cookie);

// Delete a cookie
ctx.remove_cookie("pref");
```

Unsigned cookies are also available:

```rust
ctx.get_cookie()
ctx.set_cookie()
```

---

## XSS Prevention


All user input arrives as `UntrustedString`.

To display safely:

```rust
let safe_html = Sanitizer::encode(untrusted_string);
```

To return trusted HTML:

```rust
Sanitizer::trust("...")
```

Only use for strings you fully control.

---

## Rate Limiting


```rust
let limiter = RateLimiter::new(50, Duration::from_secs(60));

let rate_middleware = RateLimitMiddleware { limiter };

router = router.add_middleware(rate_middleware);
```

---

## IP Blacklisting


```rust
let blacklist = IPBlacklistMiddleware::new(vec![
    "192.168.1.100",
    "10.0.0.5"
]);

router = router.add_middleware(blacklist);
```

---

# Database Integration


GritShield integrates with SeaORM.

```rust
let db = sea_orm::Database::connect(
    "postgres://user:pass@localhost/db"
).await.unwrap();

let router = Router::new().mound_db(Arc::new(db));
```

Access the connection inside handlers:

```rust
async fn list_users(ctx: RequestContext) -> String {
    let db = ctx.db.as_ref().unwrap();

    // use db
}
```

---

# Request Data Parsing


Supported formats:

- JSON → `ctx.json::<T>()`
- Form URL‑encoded → `ctx.form.fields`
- Multipart uploads → `ctx.form.files`
- Query parameters → `ctx.query`
- Raw body → `ctx.raw_body`

### Example file upload


```rust
#[post("/upload")]

async fn upload(ctx: RequestContext) -> String {
    if let Some(file) = ctx.form.files.get("avatar") {
        std::fs::write(
            format!("uploads/{}", file.filename),
            &file.data
        ).unwrap();

        "Uploaded!"
    } else {
        "No file".into()
    }
}
```

---

# Templating


Place templates in the `templates/` folder.

```rust
use gritshield::html::TemplateEngine;

// Precompile templates, this should be used once, then get template will be cashed
TemplateEngine::precompile_all("templates").unwrap();

// Render template
let html = TemplateEngine::get("home.html");

Response::new(200, Sanitizer::trust(&html))
```

You can integrate with templating crates like Handlebars.

---

# Configuration & Environment


Environment variables are loaded from `.env` or the system.

### Important variables


- `APP_ENV`
- `JWT_SECRET`

### Access variables


```rust
let db_url = gritshield::core::env::get_env(
    "DATABASE_URL",
    "sqlite::memory:"
);
```

---

# Telemetry & Logging


Enable request logging:

```rust
router = Router::new().mount_logger();
```

Example log output:

```text
🗲  [200] GET /dashboard ➔  Size: 2.34 KB | Time: 12ms | Auth: 🍪 Session ID: a1b2c3d4
```

Supports custom telemetry hooks and metrics collection.

---

# Hot Reload (Development)


```rust
run_server("127.0.0.1", "8080", router, true).await;
```

Automatically rebuilds and reloads on source changes.

---

# Error Handling


The framework catches errors globally.

### Development mode


- Detailed technical information shown.

### Production mode


- Generic user‑safe error page.
- Errors logged server‑side.

### Custom error handler


```rust
fn custom_error_handler(
    ctx: RequestContext,
    err: ShieldError
) -> BoxFuture<'static, Response> {

    Box::pin(async move {
        Response::new(500, Sanitizer::trust("Oops"))
    })
}

router.global_error_handler.handler = Some(custom_error_handler);
```

---

# Deployment


### Recommended production setup


- Set `APP_ENV=production`
- Use strong `JWT_SECRET`
- Use reverse proxy (Nginx/Caddy)
- Compile with `--release`

### Example Dockerfile


```dockerfile
FROM rust:1.70 as builder

WORKDIR /app

COPY . .

RUN cargo build --release

FROM debian:bookworm-slim

COPY --from=builder /app/target/release/myapp /usr/local/bin/

ENV APP_ENV=production

CMD ["myapp"]
```

---

# Testing Helpers


```rust
#[tokio::test]

async fn test_handler() {
    let ctx = RequestContext::new();

    let response = my_handler(ctx).await;

    assert_eq!(response.status, 200);
}
```

---

# API Reference (Selected)


| Type / Function                             | Description                         |
| ------------------------------------------- | ----------------------------------- |
| `Router::new()`                             | Creates empty router.               |
| `router.add_route(method, path, handler)`   | Manual route registration.          |
| `router.add_middleware(M)`                  | Appends middleware to the pipeline. |
| `RequestContext::get_signed_cookie(name)`   | Reads signed cookie.                |
| `RequestContext::set_signed_cookie(cookie)` | Writes signed cookie.               |
| `RequestContext::remove_cookie(name)`       | Deletes cookie.                     |
| `RequestContext::login_user_id(id)`         | Sets authenticated user ID.         |
| `RequestContext::get_csrf_token()`          | Returns CSRF token.                 |
| `Response::redirect(status, location)`      | Redirect response.                  |
| `Response::json(status, &data)`             | Serialises JSON response.           |
| `Sanitizer::encode(UntrustedString)`        | Escapes HTML safely.                |
| `Sanitizer::trust(str)`                     | Creates trusted HTML.               |
| `RateLimiter::new(max, window)`             | Creates rate limiter.               |
| `JwtHandler::sign(claims)`                  | Signs JWT token.                    |
| `JwtHandler::verify(token)`                 | Verifies JWT token.                 |
| `TemplateEngine::get(name)`                 | Returns template content.           |

---

# Contributing


GritShield is built with security as the highest priority.

Contributions, bug reports, and security advisories are welcome.

Please open an issue before submitting a PR.

---

# License


Apache‑2.0.