hypershunt 1.1.0

HTTP server and reverse proxy
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
// OIDC back-channel logout: validates IdP-pushed logout_tokens against
// the cached JWKS, replay-protects with a TTL'd seen-jti set, and
// drops matching refresh entries.  Per OpenID Connect Back-Channel
// Logout 1.0.

use super::{OidcProvider, jwks_signature_verifies, parse_compact_jws};
use anyhow::{Context, Result, anyhow, bail};
use std::time::{Duration, Instant, SystemTime};

impl OidcProvider {
    /// True when back-channel logout is enabled in config.  Used by
    /// `listener.rs` to decide whether to register the endpoint at
    /// dispatch time.
    pub fn backchannel_logout_enabled(&self) -> bool {
        self.cfg.backchannel_logout_enabled
    }

    /// Path the IdP POSTs logout_tokens to.
    pub fn backchannel_logout_path(&self) -> &str {
        &self.cfg.backchannel_logout_path
    }

    /// Validate an IdP-pushed `logout_token` and tear down any
    /// matching server-side refresh entries.  Returns the number of
    /// entries removed (0 is a valid outcome -- the token was
    /// well-formed and signed but matched no live sessions).
    ///
    /// Spec: OpenID Connect Back-Channel Logout 1.0 §2.6 (request),
    /// §2.4 (logout_token), §2.6 (validation).
    pub fn apply_backchannel_logout(&self, token: &str) -> Result<usize> {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .context("clock before epoch")?
            .as_secs() as i64;

        // Parse the compact JWS without verifying yet.
        let parsed = parse_compact_jws(token)?;

        // Locate a matching JWK.  Empty JWKS or missing kid match
        // count as "verification failed"; some IdPs sign with a key
        // that has no `kid` set, so absent `kid` means "any of our
        // keys may match" -- we walk them in order.
        let jwks_guard = self.jwks.load_full();
        let jwks = jwks_guard
            .as_ref()
            .clone()
            .ok_or_else(|| anyhow!("JWKS not available; OIDC not ready"))?;
        if !jwks_signature_verifies(&jwks, &parsed) {
            bail!("logout_token signature did not match any JWKS key");
        }

        let payload = &parsed.payload;
        let (sub, sid, jti) = check_backchannel_claims(
            payload,
            &self.cfg.issuer,
            &self.cfg.client_id,
            now,
            self.cfg.backchannel_max_iat_skew_secs as i64,
        )?;
        if !self.record_jti(jti) {
            bail!("logout_token jti replay detected");
        }

        // Tear down matching refresh entries.  When sid is given,
        // remove only matching sessions; when only sub is given,
        // remove every entry for that user (RP-implementation
        // recommended by §2.6).
        let removed = {
            let mut map = self.refreshes.lock().expect("oidc refresh mutex");
            let before = map.len();
            match (sid, sub) {
                (Some(sid_val), _) => {
                    map.retain(|_, e| e.idp_sid.as_deref() != Some(sid_val));
                }
                (None, Some(sub_val)) => {
                    map.retain(|_, e| e.subject != sub_val);
                }
                _ => {}
            }
            before - map.len()
        };

        Ok(removed)
    }

    /// Record a `jti`; returns `false` when the value was already
    /// seen within the TTL window (i.e. this is a replay attempt).
    pub(super) fn record_jti(&self, jti: &str) -> bool {
        let ttl =
            Duration::from_secs(self.cfg.backchannel_jti_ttl_secs);
        let now = Instant::now();
        let mut map = self.seen_jtis.lock().expect("oidc jti mutex");
        // Opportunistic prune of long-stale entries so the map
        // doesn't grow unboundedly between scheduled evictions.
        map.retain(|_, expires_at| *expires_at > now);
        if map.contains_key(jti) {
            return false;
        }
        map.insert(jti.to_owned(), now + ttl);
        true
    }
}

/// Validate the claims in a back-channel logout token payload.
///
/// Returns `(sub, sid, jti)` on success.  Called after JWS signature
/// verification so the payload can be trusted.  Extracted as a pure
/// function so it can be unit-tested without a live JWKS.
fn check_backchannel_claims<'p>(
    payload: &'p serde_json::Value,
    issuer: &str,
    client_id: &str,
    now: i64,
    max_skew: i64,
) -> Result<(Option<&'p str>, Option<&'p str>, &'p str)> {
    let iss = payload
        .get("iss")
        .and_then(|v| v.as_str())
        .ok_or_else(|| anyhow!("logout_token missing iss"))?;
    if iss != issuer.trim_end_matches('/') && iss != issuer {
        bail!("logout_token iss does not match configured issuer");
    }
    let aud_ok = match payload.get("aud") {
        Some(serde_json::Value::String(s)) => s == client_id,
        Some(serde_json::Value::Array(items)) => {
            items.iter().any(|v| v.as_str() == Some(client_id))
        }
        _ => false,
    };
    if !aud_ok {
        bail!("logout_token aud does not include our client_id");
    }
    let iat = payload
        .get("iat")
        .and_then(|v| v.as_i64())
        .ok_or_else(|| anyhow!("logout_token missing iat"))?;
    if (now - iat).abs() > max_skew {
        bail!("logout_token iat outside accepted skew window");
    }
    // OIDC Back-Channel Logout 1.0 §2.5: the events claim MUST be
    // an object containing the back-channel-logout schema URL as
    // a key, with an empty object as its value.
    let events = payload
        .get("events")
        .and_then(|v| v.as_object())
        .ok_or_else(|| anyhow!("logout_token missing events object"))?;
    if !events
        .contains_key("http://schemas.openid.net/event/backchannel-logout")
    {
        bail!("logout_token events does not declare back-channel-logout");
    }
    // The spec explicitly forbids the `nonce` claim on logout_tokens.
    if payload.get("nonce").is_some() {
        bail!("logout_token must not carry a nonce claim");
    }
    let sub = payload.get("sub").and_then(|v| v.as_str());
    let sid = payload.get("sid").and_then(|v| v.as_str());
    if sub.is_none() && sid.is_none() {
        bail!("logout_token must contain at least one of sub or sid");
    }
    let jti = payload
        .get("jti")
        .and_then(|v| v.as_str())
        .ok_or_else(|| anyhow!("logout_token missing jti"))?;
    Ok((sub, sid, jti))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    const ISSUER: &str = "https://idp.example";
    const CLIENT_ID: &str = "my-client";

    fn valid_payload(now: i64) -> serde_json::Value {
        serde_json::json!({
            "iss": ISSUER,
            "aud": CLIENT_ID,
            "iat": now,
            "events": {
                "http://schemas.openid.net/event/backchannel-logout": {}
            },
            "sub": "user1",
            "jti": "tok1",
        })
    }

    // check_backchannel_claims ---------------------------------------

    #[test]
    fn claims_ok_with_sub_and_sid() {
        let mut p = valid_payload(0);
        p["sid"] = "sess1".into();
        let (sub, sid, jti) =
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .unwrap();
        assert_eq!(sub, Some("user1"));
        assert_eq!(sid, Some("sess1"));
        assert_eq!(jti, "tok1");
    }

    #[test]
    fn claims_ok_with_sub_only() {
        let p = valid_payload(0);
        let (sub, sid, _) =
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .unwrap();
        assert_eq!(sub, Some("user1"));
        assert!(sid.is_none());
    }

    #[test]
    fn claims_ok_with_sid_only() {
        let p = serde_json::json!({
            "iss": ISSUER, "aud": CLIENT_ID, "iat": 0_i64,
            "events": {
                "http://schemas.openid.net/event/backchannel-logout": {}
            },
            "sid": "sess1",
            "jti": "tok1",
        });
        let (sub, sid, _) =
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .unwrap();
        assert!(sub.is_none());
        assert_eq!(sid, Some("sess1"));
    }

    #[test]
    fn claims_iss_trailing_slash_accepted() {
        let p = valid_payload(0);
        // Issuer with trailing slash — both forms must match.
        check_backchannel_claims(
            &p,
            &format!("{ISSUER}/"),
            CLIENT_ID,
            0,
            120,
        )
        .unwrap();
    }

    #[test]
    fn claims_iss_mismatch_rejected() {
        let p = valid_payload(0);
        assert!(check_backchannel_claims(
            &p,
            "https://other.example",
            CLIENT_ID,
            0,
            120
        )
        .is_err());
    }

    #[test]
    fn claims_missing_iss_rejected() {
        let mut p = valid_payload(0);
        p.as_object_mut().unwrap().remove("iss");
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_aud_string_match() {
        check_backchannel_claims(
            &valid_payload(0),
            ISSUER,
            CLIENT_ID,
            0,
            120,
        )
        .unwrap();
    }

    #[test]
    fn claims_aud_array_match() {
        let mut p = valid_payload(0);
        p["aud"] = serde_json::json!(["other-client", CLIENT_ID]);
        check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120).unwrap();
    }

    #[test]
    fn claims_aud_mismatch_rejected() {
        let mut p = valid_payload(0);
        p["aud"] = "wrong-client".into();
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_aud_array_mismatch_rejected() {
        let mut p = valid_payload(0);
        p["aud"] = serde_json::json!(["a", "b"]);
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_iat_within_skew_accepted() {
        // iat exactly at boundary (skew=10, delta=10) must be accepted.
        let p = valid_payload(100);
        check_backchannel_claims(&p, ISSUER, CLIENT_ID, 110, 10).unwrap();
        check_backchannel_claims(&p, ISSUER, CLIENT_ID, 90, 10).unwrap();
    }

    #[test]
    fn claims_iat_outside_skew_rejected() {
        let p = valid_payload(0);
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 200, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_missing_iat_rejected() {
        let mut p = valid_payload(0);
        p.as_object_mut().unwrap().remove("iat");
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_missing_events_rejected() {
        let mut p = valid_payload(0);
        p.as_object_mut().unwrap().remove("events");
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_events_missing_required_key_rejected() {
        let mut p = valid_payload(0);
        p["events"] = serde_json::json!({"other:event": {}});
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_nonce_present_rejected() {
        let mut p = valid_payload(0);
        p["nonce"] = "n".into();
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_neither_sub_nor_sid_rejected() {
        let p = serde_json::json!({
            "iss": ISSUER, "aud": CLIENT_ID, "iat": 0_i64,
            "events": {
                "http://schemas.openid.net/event/backchannel-logout": {}
            },
            "jti": "tok1",
        });
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    #[test]
    fn claims_missing_jti_rejected() {
        let mut p = valid_payload(0);
        p.as_object_mut().unwrap().remove("jti");
        assert!(
            check_backchannel_claims(&p, ISSUER, CLIENT_ID, 0, 120)
                .is_err()
        );
    }

    // record_jti -----------------------------------------------------

    #[test]
    fn record_jti_new_returns_true() {
        let p = crate::oidc::tests::provider_for_store(
            Duration::from_secs(300),
        );
        assert!(p.record_jti("unique-jti-1"));
    }

    #[test]
    fn record_jti_replay_returns_false() {
        let p = crate::oidc::tests::provider_for_store(
            Duration::from_secs(300),
        );
        assert!(p.record_jti("jti-replay"));
        assert!(!p.record_jti("jti-replay"));
    }

    #[test]
    fn record_jti_different_jtis_all_accepted() {
        let p = crate::oidc::tests::provider_for_store(
            Duration::from_secs(300),
        );
        for i in 0..10 {
            assert!(p.record_jti(&format!("jti-{i}")));
        }
    }
}