ic-asset-router 0.1.1

File-based HTTP routing with IC response certification for Internet Computer canisters
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
//! Certification mode configuration for HTTP responses.
//!
//! IC HTTP certification has three fundamental modes that determine which
//! parts of the HTTP request/response are hashed and cryptographically
//! certified. These types let you configure certification granularity
//! per-asset or per-route.
//!
//! # Modes
//!
//! - [`CertificationMode::Skip`] — No certification. Fastest, use for public
//!   endpoints where tampering risk is acceptable.
//! - [`CertificationMode::ResponseOnly`] — Only the response is certified.
//!   Good for static assets where the response depends only on the URL path.
//! - [`CertificationMode::Full`] — Both request and response are certified.
//!   Required when the response depends on request headers (e.g.,
//!   `Authorization`, `Accept`).
//!
//! # Choosing a Mode
//!
//! **Response-only (default)** is correct for 90% of routes — use it when
//! the response depends only on the URL path and the canister state.
//!
//! **Skip** is appropriate for health-check or status endpoints where
//! tampering has no security impact and maximum performance is desired.
//!
//! **Full** (or the [`CertificationMode::authenticated`] preset) is
//! required when the response depends on *who* is making the request
//! (e.g., the `Authorization` header). Without full certification a
//! malicious replica could serve one user's response to another.

/// Certification mode for HTTP responses.
///
/// Determines which parts of the HTTP request/response are hashed and
/// certified by the Internet Computer's boundary nodes. The default mode
/// is [`CertificationMode::ResponseOnly`] with wildcard header inclusion
/// and standard exclusions.
///
/// # When to Use Each Variant
///
/// | Variant | Use when | Example |
/// |---------|----------|---------|
/// | [`Skip`](Self::Skip) | Tampering has no security impact | Health checks, `/ping` |
/// | [`ResponseOnly`](Self::ResponseOnly) | Same URL always returns same content | Static pages, blog posts |
/// | [`Full`](Self::Full) | Response depends on request identity | Authenticated APIs |
///
/// # Examples
///
/// ```
/// use ic_asset_router::CertificationMode;
///
/// // Default: response-only (recommended for most routes)
/// let mode = CertificationMode::default();
/// assert!(matches!(mode, CertificationMode::ResponseOnly(_)));
///
/// // Skip: no certification overhead
/// let mode = CertificationMode::skip();
/// assert!(matches!(mode, CertificationMode::Skip));
///
/// // Authenticated: full certification with Authorization header
/// let mode = CertificationMode::authenticated();
/// assert!(matches!(mode, CertificationMode::Full(_)));
/// ```
#[derive(Clone, Debug)]
pub enum CertificationMode {
    /// No certification. The response is served without cryptographic
    /// verification.
    ///
    /// **Handler execution:** Unlike ResponseOnly and Full modes, skip-mode
    /// routes run the handler on every query call. This makes them behave
    /// like candid `query` calls — fast (~200ms) and executed on a single
    /// replica without consensus. This enables handler-level auth checks
    /// (e.g. validating a JWT or checking `ic_cdk::caller()`) on every
    /// request, which is useful for authenticated API endpoints where
    /// per-call latency matters more than response certification.
    ///
    /// **Security model:** Skip certification provides the same trust level
    /// as candid query calls — both trust the responding replica. The
    /// response is not cryptographically verified by the boundary node in
    /// either case. If candid queries are acceptable for your application,
    /// skip certification is equally acceptable.
    ///
    /// **When to use:**
    /// - Health checks, `/ping`, and other low-value endpoints
    /// - Auth-gated API endpoints where you need fast query-path performance
    ///   with per-call authentication (combine with handler-level auth)
    ///
    /// **When NOT to use:**
    /// - Endpoints where you need the boundary node to cryptographically
    ///   verify the response (use ResponseOnly or Full instead)
    Skip,

    /// Only the response is certified. Request details (headers, query
    /// params) are not included in the certification hash. This is the
    /// **default mode** and is correct for the vast majority of routes
    /// where the response depends only on the URL path and canister state.
    ///
    /// Use [`ResponseOnlyConfig`] to control which response headers
    /// participate in the hash. The response body and status code are
    /// always certified regardless of header configuration.
    ResponseOnly(ResponseOnlyConfig),

    /// Both request and response are certified. Required when the response
    /// depends on request identity — for example, when different
    /// `Authorization` headers produce different responses. Without full
    /// certification a malicious replica could serve one user's cached
    /// response to another user.
    ///
    /// Use [`FullConfig`] (or the [`FullConfigBuilder`]) to specify which
    /// request headers and query parameters participate in the hash.
    /// The request method and body are **always** certified automatically.
    Full(FullConfig),
}

impl CertificationMode {
    /// Create a skip-certification mode.
    ///
    /// Skip-mode routes run the handler on every query call (like candid
    /// queries) and attach a skip certification witness. See
    /// [`CertificationMode::Skip`] for the full security model.
    ///
    /// Equivalent to `CertificationMode::Skip`. Provided for symmetry
    /// with [`response_only()`](Self::response_only) and
    /// [`authenticated()`](Self::authenticated).
    pub fn skip() -> Self {
        Self::Skip
    }

    /// Create a response-only certification mode with the default
    /// [`ResponseOnlyConfig`] (wildcard header inclusion, standard
    /// exclusions).
    ///
    /// This is also what [`CertificationMode::default()`] returns.
    pub fn response_only() -> Self {
        Self::ResponseOnly(ResponseOnlyConfig::default())
    }

    /// Create a full-certification preset for authenticated APIs.
    ///
    /// Includes the `Authorization` request header and `Content-Type`
    /// response header in the certification hash. Use this when the
    /// response depends on the caller's identity — different
    /// `Authorization` tokens will produce independently certified
    /// responses, preventing cross-user response mixing.
    ///
    /// # Example
    ///
    /// ```
    /// use ic_asset_router::CertificationMode;
    ///
    /// let mode = CertificationMode::authenticated();
    /// match mode {
    ///     CertificationMode::Full(config) => {
    ///         assert_eq!(config.request_headers, vec!["authorization"]);
    ///         assert_eq!(config.response.include_headers, vec!["content-type"]);
    ///     }
    ///     _ => unreachable!(),
    /// }
    /// ```
    pub fn authenticated() -> Self {
        Self::Full(
            FullConfig::builder()
                .with_request_headers(&["authorization"])
                .with_response_headers(&["content-type"])
                .build(),
        )
    }
}

impl Default for CertificationMode {
    fn default() -> Self {
        Self::response_only()
    }
}

/// Configuration for response-only certification.
///
/// Controls which response headers participate in the certification hash.
/// The response body and status code are **always** certified regardless
/// of header configuration.
///
/// # Header Selection
///
/// There are two strategies for selecting headers:
///
/// 1. **Wildcard with exclusions** (default) — set `include_headers` to
///    `["*"]` and list headers to skip in `exclude_headers`. This is the
///    safest default because new headers are automatically covered.
///
/// 2. **Explicit inclusion** — list only the headers you want certified.
///    Use this when you need precise control or want to minimize the
///    certification payload.
///
/// # Default
///
/// The default configuration includes all headers (`"*"`) and excludes
/// `date`, `ic-certificate`, and `ic-certificate-expression` (which
/// are either non-deterministic or managed by the certification layer
/// itself).
#[derive(Clone, Debug)]
pub struct ResponseOnlyConfig {
    /// Response headers to include in the certification hash.
    ///
    /// Set to `vec!["*".to_string()]` to include all headers (with
    /// exclusions applied via [`exclude_headers`](Self::exclude_headers)).
    /// Alternatively, list specific header names for explicit inclusion.
    pub include_headers: Vec<String>,

    /// Response headers to explicitly exclude from certification.
    ///
    /// Applied after `include_headers`. Only meaningful when
    /// `include_headers` contains `"*"`.
    pub exclude_headers: Vec<String>,
}

impl Default for ResponseOnlyConfig {
    fn default() -> Self {
        Self {
            include_headers: vec!["*".to_string()],
            exclude_headers: vec![
                "date".to_string(),
                "ic-certificate".to_string(),
                "ic-certificate-expression".to_string(),
            ],
        }
    }
}

/// Configuration for full request+response certification.
///
/// In full mode the request method and body are **always** certified by
/// `ic-http-certification` — there is no opt-out. The configurable parts
/// are which request headers and query parameters participate in the
/// certification hash.
///
/// # When to Use
///
/// Use full certification when the response depends on details of the
/// incoming request beyond the URL path:
///
/// - **`Authorization` header** — different users receive different
///   responses. Use the [`CertificationMode::authenticated`] preset.
/// - **`Accept` header** — content negotiation (JSON vs HTML).
/// - **Query parameters** — pagination (`?page=2`), filtering, sorting.
///
/// # Which Headers to Certify
///
/// Only certify headers that **affect the response content**. Certifying
/// headers like `User-Agent` causes cache fragmentation (every browser
/// version gets a separate certificate) with no security benefit.
///
/// # Example
///
/// ```
/// use ic_asset_router::FullConfig;
///
/// let config = FullConfig::builder()
///     .with_request_headers(&["authorization", "accept"])
///     .with_query_params(&["page", "limit"])
///     .with_response_headers(&["content-type"])
///     .build();
///
/// assert_eq!(config.request_headers, vec!["authorization", "accept"]);
/// assert_eq!(config.query_params, vec!["page", "limit"]);
/// ```
#[derive(Clone, Debug)]
#[derive(Default)]
pub struct FullConfig {
    /// Request headers to include in the certification hash.
    ///
    /// Only these headers are hashed; all other request headers are
    /// ignored during certification. Header names should be lowercase.
    pub request_headers: Vec<String>,

    /// Query parameters to include in the certification hash.
    ///
    /// When set, requests with different values for these parameters
    /// produce independently certified responses. A malicious replica
    /// cannot serve the `?page=1` response when `?page=2` is requested.
    pub query_params: Vec<String>,

    /// Response certification configuration (header inclusion/exclusion).
    pub response: ResponseOnlyConfig,
}


impl FullConfig {
    /// Create a builder for ergonomic construction of [`FullConfig`].
    pub fn builder() -> FullConfigBuilder {
        FullConfigBuilder::default()
    }
}

/// Builder for [`FullConfig`] with ergonomic chained construction.
///
/// All `with_*_headers` methods normalize header names to lowercase,
/// so `"Authorization"` and `"authorization"` are treated identically.
///
/// # Example
///
/// ```
/// use ic_asset_router::FullConfig;
///
/// let config = FullConfig::builder()
///     .with_request_headers(&["Authorization", "Accept"])
///     .with_query_params(&["page", "limit"])
///     .with_response_headers(&["Content-Type"])
///     .excluding_response_headers(&["Set-Cookie"])
///     .build();
///
/// assert_eq!(config.request_headers, vec!["authorization", "accept"]);
/// assert_eq!(config.query_params, vec!["page", "limit"]);
/// assert_eq!(config.response.include_headers, vec!["content-type"]);
/// assert_eq!(config.response.exclude_headers, vec!["set-cookie"]);
/// ```
#[derive(Default)]
pub struct FullConfigBuilder {
    request_headers: Vec<String>,
    query_params: Vec<String>,
    include_response_headers: Vec<String>,
    exclude_response_headers: Vec<String>,
}

impl FullConfigBuilder {
    /// Set the request headers to include in certification.
    ///
    /// Header names are normalized to lowercase.
    pub fn with_request_headers(mut self, headers: &[&str]) -> Self {
        self.request_headers = headers.iter().map(|s| s.to_lowercase()).collect();
        self
    }

    /// Set the query parameters to include in certification.
    pub fn with_query_params(mut self, params: &[&str]) -> Self {
        self.query_params = params.iter().map(|s| s.to_string()).collect();
        self
    }

    /// Set the response headers to include in certification.
    ///
    /// Header names are normalized to lowercase. When set, replaces the
    /// default wildcard inclusion.
    pub fn with_response_headers(mut self, headers: &[&str]) -> Self {
        self.include_response_headers = headers.iter().map(|s| s.to_lowercase()).collect();
        self
    }

    /// Set the response headers to exclude from certification.
    ///
    /// Header names are normalized to lowercase.
    pub fn excluding_response_headers(mut self, headers: &[&str]) -> Self {
        self.exclude_response_headers = headers.iter().map(|s| s.to_lowercase()).collect();
        self
    }

    /// Consume the builder and produce a [`FullConfig`].
    pub fn build(self) -> FullConfig {
        FullConfig {
            request_headers: self.request_headers,
            query_params: self.query_params,
            response: ResponseOnlyConfig {
                include_headers: if self.include_response_headers.is_empty() {
                    vec!["*".to_string()]
                } else {
                    self.include_response_headers
                },
                exclude_headers: self.exclude_response_headers,
            },
        }
    }
}

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

    #[test]
    fn default_is_response_only() {
        let mode = CertificationMode::default();
        assert!(matches!(mode, CertificationMode::ResponseOnly(_)));
    }

    #[test]
    fn skip_produces_skip() {
        let mode = CertificationMode::skip();
        assert!(matches!(mode, CertificationMode::Skip));
    }

    #[test]
    fn response_only_has_correct_default_config() {
        let mode = CertificationMode::response_only();
        match mode {
            CertificationMode::ResponseOnly(config) => {
                assert_eq!(config.include_headers, vec!["*"]);
                assert_eq!(
                    config.exclude_headers,
                    vec!["date", "ic-certificate", "ic-certificate-expression"]
                );
            }
            _ => panic!("expected ResponseOnly"),
        }
    }

    #[test]
    fn authenticated_has_authorization_in_request_headers() {
        let mode = CertificationMode::authenticated();
        match mode {
            CertificationMode::Full(config) => {
                assert_eq!(config.request_headers, vec!["authorization"]);
                assert_eq!(config.response.include_headers, vec!["content-type"]);
            }
            _ => panic!("expected Full"),
        }
    }

    #[test]
    fn builder_with_all_options() {
        let config = FullConfig::builder()
            .with_request_headers(&["Authorization", "Accept"])
            .with_query_params(&["page", "limit"])
            .with_response_headers(&["Content-Type", "ETag"])
            .excluding_response_headers(&["Set-Cookie"])
            .build();

        assert_eq!(config.request_headers, vec!["authorization", "accept"]);
        assert_eq!(config.query_params, vec!["page", "limit"]);
        assert_eq!(
            config.response.include_headers,
            vec!["content-type", "etag"]
        );
        assert_eq!(config.response.exclude_headers, vec!["set-cookie"]);
    }

    #[test]
    fn builder_with_partial_options() {
        let config = FullConfig::builder()
            .with_request_headers(&["authorization"])
            .build();

        assert_eq!(config.request_headers, vec!["authorization"]);
        assert!(config.query_params.is_empty());
        // No explicit response headers → wildcard
        assert_eq!(config.response.include_headers, vec!["*"]);
        assert!(config.response.exclude_headers.is_empty());
    }

    #[test]
    fn builder_with_no_options() {
        let config = FullConfig::builder().build();

        assert!(config.request_headers.is_empty());
        assert!(config.query_params.is_empty());
        assert_eq!(config.response.include_headers, vec!["*"]);
        assert!(config.response.exclude_headers.is_empty());
    }

    #[test]
    fn header_normalization_to_lowercase() {
        let config = FullConfig::builder()
            .with_request_headers(&["AUTHORIZATION", "Accept-Encoding"])
            .with_response_headers(&["Content-Type", "X-CUSTOM-HEADER"])
            .excluding_response_headers(&["SET-COOKIE"])
            .build();

        assert_eq!(
            config.request_headers,
            vec!["authorization", "accept-encoding"]
        );
        assert_eq!(
            config.response.include_headers,
            vec!["content-type", "x-custom-header"]
        );
        assert_eq!(config.response.exclude_headers, vec!["set-cookie"]);
    }

    #[test]
    fn full_config_default() {
        let config = FullConfig::default();
        assert!(config.request_headers.is_empty());
        assert!(config.query_params.is_empty());
        assert_eq!(config.response.include_headers, vec!["*"]);
        assert_eq!(
            config.response.exclude_headers,
            vec!["date", "ic-certificate", "ic-certificate-expression"]
        );
    }

    #[test]
    fn response_only_config_default() {
        let config = ResponseOnlyConfig::default();
        assert_eq!(config.include_headers, vec!["*"]);
        assert_eq!(
            config.exclude_headers,
            vec!["date", "ic-certificate", "ic-certificate-expression"]
        );
    }

    #[test]
    fn certification_mode_clone_and_debug() {
        let mode = CertificationMode::authenticated();
        let cloned = mode.clone();
        // Verify Debug is implemented (would fail to compile otherwise)
        let _debug = format!("{:?}", cloned);
    }
}