gatehouse 0.6.0

An in-process authorization engine for Rust with composable policies and request-scoped fact loading.
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
use actix_web::{http::StatusCode, test, web, App};
use uuid::Uuid;

mod actix_example {
    #![allow(dead_code)]
    include!(concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/examples/actix_web.rs"
    ));
}

macro_rules! init_actix_app {
    () => {{
        let state = web::Data::new(actix_example::AppState::demo());
        test::init_service(
            App::new()
                .app_data(state.clone())
                .route("/posts", web::get().to(actix_example::list_posts))
                .route("/posts/{id}", web::get().to(actix_example::view_post))
                .route("/posts/{id}", web::put().to(actix_example::edit_post))
                .route(
                    "/posts/{id}/publish",
                    web::post().to(actix_example::publish_post),
                ),
        )
    }};
}

// Demo fixtures (see `AppState::demo` in the example).
fn author_id() -> Uuid {
    Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap()
}

fn collaborator_id() -> Uuid {
    Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap()
}

fn draft_post_id() -> Uuid {
    Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap()
}

fn published_post_id() -> Uuid {
    Uuid::parse_str("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap()
}

fn locked_post_id() -> Uuid {
    Uuid::parse_str("cccccccc-cccc-cccc-cccc-cccccccccccc").unwrap()
}

#[actix_web::test]
async fn edit_post_allows_author() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::put()
        .uri(&format!("/posts/{}", draft_post_id()))
        .insert_header(("x-user-id", author_id().to_string()))
        .insert_header(("x-roles", "author"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_web::test]
async fn edit_post_allows_collaborator_via_relationship() {
    // The collaborator is not the author, but holds an `editor` relationship on
    // the demo draft, loaded through the request-scoped session.
    let app = init_actix_app!().await;

    let req = test::TestRequest::put()
        .uri(&format!("/posts/{}", draft_post_id()))
        .insert_header(("x-user-id", collaborator_id().to_string()))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_web::test]
async fn collaborator_edit_is_bounded_by_the_author_draft_window() {
    // The collaborator holds an editor relationship on every demo post, so
    // only the draft window can refuse these edits. A collaborator must never
    // be able to edit a post its author cannot.
    let app = init_actix_app!().await;
    let old_draft = Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap();

    for post_id in [published_post_id(), locked_post_id(), old_draft] {
        for user_id in [author_id(), collaborator_id()] {
            let req = test::TestRequest::put()
                .uri(&format!("/posts/{post_id}"))
                .insert_header(("x-user-id", user_id.to_string()))
                .to_request();
            let resp = test::call_service(&app, req).await;
            assert_eq!(
                resp.status(),
                StatusCode::FORBIDDEN,
                "user {user_id} editing post {post_id}"
            );
        }
    }
}

#[actix_web::test]
async fn edit_post_denies_non_author() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::put()
        .uri(&format!("/posts/{}", draft_post_id()))
        .insert_header(("x-user-id", Uuid::new_v4().to_string()))
        .insert_header(("x-roles", "author"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}

#[actix_web::test]
async fn edit_post_denies_locked_post() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::put()
        .uri(&format!("/posts/{}", locked_post_id()))
        .insert_header(("x-user-id", author_id().to_string()))
        .insert_header(("x-roles", "author"))
        .insert_header(("x-post-locked", "false"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}

#[actix_web::test]
async fn publish_post_allows_editor() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::post()
        .uri(&format!("/posts/{}/publish", draft_post_id()))
        .insert_header(("x-user-id", Uuid::new_v4().to_string()))
        .insert_header(("x-roles", "editor"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_web::test]
async fn publish_post_denies_non_editor() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::post()
        .uri(&format!("/posts/{}/publish", draft_post_id()))
        .insert_header(("x-user-id", Uuid::new_v4().to_string()))
        .insert_header(("x-roles", "author"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}

#[actix_web::test]
async fn publish_post_denies_locked_post() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::post()
        .uri(&format!("/posts/{}/publish", locked_post_id()))
        .insert_header(("x-user-id", Uuid::new_v4().to_string()))
        .insert_header(("x-roles", "editor"))
        .insert_header(("x-post-locked", "false"))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}

#[actix_web::test]
async fn view_post_allows_published_post() {
    let app = init_actix_app!().await;

    // The published demo post is viewable by anyone.
    let req = test::TestRequest::get()
        .uri(&format!("/posts/{}", published_post_id()))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_web::test]
async fn view_post_denies_unpublished_anonymous() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::get()
        .uri(&format!("/posts/{}", draft_post_id()))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::FORBIDDEN);
}

#[actix_web::test]
async fn view_post_allows_author_on_unpublished_post() {
    let app = init_actix_app!().await;

    let req = test::TestRequest::get()
        .uri(&format!("/posts/{}", draft_post_id()))
        .insert_header(("x-user-id", author_id().to_string()))
        .to_request();

    let resp = test::call_service(&app, req).await;
    assert_eq!(resp.status(), StatusCode::OK);
}

#[actix_web::test]
async fn list_posts_filters_by_relationship() {
    let app = init_actix_app!().await;

    // The collaborator sees both posts (editor relationship on each).
    let req = test::TestRequest::get()
        .uri("/posts")
        .insert_header(("x-user-id", collaborator_id().to_string()))
        .to_request();
    let body = test::call_and_read_body(&app, req).await;
    let body = String::from_utf8(body.to_vec()).unwrap();
    assert!(
        body.contains("draft roadmap"),
        "collaborator should see the draft: {body}"
    );
    assert!(body.contains("published announcement"));

    // An anonymous caller (no x-user-id; the extractor falls back to the nil
    // UUID) sees only the published post.
    let req = test::TestRequest::get().uri("/posts").to_request();
    let body = test::call_and_read_body(&app, req).await;
    let body = String::from_utf8(body.to_vec()).unwrap();
    assert!(
        !body.contains("draft roadmap"),
        "anonymous must not see the draft: {body}"
    );
    assert!(body.contains("published announcement"));
}

struct UnavailableRelationships;

#[async_trait::async_trait]
impl<K: gatehouse::FactKey<Value = bool>> gatehouse::FactSource<K> for UnavailableRelationships {
    async fn load_many(&self, keys: &[K]) -> Vec<gatehouse::FactLoadResult<bool>> {
        keys.iter()
            .map(|_| {
                gatehouse::FactLoadResult::Error(gatehouse::FactLoadError::backend_message(
                    "injected outage",
                ))
            })
            .collect()
    }
}

#[actix_web::test]
async fn authorization_outage_returns_503_for_single_and_list_but_admin_still_grants() {
    let state = actix_example::AppState::demo().with_relationship_source(UnavailableRelationships);
    let app = test::init_service(
        App::new()
            .app_data(web::Data::new(state))
            .route("/posts", web::get().to(actix_example::list_posts))
            .route("/posts/{id}", web::get().to(actix_example::view_post)),
    )
    .await;
    for uri in ["/posts", "/posts/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"] {
        let request = test::TestRequest::get().uri(uri).to_request();
        let response = test::call_service(&app, request).await;
        assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
        let body = test::read_body(response).await;
        assert!(!String::from_utf8_lossy(&body).contains("injected outage"));
        let request = test::TestRequest::get()
            .uri(uri)
            .insert_header(("x-roles", "admin"))
            .to_request();
        assert_eq!(
            test::call_service(&app, request).await.status(),
            StatusCode::OK
        );
    }
}

#[actix_web::test]
async fn resource_headers_cannot_change_authorization() {
    let app = init_actix_app!().await;
    for (method, post_id, user_id, expected) in [
        (
            actix_web::http::Method::GET,
            draft_post_id(),
            Uuid::nil(),
            StatusCode::FORBIDDEN,
        ),
        (
            actix_web::http::Method::GET,
            published_post_id(),
            Uuid::nil(),
            StatusCode::OK,
        ),
        (
            actix_web::http::Method::PUT,
            draft_post_id(),
            author_id(),
            StatusCode::OK,
        ),
        (
            actix_web::http::Method::PUT,
            locked_post_id(),
            collaborator_id(),
            StatusCode::FORBIDDEN,
        ),
        (
            actix_web::http::Method::PUT,
            Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap(),
            author_id(),
            StatusCode::FORBIDDEN,
        ),
    ] {
        for spoof in [
            None,
            Some(("true", "false", "0")),
            Some(("false", "true", "999999")),
        ] {
            let request = test::TestRequest::default()
                .method(method.clone())
                .uri(&format!("/posts/{post_id}"))
                .insert_header(("x-user-id", user_id.to_string()));
            let request = if let Some((published, locked, age_days)) = spoof {
                request
                    .insert_header(("x-post-published", published))
                    .insert_header(("x-post-locked", locked))
                    .insert_header(("x-post-age-days", age_days))
            } else {
                request
            };
            let response = test::call_service(&app, request.to_request()).await;
            assert_eq!(
                response.status(),
                expected,
                "{method} {post_id}, spoof={spoof:?}"
            );
            if expected == StatusCode::FORBIDDEN {
                assert_eq!(test::read_body(response).await.as_ref(), b"Access denied");
            }
        }
    }
}

#[actix_web::test]
async fn unknown_posts_are_not_manufactured_even_for_admins() {
    let app = init_actix_app!().await;
    for (method, suffix) in [
        (actix_web::http::Method::GET, ""),
        (actix_web::http::Method::PUT, ""),
        (actix_web::http::Method::POST, "/publish"),
    ] {
        let request = test::TestRequest::default()
            .method(method)
            .uri(&format!("/posts/{}{suffix}", Uuid::nil()))
            .insert_header(("x-roles", "admin"))
            .insert_header(("x-post-published", "true"))
            .to_request();
        assert_eq!(
            test::call_service(&app, request).await.status(),
            StatusCode::NOT_FOUND
        );
    }
}

#[actix_web::test]
async fn list_and_point_reads_agree_on_stored_resources() {
    let app = init_actix_app!().await;
    for user_id in [Uuid::nil(), author_id(), collaborator_id()] {
        let request = test::TestRequest::get()
            .uri("/posts")
            .insert_header(("x-user-id", user_id.to_string()))
            .to_request();
        let posts: serde_json::Value = test::call_and_read_body_json(&app, request).await;
        for post_id in [
            draft_post_id(),
            published_post_id(),
            locked_post_id(),
            Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd").unwrap(),
        ] {
            let listed = posts
                .as_array()
                .unwrap()
                .iter()
                .any(|post| post["id"] == post_id.to_string());
            let request = test::TestRequest::get()
                .uri(&format!("/posts/{post_id}"))
                .insert_header(("x-user-id", user_id.to_string()))
                .to_request();
            assert_eq!(
                test::call_service(&app, request).await.status() == StatusCode::OK,
                listed
            );
        }
    }
}

#[actix_web::test]
async fn write_routes_explicitly_demonstrate_authorization_only() {
    let app = init_actix_app!().await;
    for (method, suffix, message) in [
        (
            actix_web::http::Method::PUT,
            "",
            "Edit authorized; no changes made",
        ),
        (
            actix_web::http::Method::POST,
            "/publish",
            "Publish authorized; no changes made",
        ),
    ] {
        let request = test::TestRequest::default()
            .method(method)
            .uri(&format!("/posts/{}{suffix}", draft_post_id()))
            .insert_header(("x-roles", "admin"))
            .to_request();
        let response = test::call_service(&app, request).await;
        assert_eq!(response.status(), StatusCode::OK);
        assert_eq!(test::read_body(response).await.as_ref(), message.as_bytes());
    }
    let request = test::TestRequest::get()
        .uri(&format!("/posts/{}", draft_post_id()))
        .to_request();
    assert_eq!(
        test::call_service(&app, request).await.status(),
        StatusCode::FORBIDDEN
    );
}

#[actix_web::test]
async fn author_edit_window_rejects_future_and_exact_thirty_day_ages() {
    use std::time::{Duration, SystemTime};

    struct NoRelationships;

    #[async_trait::async_trait]
    impl<K: gatehouse::FactKey<Value = bool>> gatehouse::FactSource<K> for NoRelationships {
        async fn load_many(&self, keys: &[K]) -> Vec<gatehouse::FactLoadResult<bool>> {
            vec![gatehouse::FactLoadResult::Found(false); keys.len()]
        }
    }

    let session = gatehouse::FactRegistry::builder()
        .with::<gatehouse::RelationshipQuery<Uuid, Uuid, actix_example::Relation>, _>(
            NoRelationships,
        )
        .build()
        .session();
    let current_time = SystemTime::UNIX_EPOCH + Duration::from_secs(60 * 24 * 60 * 60);
    let boundary = current_time - Duration::from_secs(30 * 24 * 60 * 60);
    let tick = Duration::from_nanos(1);
    let checker = actix_example::build_permission_checker();
    let user = actix_example::User {
        id: author_id(),
        roles: vec![],
    };
    let context = actix_example::RequestContext { current_time };
    for (created_at, allowed) in [
        (current_time + tick, false),
        (boundary + tick, true),
        (boundary, false),
        (boundary - tick, false),
    ] {
        let post = actix_example::BlogPost {
            id: draft_post_id(),
            title: "draft".into(),
            author_id: user.id,
            locked: false,
            published_at: None,
            created_at,
        };
        let decision = checker
            .bind(&session, &user, &actix_example::Action::Edit, &context)
            .check(&post)
            .await;
        assert_eq!(decision.is_granted(), allowed, "created_at={created_at:?}");
        if !allowed {
            assert!(
                matches!(decision, gatehouse::AccessEvaluation::Denied { .. }),
                "created_at={created_at:?}: {decision:?}"
            );
        }
    }
}