Announcing the HTTP certification library for Rust

Hello ICP builders :astronaut:

Nathan here from the Trust team :wave: Today I’m happy to announce the HTTP certification library for Rust!

Background

As a quick reminder, HTTP certification is a procedure that we follow in canisters to serve HTTP responses through query calls in a secure way.

Query calls are executed by a single replica node and do not go through consensus. This makes them fast, but insecure. Update calls are executed by all replica nodes on a subnet and go through consensus. This makes them slow, but secure.

Certification involves pre-calculating responses, putting them through consensus, and generating a certificate to prove that they have gone through consensus. These certificates are served alongside the pre-calculated responses, allowing clients to verify that the responding replica node has not maliciously altered the response.

HTTP certification is a more advanced version of this procedure that is used specifically for HTTP responses that will be received by HTTP clients, such as a browser. You can find out more about this in my previous post about response verification v2.

The library

The most common usage of HTTP certification today is through the beloved asset canister. To bring this functionality into their own canisters, developers previously had to copy/paste and maintain a lot of complex code from the asset canister. Now developers can leverage this code in a reusable way and build more flexibly using the HTTP certification library.

With this library, developers will be able to:

  • Serve certified assets from the same canister as their primary “backend” canister
  • Embed assets directly into a canister’s WASM, instead of uploading them at runtime
  • Create custom routing logic, such as:
    • Serving 404 pages in multi-page apps
    • Serving multiple frontends from the same canister
  • Certify more complex caching or streaming scenarios

As part of this release, we’ve published a user guide for the library along with example projects and guides demonstrating how to use the library to create an HTTP-compatible JSON API or serve static assets.

The library is available on crates.io, with the docs available on docs.rs and the source code available on github.

What’s next?

If the static asset example is too low-level, don’t worry. There’s another library in the works - the asset certification library will expose a higher-level interface purpose built for certifying and serving static assets in the simplest way possible. Stay tuned and you can expect to see the asset certification library in the coming months.

Feel free to reach out if you have any questions, suggestions, or issues with the library, I’ll be happy to help and I look forward to seeing the cool things that will be built with these libraries :rocket::new_moon:

27 Likes

Great work!

I believe the “single canister solution” (a canister that functions as a frontend server and a backend) is an important step towards microservices that are fully owned by the user.

It is convenient to have a single wasm. Users will be able to use a browser to create a canister, install some service wasm, manage and upgrade etc. without giving up control or ownership to any other party

5 Likes

Great work, can’t wait to give this a try! :heart_eyes:

3 Likes

@NathanosDev

Can we expect that these related crates will be maintained and what is the outlook for the Rust suite of certification crates?

ic_certified_assets
ic_certified_map
ic_certification

ic-certified-assets will likely be replaced at least partially by ic-asset-certification in the asset canister but I can’t speak for the exact plans that the SDK team has there.

ic-asset-certification is still in progress and will build on top of ic-http-certification.

ic-certified-map isa subset of ic-certification now and will be deprecated once some newer parts of ic-certified-map are merged into ic-certification.

ic-certification is used as the base for ic-http-certification so it will continue to be maintained.

3 Likes

Good to know

@Severin Could you perhaps give an update and outlook on ic-certified-assets crate and the ic-asset-certification module

ic-certified-assets is basically the asset canister that ships with dfx. The way it is structured does IMO not make it suited for any form of reusability. I plan to migrate it to ic-asset-certification as much as possible once that’s available. With that, I don’t think there would be much value to ic-certified-assets anymore, especially with how it is structured today (I think it doesn’t really allow reuse of anything useful).

While we have not discussed this in the team I honestly don’t really see a way forward to make ic-certified-assets a useful crate. What I could see is a crate that simplifies the use of the batch upload interface since one can use dfx to sync via that interface.

@Samer what do you think? Do you see a hole that ic-certified-assets could fill? Or what do you get as a consumer of it today?

3 Likes

Great work, @NathanosDev!
I have 2 questions: are there plans to bring this feature to other languages as well, and how much effort do you expect this to be?
Regarding serving frontend and backend from a single canister: I might be missing something here, but isn’t that already possible today (modulo the certification)? Here’s an example: GitHub - fxgst/azle-react: One-click dev env for developing canisters on ICP with JS/TS and a React frontend
Thanks!

1 Like

Yes, I’m especially interested the state_machine api that allows fast async chunked uploads with icx-asset. The api could also be consumed from browser.

Good to know where things stand. I have not made any decisions yet, but exploring the possibilities and available tools to create canisters that contain their frontend inside the wasm and serve it via certified query.

In Rust, we have the include_dir crate which works well for embedding assets in the wasm.

Every canister developer should be able to add a couple lines of code and the canister now implements the http_request and related endpoints and serves certified queries for an ‘onboard’ frontend.

static ASSET_DIR: Dir<'_> = include_dir!("src/frontend/build");

fn init() {
    init_frontend_assets!()
}

are there plans to bring this feature to other languages as well

There was BNT-13 for the community to bring some support for Motoko but I’m unsure of the current status.

For TypeScript/Azle, ideally, Azle would add support for using WASM modules so that we can use wasm-bindgen to generate a TypeScript version without needing to write it again from scratch.

In the longer term, supporting the WASM component model would make it much easier for libraries to be shared with other languages and I’m cautious about spending time writing libraries in other languages with the hope that we’ll have the component model sooner than later.

how much effort do you expect this to be?

The hardest thing about writing the library was trying to get the right interface design. There’s a lot of complexity that this library is trying to hide. I made multiple attempts before settling on the current design and I’m still not sure if I did it right yet :sweat_smile:

So if someone was to transpose the library into a new language and follow the same patterns then I think it would take significantly less time, but there’s still a lot to learn in terms of understanding what’s going on under the hood. I suspect that someone already familiar with the protocol could probably transpose the library in several weeks.

Regarding serving frontend and backend from a single canister: I might be missing something here, but isn’t that already possible today (modulo the certification)?

You’re right, the difference with the library is the certification. I’ll update the original post to say: “Serve certified assets from the same canister as their primary “backend” canister”.

Having said that, serving uncertified assets (or any uncertified query response from the IC for that matter) is extremely dangerous. It would allow a malicious replica to respond with anything it likes and expose a virtually unlimited number of attack vectors, depending on the canister being targeted. Disregarding certification reduces the security of your static asset serving down to the same level as a single replica subnet.

2 Likes

Every canister developer should be able to add a couple lines of code and the canister now implements the http_request and related endpoints and serves certified queries for an ‘onboard’ frontend.

I have the same vision for the ic-asset-certification library. The ic-http-certification library is a lower-level building block that allows for full flexibility, while ic-asset-certification aims to be much higher level and require as little involvement from the developer as possible.

1 Like