ic-http-gateway 0.4.3

An HTTP Gateway implementation for interfacing with the Internet Computer over HTTP
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
use super::validate;
use crate::{
    get_206_stream_response_body_and_total_length, get_body_and_streaming_body, CanisterRequest,
    CanisterResponse, HttpGatewayError, HttpGatewayResponse, HttpGatewayResponseBody,
    HttpGatewayResponseMetadata, HttpGatewayResult, ACCEPT_ENCODING_HEADER_NAME, CACHE_HEADER_NAME,
};
use candid::Principal;
use http::header as http_header;
use http::{Response, StatusCode};
use http_body_util::{BodyExt, Either, Full};
use ic_agent::{
    agent::{RejectCode, RejectResponse},
    Agent, AgentError,
};
use ic_http_certification::{HttpRequest, HttpResponse};
use ic_response_verification::MAX_VERIFICATION_VERSION;
use ic_utils::{
    call::{AsyncCall, SyncCall},
    interfaces::{http_request::HeaderField, HttpRequestCanister},
};

fn create_err_response(status_code: StatusCode, msg: &str) -> CanisterResponse {
    let mut response = Response::new(HttpGatewayResponseBody::Right(Full::from(
        msg.as_bytes().to_vec(),
    )));
    *response.status_mut() = status_code;

    response
}

fn convert_request(request: CanisterRequest) -> HttpGatewayResult<HttpRequest<'static>> {
    let uri = request.uri();
    let mut url = uri.path().to_string();
    if let Some(query) = uri.query() {
        url.push('?');
        url.push_str(query);
    }

    Ok(HttpRequest::builder()
        .with_method(request.method().clone())
        .with_url(url)
        .with_headers(
            request
                .headers()
                .into_iter()
                .map(|(name, value)| {
                    Ok((
                        name.to_string(),
                        value
                            .to_str()
                            .map_err(|_| HttpGatewayError::HeaderValueParsingError {
                                header_name: name.to_string(),
                                header_value: String::from_utf8_lossy(value.as_bytes()).to_string(),
                            })?
                            .to_string(),
                    ))
                })
                .collect::<HttpGatewayResult<Vec<_>>>()?,
        )
        .with_body(request.body().to_vec())
        .build())
}

pub async fn process_request(
    agent: &Agent,
    request: CanisterRequest,
    canister_id: Principal,
    skip_verification: bool,
) -> HttpGatewayResponse {
    let http_request = match convert_request(request) {
        Ok(http_request) => http_request,
        Err(e) => {
            return HttpGatewayResponse {
                canister_response: create_err_response(
                    StatusCode::BAD_REQUEST,
                    &format!("Failed to parse request: {}", e),
                ),
                metadata: HttpGatewayResponseMetadata {
                    upgraded_to_update_call: false,
                    response_verification_version: None,
                    internal_error: Some(e),
                },
            }
        }
    };

    let canister = HttpRequestCanister::create(agent, canister_id);
    let mut is_range_request = false;
    let header_fields = http_request
        .headers()
        .iter()
        .filter(|(name, _)| name != "x-request-id")
        .map(|(name, value)| {
            if name.eq_ignore_ascii_case(ACCEPT_ENCODING_HEADER_NAME) {
                let mut encodings = value.split(',').map(|s| s.trim()).collect::<Vec<_>>();
                if !encodings.iter().any(|s| s.eq_ignore_ascii_case("identity")) {
                    encodings.push("identity");
                };

                let value = encodings.join(", ");
                return HeaderField(name.into(), value.into());
            } else if name.eq_ignore_ascii_case(http_header::RANGE.as_ref())
                || name.eq_ignore_ascii_case(http_header::IF_RANGE.as_ref())
            {
                is_range_request = true;
            }

            HeaderField(name.into(), value.into())
        })
        .collect::<Vec<HeaderField>>()
        .into_iter();

    let query_result = canister
        .http_request_custom(
            http_request.method().as_str(),
            http_request.url(),
            header_fields.clone(),
            http_request.body(),
            Some(&u16::from(MAX_VERIFICATION_VERSION)),
        )
        .call()
        .await;

    let agent_response = match query_result {
        Ok((response,)) => response,
        Err(e) => {
            return HttpGatewayResponse {
                canister_response: handle_agent_error(&e),
                metadata: HttpGatewayResponseMetadata {
                    upgraded_to_update_call: false,
                    response_verification_version: None,
                    internal_error: Some(e.into()),
                },
            };
        }
    };

    let is_update_call = agent_response.upgrade == Some(true);
    let agent_response = if is_update_call {
        let update_result = canister
            .http_request_update_custom(
                http_request.method().as_str(),
                http_request.url(),
                header_fields.clone(),
                http_request.body(),
            )
            .call_and_wait()
            .await;

        match update_result {
            Ok((response,)) => response,
            Err(e) => {
                return HttpGatewayResponse {
                    canister_response: handle_agent_error(&e),
                    metadata: HttpGatewayResponseMetadata {
                        upgraded_to_update_call: true,
                        response_verification_version: None,
                        internal_error: Some(e.into()),
                    },
                };
            }
        }
    } else {
        agent_response
    };

    let response_body = match get_body_and_streaming_body(agent, &agent_response).await {
        Ok(response_body) => response_body,
        Err(e) => {
            return HttpGatewayResponse {
                canister_response: create_err_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    &format!("Failed to parse response body: {}", e),
                ),
                metadata: HttpGatewayResponseMetadata {
                    upgraded_to_update_call: is_update_call,
                    response_verification_version: None,
                    internal_error: Some(e.into()),
                },
            }
        }
    };

    // There is no need to verify the response if the request was upgraded to an update call.
    let validation_info = if !is_update_call {
        // At the moment verification is only performed if the response is not using a streaming
        // strategy. Performing verification for those requests would require to join all the chunks
        // and this could cause memory issues and possibly create DOS attack vectors.
        match &response_body {
            Either::Right(body) => {
                // this unwrap should never panic because `Either::Right` will always have a full body
                let body = body.clone().collect().await.unwrap().to_bytes().to_vec();

                let status_code = match StatusCode::from_u16(agent_response.status_code) {
                    Ok(status) => status,
                    Err(e) => {
                        return HttpGatewayResponse {
                            canister_response: create_err_response(
                                StatusCode::INTERNAL_SERVER_ERROR,
                                &format!("Invalid canister response status code: {}", e),
                            ),
                            metadata: HttpGatewayResponseMetadata {
                                upgraded_to_update_call: is_update_call,
                                response_verification_version: None,
                                internal_error: Some(http::Error::from(e).into()),
                            },
                        };
                    }
                };
                let response = HttpResponse::builder()
                    .with_status_code(status_code)
                    .with_headers(
                        agent_response
                            .headers
                            .iter()
                            .map(|HeaderField(k, v)| (k.to_string(), v.to_string()))
                            .collect(),
                    )
                    .with_body(body)
                    .build();

                let validation_result = validate(
                    agent,
                    &canister_id,
                    http_request.clone(),
                    response,
                    skip_verification,
                );

                match validation_result {
                    Err(e) => {
                        return HttpGatewayResponse {
                            canister_response: create_err_response(
                                StatusCode::INTERNAL_SERVER_ERROR,
                                &format!("Response verification failed: {}", e),
                            ),
                            metadata: HttpGatewayResponseMetadata {
                                upgraded_to_update_call: is_update_call,
                                response_verification_version: None,
                                internal_error: Some(e),
                            },
                        };
                    }
                    Ok(validation_info) => validation_info,
                }
            }
            _ => None,
        }
    } else {
        None
    };

    let response_verification_version = validation_info.as_ref().map(|e| e.verification_version);

    let status_code = match StatusCode::from_u16(agent_response.status_code) {
        Ok(status_code) => status_code,
        Err(e) => {
            return HttpGatewayResponse {
                canister_response: create_err_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    &format!("Invalid canister response status code: {}", e),
                ),
                metadata: HttpGatewayResponseMetadata {
                    upgraded_to_update_call: is_update_call,
                    response_verification_version,
                    internal_error: Some(http::Error::from(e).into()),
                },
            }
        }
    };

    let mut response_builder = Response::builder().status(status_code);
    match &validation_info {
        // if there is no validation info, that means we've skipped verification,
        // this should only happen for raw domains.
        None => {
            for HeaderField(name, value) in &agent_response.headers {
                response_builder = response_builder.header(name.as_ref(), value.as_ref());
            }
        }

        Some(validation_info) => {
            if validation_info.verification_version < 2 {
                // status codes are not certified in v1, reject known dangerous status codes
                if agent_response.status_code >= 300 && agent_response.status_code < 400 {
                    return HttpGatewayResponse {
                        canister_response: create_err_response(
                            StatusCode::INTERNAL_SERVER_ERROR,
                            "Response verification v1 does not allow redirects",
                        ),
                        metadata: HttpGatewayResponseMetadata {
                            upgraded_to_update_call: is_update_call,
                            response_verification_version,
                            internal_error: None,
                        },
                    };
                }

                // headers are also not certified in v1, filter known dangerous headers
                for HeaderField(name, value) in &agent_response.headers {
                    if !name.eq_ignore_ascii_case(CACHE_HEADER_NAME) {
                        response_builder = response_builder.header(name.as_ref(), value.as_ref());
                    }
                }
            } else {
                match &validation_info.response {
                    // if there is no response, the canister has decided to certifiably skip verification,
                    // assume the developer knows what they're doing and return the response as-is
                    None => {
                        for HeaderField(name, value) in &agent_response.headers {
                            // If the request is not a range-request, but got range-response,
                            // do not copy "Content-Range" and "Content-Length" headers,
                            // as clients obtain the full asset via a streaming response.
                            if !is_range_request
                                && status_code == 206
                                && (name.eq_ignore_ascii_case(http_header::CONTENT_RANGE.as_ref())
                                    || name
                                        .eq_ignore_ascii_case(http_header::CONTENT_LENGTH.as_ref()))
                            {
                                // skip copying
                            } else {
                                response_builder =
                                    response_builder.header(name.as_ref(), value.as_ref());
                            }
                        }
                    }
                    // if there is a response, the canister has decided to certify some (but not necessarily all) headers,
                    // return only the certified headers
                    Some(certified_http_response) => {
                        for (name, value) in &certified_http_response.headers {
                            // If the request is not a range-request, but got range-response,
                            // do not copy "Content-Range" and "Content-Length" headers,
                            // as clients obtain the full asset via a streaming response.
                            if !is_range_request
                                && status_code == 206
                                && (name.eq_ignore_ascii_case(http_header::CONTENT_RANGE.as_ref())
                                    || name
                                        .eq_ignore_ascii_case(http_header::CONTENT_LENGTH.as_ref()))
                            {
                                // skip copying
                            } else {
                                response_builder = response_builder.header(name, value);
                            }
                        }
                    }
                }
            }
        }
    }

    let response_body: HttpGatewayResponseBody = if status_code == 206 && !is_range_request {
        // We got only the first chunk, add a correct content-length-header,
        // and turn the response into a streaming response.
        let (stream_response_body, content_length) =
            match get_206_stream_response_body_and_total_length(
                agent,
                http_request,
                canister_id,
                &agent_response.headers,
                response_body,
                skip_verification,
            )
            .await
            {
                Ok((stream_response_body, content_length)) => {
                    (stream_response_body, content_length)
                }
                Err(e) => {
                    return HttpGatewayResponse {
                        canister_response: create_err_response(
                            StatusCode::INTERNAL_SERVER_ERROR,
                            &format!("Failed to create streaming response: {}", e),
                        ),
                        metadata: HttpGatewayResponseMetadata {
                            upgraded_to_update_call: is_update_call,
                            response_verification_version,
                            internal_error: Some(e.into()),
                        },
                    }
                }
            };
        response_builder =
            response_builder.header(http_header::CONTENT_LENGTH, content_length.to_string());
        response_builder = response_builder.status(200);
        stream_response_body
    } else {
        response_body
    };

    let response = match response_builder.body(response_body) {
        Ok(response) => response,
        Err(e) => {
            return HttpGatewayResponse {
                canister_response: create_err_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    &format!("Failed to build response: {}", e),
                ),
                metadata: HttpGatewayResponseMetadata {
                    upgraded_to_update_call: is_update_call,
                    response_verification_version,
                    internal_error: Some(e.into()),
                },
            }
        }
    };

    HttpGatewayResponse {
        canister_response: response,
        metadata: HttpGatewayResponseMetadata {
            upgraded_to_update_call: is_update_call,
            response_verification_version,
            internal_error: None,
        },
    }
}

fn handle_agent_error(error: &AgentError) -> CanisterResponse {
    match error {
        // Turn all `DestinationInvalid`s into 404
        AgentError::CertifiedReject {
            reject:
                RejectResponse {
                    reject_code: RejectCode::DestinationInvalid,
                    reject_message,
                    ..
                },
            ..
        } => create_err_response(StatusCode::NOT_FOUND, reject_message),

        // If the result is a Replica error, returns the 500 code and message. There is no information
        // leak here because a user could use `dfx` to get the same reply.
        AgentError::CertifiedReject { reject, .. } => create_err_response(
            StatusCode::BAD_GATEWAY,
            &format!(
                "Replica Error: reject code {:?}, message {}, error code {:?}",
                reject.reject_code, reject.reject_message, reject.error_code,
            ),
        ),

        AgentError::UncertifiedReject {
            reject:
                RejectResponse {
                    reject_code: RejectCode::DestinationInvalid,
                    reject_message,
                    ..
                },
            ..
        } => create_err_response(StatusCode::NOT_FOUND, reject_message),

        // If the result is a Replica error, returns the 500 code and message. There is no information
        // leak here because a user could use `dfx` to get the same reply.
        AgentError::UncertifiedReject { reject, .. } => create_err_response(
            StatusCode::BAD_GATEWAY,
            &format!(
                "Replica Error: reject code {:?}, message {}, error code {:?}",
                reject.reject_code, reject.reject_message, reject.error_code,
            ),
        ),

        AgentError::ResponseSizeExceededLimit() => create_err_response(
            StatusCode::INSUFFICIENT_STORAGE,
            "Response size exceeds limit",
        ),

        AgentError::HttpError(payload) => match StatusCode::from_u16(payload.status) {
            Ok(status) => create_err_response(status, &format!("{:?}", payload)),
            Err(_) => create_err_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                &format!("Received invalid status code {:?}", payload),
            ),
        },

        // Handle all other errors
        _ => create_err_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            &format!("Internal Server Error: {:?}", error),
        ),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use http::Request;

    #[test]
    fn test_convert_request() {
        let request = Request::builder()
            .uri("http://example.com/foo/bar/baz?q=hello+world&t=1")
            .header("Accept", "text/html")
            .header("Accept-Encoding", "gzip, deflate, br, zstd")
            .body(Bytes::from("body"))
            .unwrap();

        let http_request = convert_request(request).unwrap();

        assert_eq!(
            http_request,
            HttpRequest::get("/foo/bar/baz?q=hello+world&t=1")
                .with_headers(vec![
                    ("accept".to_string(), "text/html".to_string()),
                    (
                        "accept-encoding".to_string(),
                        "gzip, deflate, br, zstd".to_string()
                    ),
                ])
                .with_body(b"body".to_vec())
                .build()
        );
    }
}