Skip to content

refactor: replace time::Duration with std::time::Duration in public API#79

Merged
AkshatM merged 1 commit intocloudflare:mainfrom
valkum:stdtime-in-pub-api
Apr 22, 2026
Merged

refactor: replace time::Duration with std::time::Duration in public API#79
AkshatM merged 1 commit intocloudflare:mainfrom
valkum:stdtime-in-pub-api

Conversation

@valkum
Copy link
Copy Markdown
Contributor

@valkum valkum commented Apr 21, 2026

Closes #74

This updates the public API surfaced via web_bot_auth::message_signatures::MessageSigner::generate_signature_headers_content and web_bot_auth::message_signatures::SignatureTiming to use std::time::Duration instead. The time crate is now an implementation detail.
This change makes usage with other time crates easier. You do not need to pull in the time crate or convert from a different Duration into time::Duration (direct or via std::time::Duration)

I tested this with https://github.com/cloudflare/web-bot-auth/tree/main/examples/signature-agent-card-and-registry and did not find any errors. I am looking forward to better test instructions.

This is a breaking change and requires a minor version bump.

Copy link
Copy Markdown
Contributor

@AkshatM AkshatM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some comments, but overall LGTM. Similar to you, I tested the example worker and was pleased to see it working as expected - it directly tests Duration by generating headers in the response containing an expiry time.

Once you make the changes to use unisgned_abs(), I am happy to approve.

let generation = UtcDateTime::now();
let (base_representation, _) = self.parsed.base.into_ascii()?;
let generation = UtcDateTime::now() - generation;
let generation_nanos = (UtcDateTime::now() - generation).whole_nanoseconds();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could just do:

Suggested change
let generation_nanos = (UtcDateTime::now() - generation).whole_nanoseconds();
let generation_nanos = (UtcDateTime::now() - generation).usigned_abs()

and realize the same gains without first converting to whole_nanoseconds and recasting to std::time::Duration using Duration::from_nanos.

SignatureTiming (the sole user of generation) is not involved in anything cryptographic. It's just a convenient metric to expose how long each constituent operation took for anyone interested. So it doesn't need a very high bar of safety to begin with.

It is possible to induce negative time in generation (UtcDateTime::now() relies on a wall clock time, rather than a monotonic clock, and someone could certainly reset the wall-clock between measurements), but, seeing as it's not load-bearing, I don't think we need to worry about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks. Valid point. Was doing this on the train and tried to find a method that turns time::Duration into std::time::Duration but must have overlooked that one.

.map_err(ImplementationError::FailedToVerify)
.map(|()| SignatureTiming {
generation,
verification: UtcDateTime::now() - verification,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in https://github.com/cloudflare/web-bot-auth/pull/79/changes#r3118154134, you could just do

Suggested change
verification: UtcDateTime::now() - verification,
verification: (UtcDateTime::now() - verification).unsighed_abs(),

and call it a day.

std::time::Duration has less overhead when using the crate, so it makes
more sense to stick to Duration in public APIs.

time::Duration was used because the time crate is able to run in
wasm32-unknown-unknown on Cloudflare Workers.
The exact reason is limited to SystemTime/Instant, used in
duration_since.

We still use the time crate now to work around the issue of
duration_since, but merely as an implementation detail.

time::Duration implements Sub<std::time::Duration>
@valkum valkum force-pushed the stdtime-in-pub-api branch from f41015c to 261bbab Compare April 22, 2026 10:47
@AkshatM AkshatM merged commit ff02d95 into cloudflare:main Apr 22, 2026
3 checks passed
AkshatM added a commit to AkshatM/web-bot-auth that referenced this pull request Apr 22, 2026
- [Implement `std:error:Error` trait for
  `ImplementationError`](cloudflare#78)

- [refactor: replace time::Duration with std::time::Duration in public
  API](cloudflare#79).

The last makes a breaking change to our APIs, and so mandates a minor
version bump per our existing convention.
@AkshatM AkshatM mentioned this pull request Apr 22, 2026
AkshatM added a commit that referenced this pull request Apr 22, 2026
- [Implement `std:error:Error` trait for
  `ImplementationError`](#78)

- [refactor: replace time::Duration with std::time::Duration in public
  API](#79).

The last makes a breaking change to our APIs, and so mandates a minor
version bump per our existing convention.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rust: reconsider dependency on time crate in public APIs

2 participants