mkit-server 0.5.0

Runtime-agnostic core of the mkit server: operation model, errors, runtime and telemetry vocabulary
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
//! Owner-signed write grant verification.

use mkit_attest::grant::{
    AcceptedSchemes, Capability, GrantError, GrantRequest, RelyingParty, RepositoryIdentity,
    VerifiedEpoch, VerifiedGrant, VerifierConfig, verify_epoch_statement, verify_grant_owner,
};

use crate::error::ServerError;
use crate::op::Operation;

/// Grant verification settings for a Multi/Owner deployment.
#[derive(Clone, Debug)]
pub struct GrantConfig {
    verifier: VerifierConfig,
}

impl GrantConfig {
    /// Build production settings. Loopback audiences and relying parties are refused.
    ///
    /// # Errors
    /// `invalid_argument` if the verifier settings are invalid.
    pub fn new(
        audience: &str,
        schemes: AcceptedSchemes,
        relying_parties: Vec<RelyingParty>,
    ) -> Result<Self, ServerError> {
        VerifierConfig::new(audience, schemes, relying_parties)
            .map(|verifier| Self { verifier })
            .map_err(config_error)
    }

    /// Build settings for tests and local development, allowing loopback origins.
    ///
    /// # Errors
    /// `invalid_argument` if the verifier settings are invalid.
    pub fn new_allowing_loopback(
        audience: &str,
        schemes: AcceptedSchemes,
        relying_parties: Vec<RelyingParty>,
    ) -> Result<Self, ServerError> {
        VerifierConfig::new_allowing_loopback(audience, schemes, relying_parties)
            .map(|verifier| Self { verifier })
            .map_err(config_error)
    }

    /// The byte-exact auth v2 audience this verifier accepts.
    #[must_use]
    pub fn audience(&self) -> &str {
        self.verifier.audience()
    }

    /// Advertised schemes in SPEC-WRITE-GRANTS ยง4 order.
    #[must_use]
    pub fn schemes(&self) -> AcceptedSchemes {
        self.verifier.schemes()
    }

    /// The owner-scheme verifier, for statement kinds that are not write
    /// grants (visibility statements, WP-2.9).
    pub(crate) fn verifier(&self) -> &VerifierConfig {
        &self.verifier
    }

    pub(crate) fn verify(
        &self,
        header: &str,
        op: &Operation,
    ) -> Result<VerifiedGrant, ServerError> {
        let auth = op.auth.as_ref().ok_or_else(|| {
            ServerError::permission_denied("write grant rejected: missing auth v2 signer")
        })?;
        let now_ms = op.business_now_ms.ok_or_else(|| {
            ServerError::permission_denied("write grant rejected: missing business clock")
        })?;
        let identity = format!("{}/{}", op.repo.namespace.as_str(), op.repo.name.as_str());
        let repository = RepositoryIdentity::parse(&identity)
            .map_err(|_| ServerError::permission_denied("write grant rejected: repository"))?;
        let owner = verify_grant_owner(&self.verifier, header).map_err(rejected)?;
        owner
            .check(
                &self.verifier,
                &GrantRequest {
                    repository: &repository,
                    signer: &auth.signer,
                    capability: Capability::Write,
                    now_ms,
                },
            )
            .map_err(rejected)
    }

    /// ยง5.2 checks 1โ€“5. This time-dependent result is never cached.
    pub(crate) fn verify_epoch(
        &self,
        header: &str,
        now_ms: i64,
    ) -> Result<VerifiedEpoch, GrantError> {
        verify_epoch_statement(&self.verifier, header, now_ms)
    }
}

/// Parse the accepted owner schemes (`--grant-schemes`, the Worker
/// `GRANT_SCHEMES` var): comma-separated SPEC-WRITE-GRANTS ยง4 tokens. The
/// value must not be blank and may not contain a blank entry. Which tokens
/// exist is `mkit-attest`'s to say.
///
/// # Errors
/// A message for a blank value, a blank entry or an unknown token.
pub fn parse_grant_schemes(text: &str) -> Result<AcceptedSchemes, String> {
    let tokens: Vec<&str> = text.split(',').map(str::trim).collect();
    if tokens.iter().any(|token| token.is_empty()) {
        return Err("grant schemes must be a non-empty, comma-separated list".to_owned());
    }
    AcceptedSchemes::from_tokens(tokens)
        .map_err(|error| format!("invalid grant schemes: {}", error.reason()))
}

/// Parse one relying-party entry, `id=origin[,origin...]`, split on the
/// first `=` (origins never contain a bare `=` before their id ends, but may
/// contain one later, for example in a query-like native-app origin). The id
/// and origin rules are `mkit-attest`'s ([`RelyingParty::new`]).
///
/// # Errors
/// A message for an entry without `=`, or one `RelyingParty::new` refuses.
pub fn parse_relying_party(entry: &str) -> Result<RelyingParty, String> {
    let (id, origins) = entry
        .split_once('=')
        .ok_or_else(|| "a relying party entry is `id=origin[,origin...]`".to_owned())?;
    RelyingParty::new(id.trim(), origins.split(',').map(str::trim))
        .map_err(|error| format!("invalid relying party `{}`: {}", id.trim(), error.reason()))
}

/// Parse the Worker `WEBAUTHN_RPS` var: entries ([`parse_relying_party`])
/// separated by `;` or newlines. A blank entry and a duplicate id are
/// refused, so a stray separator never silently drops a relying party.
///
/// # Errors
/// A message naming the first bad entry, a blank value or a duplicate id.
pub fn parse_relying_parties(text: &str) -> Result<Vec<RelyingParty>, String> {
    let mut parties: Vec<RelyingParty> = Vec::new();
    for entry in text.trim().split([';', '\n']) {
        let entry = entry.trim();
        if entry.is_empty() {
            return Err("relying parties: blank entry".to_owned());
        }
        let party = parse_relying_party(entry)?;
        if parties.iter().any(|known| known.id() == party.id()) {
            return Err(format!("relying parties: duplicate id `{}`", party.id()));
        }
        parties.push(party);
    }
    Ok(parties)
}

/// A deployment's validated write-grant inputs, kept apart from the
/// [`GrantConfig`] built from them so an adapter's configuration can be
/// compared and re-built (a `GrantConfig` holds no equality).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GrantSettings {
    /// The accepted owner schemes.
    pub schemes: AcceptedSchemes,
    /// The accepted `WebAuthn` relying parties.
    pub relying_parties: Vec<RelyingParty>,
    /// Development only: allow a loopback audience or relying party.
    pub allow_loopback: bool,
}

impl GrantSettings {
    /// Combine the adapter's raw settings: `None` when nothing is set
    /// (grants stay off), otherwise the settings, which a bad or partial
    /// value never degrades to "off".
    ///
    /// # Errors
    /// A message when relying parties or the loopback opt-in are set without
    /// any scheme.
    pub fn from_parts(
        schemes: Option<AcceptedSchemes>,
        relying_parties: Vec<RelyingParty>,
        allow_loopback: bool,
    ) -> Result<Option<Self>, String> {
        match schemes {
            Some(schemes) => Ok(Some(Self {
                schemes,
                relying_parties,
                allow_loopback,
            })),
            None if relying_parties.is_empty() && !allow_loopback => Ok(None),
            None => Err(
                "relying parties and the loopback opt-in configure write grants: set the \
                 grant schemes too"
                    .to_owned(),
            ),
        }
    }

    /// Build the verifier for `audience` (the deployment's auth v2 audience):
    /// the production constructor, or the loopback one when the opt-in is set.
    ///
    /// # Errors
    /// `invalid_argument` for any refusal of `mkit-attest`'s verifier rules:
    /// a loopback audience or relying party without the opt-in,
    /// `webauthn-p256` without a relying party, or a duplicate relying party.
    pub fn build(&self, audience: &str) -> Result<GrantConfig, ServerError> {
        if self.allow_loopback {
            GrantConfig::new_allowing_loopback(audience, self.schemes, self.relying_parties.clone())
        } else {
            GrantConfig::new(audience, self.schemes, self.relying_parties.clone())
        }
    }
}

fn config_error(error: GrantError) -> ServerError {
    ServerError::invalid_argument(format!("invalid grant configuration: {}", error.reason()))
}

/// The single public mapping for a rejected write grant.
pub(crate) fn rejected(error: GrantError) -> ServerError {
    ServerError::permission_denied(format!("write grant rejected: {}", error.reason()))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Code;

    #[test]
    fn every_grant_error_is_permission_denied() {
        for error in [
            GrantError::StatementTooLong,
            GrantError::CarriageReturn,
            GrantError::ByteOutOfRange,
            GrantError::FinalLineFeed,
            GrantError::FieldCount,
            GrantError::EmptyField,
            GrantError::Domain,
            GrantError::Namespace,
            GrantError::RepositoryScope,
            GrantError::ScopeNamespaceMismatch,
            GrantError::Decimal,
            GrantError::DecimalOutOfRange,
            GrantError::Hex,
            GrantError::Capabilities,
            GrantError::Audience,
            GrantError::AudienceWildcard,
            GrantError::AudienceCount,
            GrantError::AudiencesUnordered,
            GrantError::RefScopesOnRead,
            GrantError::RefScopesMissing,
            GrantError::RefPattern,
            GrantError::PackmapPattern,
            GrantError::UnknownRefFlag,
            GrantError::RefFlagsNotCanonical,
            GrantError::RefScopeCount,
            GrantError::RefScopesUnordered,
            GrantError::DuplicateRefPattern,
            GrantError::ExpiryNotAfterCreated,
            GrantError::LifetimeTooLong,
            GrantError::HeaderTooLong,
            GrantError::HeaderFormat,
            GrantError::HeaderBase64,
            GrantError::UnknownScheme,
            GrantError::Repository,
            GrantError::Visibility,
            GrantError::SchemeNotAdvertised,
            GrantError::SchemeNamespaceMismatch,
            GrantError::SignatureLength,
            GrantError::BadSignature,
            GrantError::SignatureRecoveryId,
            GrantError::SignatureScalar,
            GrantError::HighS,
            GrantError::OwnerMismatch,
            GrantError::InvalidOwnerKey,
            GrantError::WebAuthnBlob,
            GrantError::AuthenticatorData,
            GrantError::UserNotPresent,
            GrantError::RelyingPartyMismatch,
            GrantError::ClientData,
            GrantError::ClientDataType,
            GrantError::Challenge,
            GrantError::CrossOrigin,
            GrantError::TopOrigin,
            GrantError::OriginNotAllowed,
            GrantError::NamespaceMismatch,
            GrantError::RepositoryMismatch,
            GrantError::AudienceNotListed,
            GrantError::RepositoryNotInScope,
            GrantError::CapabilityNotGranted,
            GrantError::GranteeMismatch,
            GrantError::NotYetValid,
            GrantError::Expired,
            GrantError::LoopbackAudience,
            GrantError::NoRelyingParty,
            GrantError::RelyingParty,
            GrantError::LoopbackRelyingParty,
        ] {
            let mapped = rejected(error);
            assert_eq!(mapped.code(), Code::PermissionDenied);
            assert_eq!(
                mapped.public_message(),
                format!("write grant rejected: {}", error.reason())
            );
        }
    }

    #[test]
    fn scheme_lists_parse_and_refuse_blanks_and_unknown_tokens() {
        let all = parse_grant_schemes("ed25519, secp256k1-eip191,webauthn-p256").unwrap();
        assert_eq!(
            all,
            AcceptedSchemes::of(&mkit_attest::grant::OwnerScheme::ALL)
        );
        for bad in [
            "",
            "  ",
            "ed25519,",
            ",ed25519",
            "ed25519,,webauthn-p256",
            "rsa",
        ] {
            assert!(parse_grant_schemes(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn relying_party_entries_split_on_the_first_equals() {
        let party =
            parse_relying_party("example.test=https://example.test,https://app.example.test")
                .unwrap();
        assert_eq!(party.id(), "example.test");
        assert_eq!(
            party.origins(),
            ["https://example.test", "https://app.example.test"]
        );
        // A later `=` belongs to the origin.
        let party = parse_relying_party("example.test=android:apk-key-hash:a=b").unwrap();
        assert_eq!(party.origins(), ["android:apk-key-hash:a=b"]);
        for bad in [
            "example.test",
            "=https://example.test",
            "example.test=",
            "example.test=https://a,,https://b",
            "Example.Test=https://a",
            "example.test=https://a,https://a",
        ] {
            assert!(parse_relying_party(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn relying_party_lists_take_semicolons_or_newlines_and_refuse_blanks_and_duplicates() {
        let list = "a.test=https://a.test;b.test=https://b.test\nc.test=https://c.test\n";
        let parties = parse_relying_parties(list).unwrap();
        assert_eq!(
            parties.iter().map(RelyingParty::id).collect::<Vec<_>>(),
            ["a.test", "b.test", "c.test"]
        );
        for bad in [
            "",
            "  ",
            "a.test=https://a.test;;b.test=https://b.test",
            "a.test=https://a.test;",
            "a.test=https://a.test;a.test=https://other.test",
            "a.test",
        ] {
            assert!(parse_relying_parties(bad).is_err(), "{bad:?}");
        }
    }

    #[test]
    fn grant_settings_build_refuses_what_attest_refuses_and_never_degrades() {
        let ed = parse_grant_schemes("ed25519").unwrap();
        let web = parse_grant_schemes("ed25519,webauthn-p256").unwrap();
        let rp = || parse_relying_party("example.test=https://example.test").unwrap();
        // Nothing set: grants are off. Partial settings are refused.
        assert_eq!(GrantSettings::from_parts(None, vec![], false), Ok(None));
        assert!(GrantSettings::from_parts(None, vec![rp()], false).is_err());
        assert!(GrantSettings::from_parts(None, vec![], true).is_err());
        let build = |schemes, rps, loopback, audience: &str| {
            GrantSettings::from_parts(Some(schemes), rps, loopback)
                .unwrap()
                .unwrap()
                .build(audience)
        };
        assert!(build(ed, vec![], false, "https://vcs.example").is_ok());
        assert!(build(web, vec![rp()], false, "https://vcs.example").is_ok());
        assert!(build(web, vec![], false, "https://vcs.example").is_err());
        // Loopback audiences and relying parties need the opt-in.
        assert!(build(ed, vec![], false, "http://localhost:8787").is_err());
        assert!(build(ed, vec![], true, "http://localhost:8787").is_ok());
        let local = parse_relying_party("localhost=http://localhost:8787").unwrap();
        assert!(build(web, vec![local.clone()], false, "https://vcs.example").is_err());
        assert!(build(web, vec![local], true, "https://vcs.example").is_ok());
        let error = build(ed, vec![], false, "not an origin").unwrap_err();
        assert_eq!(error.code(), Code::InvalidArgument);
    }

    #[test]
    fn configuration_and_scheme_order() {
        let schemes =
            AcceptedSchemes::from_tokens(["webauthn-p256", "ed25519", "secp256k1-eip191"]).unwrap();
        assert_eq!(
            schemes.tokens().collect::<Vec<_>>(),
            ["ed25519", "secp256k1-eip191", "webauthn-p256"]
        );
        let rp = RelyingParty::new("example.test", ["https://example.test"]).unwrap();
        assert_eq!(
            GrantConfig::new("http://localhost", schemes, vec![rp.clone()])
                .unwrap_err()
                .code(),
            Code::InvalidArgument
        );
        let cfg =
            GrantConfig::new_allowing_loopback("http://localhost", schemes, vec![rp]).unwrap();
        assert_eq!(cfg.audience(), "http://localhost");
        assert_eq!(cfg.schemes(), schemes);
        assert_eq!(
            GrantConfig::new("https://example.test", schemes, vec![])
                .unwrap_err()
                .code(),
            Code::InvalidArgument
        );
    }
}