-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e3059d
commit c8c69b7
Showing
15 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,63 @@ | ||
# Response Verification | ||
|
||
Response verification on the [Internet Computer](https://dfinity.org) is the process of | ||
verifying that a canister response from a replica has gone through consensus with other replicas | ||
hosting the same canister. | ||
|
||
This package encapsulates the protocol for such verification. It is used by the | ||
[Service Worker](https://github.com/dfinity/ic/tree/master/typescript/service-worker) and | ||
[ICX Proxy](https://github.com/dfinity/ic/tree/master/rs/boundary_node/icx_proxy) and may be | ||
used by other implementations of the | ||
[HTTP Gateway Protocol](https://internetcomputer.org/docs/current/references/ic-interface-spec/#http-gateway) | ||
in the future. These implementations can also be reviewed to see working integrations. | ||
|
||
## Usage | ||
|
||
```javascript | ||
import initResponseVerification, { | ||
verifyRequestResponsePair, | ||
ResponseVerificationError, | ||
ResponseVerificationErrorCode, | ||
} from "@dfinity/response-verification"; | ||
|
||
// this is necessary for web, but not for NodeJS consumers | ||
await initResponseVerification(); | ||
|
||
try { | ||
const result = verifyRequestResponsePair( | ||
request, | ||
response, | ||
canister_id, | ||
current_time_ns, | ||
max_cert_time_offset_ns, | ||
fromHex(IC_ROOT_KEY) | ||
); | ||
|
||
// do something with the result | ||
// `result.passed` will be true if verification succeeds, false otherwise, and | ||
// `result.response` will contain the certified response object if verification was successful. | ||
} catch (error) { | ||
if (error instanceof ResponseVerificationError) { | ||
switch (error.code) { | ||
case ResponseVerificationErrorCode.MalformedCbor: | ||
// the cbor returned from the replica was malformed. | ||
// ... | ||
break; | ||
|
||
case ResponseVerificationErrorCode.MalformedCertificate: | ||
// the certificate returned from the replica was malformed. | ||
// ... | ||
break; | ||
|
||
// Other error cases... | ||
} | ||
} | ||
} | ||
``` | ||
|
||
See the following for working examples: | ||
- [Web](https://github.com/dfinity/response-verification/tree/main/examples/web) | ||
- [Service Worker](https://github.com/dfinity/response-verification/tree/main/examples/service-worker) | ||
- [NodeJS](https://github.com/dfinity/response-verification/tree/main/examples/nodejs) | ||
|
||
Note that when bundling for a service worker with Webpack. The `target` property must be set to `webworker`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,96 @@ | ||
pub type CelParserResult<T = ()> = Result<T, CelParserError>; | ||
|
||
/// CEL expression parsing error. | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum CelParserError { | ||
/// The CEL parser encountered an unsupported CEL function. | ||
#[error(r#""{0}" is not a supported CEL function, only default_certification is currently supported"#)] | ||
UnrecognizedFunction(String), | ||
|
||
/// The CEL parser expected a parameter at a position, but none was found. | ||
#[error(r#"Parameter at position {parameter_position:?} for function {function_name:?} is missing, expected {parameter_name:?} with type {parameter_type:?}"#)] | ||
MissingFunctionParameter { | ||
/// The name of the function with a missing parameter. | ||
function_name: String, | ||
/// The expected type of the missing parameter. | ||
parameter_type: String, | ||
/// The expected name of the missing parameter. | ||
parameter_name: String, | ||
/// The expected position of the missing parameter. | ||
parameter_position: u8, | ||
}, | ||
|
||
/// The CEL parser expected a parameter to have a different type than the one it found. | ||
#[error(r#"Parameter at position {parameter_position:?} for function {function_name:?} has the wrong type, expected {parameter_name:?} {expected_parameter_type:?} found {found_parameter_type:?}"#)] | ||
IncorrectFunctionParameterType { | ||
/// The name of the function with an unexpected parameter type. | ||
function_name: String, | ||
/// The name of the parameter with the unexpected type. | ||
parameter_name: String, | ||
/// The expected type of the parameter. | ||
expected_parameter_type: String, | ||
/// The actual type of the parameter. | ||
found_parameter_type: String, | ||
/// The position of the parameter. | ||
parameter_position: u8, | ||
}, | ||
|
||
/// The CEL parser expected a node to have a different type than the one it found. | ||
#[error(r#"Expected node with name {node_name:?} to have type {expected_type:?}, found {found_type:?}"#)] | ||
UnexpectedNodeType { | ||
/// The name of the node with an unexpected type. | ||
node_name: String, | ||
/// The expected type of the node. | ||
expected_type: String, | ||
/// The actual type of the node. | ||
found_type: String, | ||
}, | ||
|
||
/// The CEL parser expected a node to have a different name than the one it found. | ||
#[error(r#"Expected node with type {node_type:?} to have name {expected_name:?}, found {found_name:?}"#)] | ||
UnexpectedNodeName { | ||
/// The type of the node with an unexpected name. | ||
node_type: String, | ||
/// The expected name of the node. | ||
expected_name: String, | ||
/// The actual name of hte node. | ||
found_name: String, | ||
}, | ||
|
||
/// The CEL parser expected an object to have a property with a particular name, but none was found. | ||
#[error(r#"Expected object {object_name:?} to have property {expected_property_name:?}"#)] | ||
MissingObjectProperty { | ||
/// The name of the object with a missing property. | ||
object_name: String, | ||
/// The expected property name. | ||
expected_property_name: String, | ||
}, | ||
|
||
/// The CEL parser encountered an extraneous property on the request certification's CEL object. | ||
#[error(r#"The request_certification object must only specify one of the no_request_certification or request_certification properties, not both"#)] | ||
ExtraneousRequestCertificationProperty, | ||
|
||
/// The CEL parser expected to find a property on the request certification's CEL object, but none was found. | ||
#[error(r#"The request_certification object must specify at least one of the no_request_certification or request_certification properties"#)] | ||
MissingRequestCertificationProperty, | ||
|
||
/// The CEL parser encountered an extraneous property on the response certification's CEL object. | ||
#[error(r#"The response_certification object must only specify one of the certified_response_headers or response_header_exclusions properties, not both"#)] | ||
ExtraneousResponseCertificationProperty, | ||
|
||
/// The CEL parser expected to find a property on the response certification's CEL object, but none was found. | ||
#[error(r#"The response_certification object must specify at least one of the certified_response_headers or response_header_exclusions properties"#)] | ||
MissingResponseCertificationProperty, | ||
|
||
/// The CEL parser encountered an extraneous property on the certification's CEL object. | ||
#[error(r#"The ValidationArgs parameter must only specify one of the no_certification or certification properties, not both"#)] | ||
ExtraneousValidationArgsProperty, | ||
|
||
/// The CEL parser expected to find a property on the certification's CEL object, but none was found. | ||
#[error(r#"The ValidationArgs parameter must specify at least one of the no_certification or certification properties"#)] | ||
MissingValidationArgsProperty, | ||
|
||
/// The CEL parser encountered a syntax error while parsing the CEL expression. Using the "debug" feature flag can help to debug these syntax errors. | ||
#[error(r#"Cel Syntax Expception: {0}"#)] | ||
CelSyntaxException(String), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
//! Public types used for response verification. | ||
/// Types to represent parsed CEL expressions. | ||
pub mod certification; | ||
pub use certification::*; | ||
|
||
/// Types to represent response objects used for certification. | ||
pub mod request; | ||
pub use request::*; | ||
|
||
/// Types to represent request objects used for certification. | ||
pub mod response; | ||
pub use response::*; | ||
|
||
/// Types to represent the result of verifying a request/response pair's certification. | ||
pub mod certification_result; | ||
pub use certification_result::*; |
11 changes: 11 additions & 0 deletions
11
packages/ic-response-verification/src/types/certification.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
/// Parsed request certification CEL expression parameters. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct RequestCertification { | ||
/// Request headers to include in certification. | ||
pub certified_request_headers: Vec<String>, | ||
/// Request query parameters to include in certification. | ||
pub certified_query_parameters: Vec<String>, | ||
} | ||
|
||
/// Parsed response certification CEL expression parameters. Can either include headers using | ||
/// [ResponseCertification::CertifiedHeaders] or exclude them using | ||
/// [ResponseCertification::HeaderExclusions]. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub enum ResponseCertification { | ||
/// Response headers to exclude from certification. | ||
HeaderExclusions(Vec<String>), | ||
/// Response headers to include in certification. | ||
CertifiedHeaders(Vec<String>), | ||
} | ||
|
||
/// Parsed request/response pair certification CEL expression. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct Certification { | ||
/// Optional rust representation of the request certification CEL expression parameters. | ||
pub request_certification: Option<RequestCertification>, | ||
/// Rust representation of the response certification CEL expression parameters. | ||
pub response_certification: ResponseCertification, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.