Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Jan 14, 2024
1 parent 567651b commit 1263da5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/canisters/frontend/ic-certified-assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ serde_cbor.workspace = true
sha2.workspace = true

[dev-dependencies]
ic-http-certification = "2.2.0"
candid_parser.workspace = true
anyhow.workspace = true
ic-response-verification-test-utils = { git = "https://github.com/dfinity/response-verification.git", rev = "c3390a34bbd8582840ccc9deeebd6f08135b7c4f" }
ic-certification-testing = { git = "https://github.com/dfinity/response-verification.git", rev = "c3390a34bbd8582840ccc9deeebd6f08135b7c4f" }
ic-crypto-tree-hash = { git = "https://github.com/dfinity/ic.git", rev = "1290256484f59c3d950c5e9a098e97383b248ad6" }

2 changes: 1 addition & 1 deletion src/canisters/frontend/ic-certified-assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ pub fn post_upgrade(stable_state: StableState, args: Option<AssetCanisterArgs>)

#[test]
fn candid_interface_compatibility() {
use candid::utils::{service_compatible, CandidSource};
use candid_parser::utils::{service_compatible, CandidSource};
use std::path::PathBuf;

candid::export_service!();
Expand Down
43 changes: 21 additions & 22 deletions src/canisters/frontend/ic-certified-assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ use crate::types::{
};
use crate::url_decode::{url_decode, UrlDecodeError};
use candid::{Nat, Principal};
use ic_certification_testing::CertificateBuilder;
use ic_crypto_tree_hash::Digest;
use ic_response_verification::ResponseVerificationError;
use ic_response_verification_test_utils::{
base64_encode, create_canister_id, get_current_timestamp, CanisterData, CertificateBuilder,
CertificateData,
base64_encode, create_canister_id, get_current_timestamp,
};
use serde_bytes::ByteBuf;
use std::collections::HashMap;
Expand All @@ -34,21 +33,20 @@ pub fn verify_response(
state: &State,
request: &HttpRequest,
response: &HttpResponse,
) -> Result<bool, ResponseVerificationError> {
) -> anyhow::Result<bool> {
let mut response = response.clone();
let current_time = get_current_timestamp();
let canister_id = create_canister_id("rdmx6-jaaaa-aaaaa-aaadq-cai");
let min_requested_verification_version = request.get_certificate_version();

// inject certificate into IC-Certificate header with 'certificate=::'
let (_cert, root_key, cert_cbor) =
CertificateBuilder::new(CertificateData::CanisterData(CanisterData {
canister_id,
certified_data: Digest(state.root_hash()),
}))
.with_time(current_time)
.build();
let replacement_cert_value = base64_encode(&cert_cbor);
let data = CertificateBuilder::new(
&canister_id.to_string(),
Digest(state.root_hash()).as_bytes(),
)?
.with_time(current_time)
.build()?;
let replacement_cert_value = base64_encode(&data.cbor_encoded_certificate);
let (_, header_value) = response
.headers
.iter_mut()
Expand All @@ -60,26 +58,27 @@ pub fn verify_response(
);

// actual verification
let request = ic_response_verification::types::Request {
let request = ic_http_certification::http::HttpRequest {
method: request.method.clone(),
url: request.url.clone(),
headers: request.headers.clone(),
body: request.body[..].into(),
};
let response = ic_response_verification::types::Response {
let response = ic_http_certification::http::HttpResponse {
status_code: response.status_code,
headers: response.headers,
body: response.body[..].into(),
};
ic_response_verification::verify_request_response_pair(
Ok(ic_response_verification::verify_request_response_pair(
request,
response,
canister_id.as_ref(),
current_time,
MAX_CERT_TIME_OFFSET_NS,
&root_key,
&data.root_key,
min_requested_verification_version.try_into().unwrap(),
)
.map(|res| res.passed)
.map(|res| res.response.is_some())?)
}

fn certified_http_request(state: &State, request: HttpRequest) -> HttpResponse {
Expand Down Expand Up @@ -962,7 +961,7 @@ fn uses_streaming_for_multichunk_assets() {
.http_request_streaming_callback(StreamingCallbackToken {
key: "/index.html".to_string(),
content_encoding: "identity".to_string(),
index: Nat::from(1),
index: Nat::from(1u8),
sha256: None,
})
.unwrap_err(),
Expand Down Expand Up @@ -1005,7 +1004,7 @@ fn get_and_get_chunk_for_multichunk_assets() {
.get_chunk(GetChunkArg {
key: "/index.html".to_string(),
content_encoding: "identity".to_string(),
index: Nat::from(1),
index: Nat::from(1u8),
sha256: chunk_0.sha256,
})
.unwrap();
Expand All @@ -1017,7 +1016,7 @@ fn get_and_get_chunk_for_multichunk_assets() {
.get_chunk(GetChunkArg {
key: "/index.html".to_string(),
content_encoding: "identity".to_string(),
index: Nat::from(1),
index: Nat::from(1u8),
sha256: None,
})
.unwrap_err(),
Expand Down Expand Up @@ -3347,7 +3346,7 @@ mod validate_commit_proposed_batch {
let time_now = 100_000_000_000;

match state.validate_commit_proposed_batch(CommitProposedBatchArguments {
batch_id: 1.into(),
batch_id: 1u8.into(),
evidence: Default::default(),
}) {
Err(err) if err.contains("batch not found") => (),
Expand All @@ -3356,7 +3355,7 @@ mod validate_commit_proposed_batch {

match state.commit_proposed_batch(
CommitProposedBatchArguments {
batch_id: 1.into(),
batch_id: 1u8.into(),
evidence: Default::default(),
},
time_now,
Expand Down

0 comments on commit 1263da5

Please sign in to comment.