Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: rewrite in rust #1577

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
dc2ee9d
Cli: many/most API calls, signature verification
svix-onelson Dec 19, 2024
dbcfbce
Cli: add support for `listen` command
svix-onelson Dec 19, 2024
dfabfc9
CLI: rename command, `svix-signature` -> `signature`
svix-onelson Dec 19, 2024
9f816cf
CLI: add trailing newline to json output
svix-onelson Dec 19, 2024
86f2a4a
CLI: add `SVIX_DEBUG_URL` fallback
svix-onelson Dec 19, 2024
d57bae6
CLI: customize the JSON color styler to have less "white"
svix-onelson Dec 19, 2024
00908c1
CLI: remove default for server url
svix-onelson Dec 19, 2024
a6caf67
CLI: cargo update, remove unused deps in manifest
svix-onelson Dec 19, 2024
db3cef9
CLI: switch to `native-tls` (non-vendored) for `tokio-tungstenite`
svix-onelson Dec 19, 2024
ba9ab41
CLI: use path dep for rust SDK
svix-onelson Dec 19, 2024
d2cc040
CLI: generate a new relay token on the fly if configured is in use
svix-onelson Dec 19, 2024
4b65259
CLI: implement `open` command
svix-onelson Dec 19, 2024
0ef53d6
Libs(Rust): add `verify_ignoring_timestamp` akin to Go lib
svix-onelson Dec 20, 2024
a740f27
CLI: make `signature verify` use `verify_ignoring_timestamp`
svix-onelson Dec 20, 2024
34cfa1c
CLI: simplify authentication logout client usage
svix-onelson Dec 20, 2024
8a305e3
CLI: mods then consts
svix-onelson Dec 20, 2024
93dcb45
CLI: update to latest codegen, add message-attempt support (and more)
svix-onelson Dec 20, 2024
3bcc036
CLI: remove `import`/`export`
svix-onelson Dec 20, 2024
d0fc43b
CLI: code review fixups
svix-onelson Dec 23, 2024
9c0b41e
CLI: add to `bump_version.js`
svix-onelson Dec 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions rust/src/webhooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ impl Webhook {
}

pub fn verify<HM: HeaderMap>(&self, payload: &[u8], headers: &HM) -> Result<(), WebhookError> {
self.verify_inner(payload, headers, /* enforce_tolerance */ true)
}

pub fn verify_ignoring_timestamp<HM: HeaderMap>(
&self,
payload: &[u8],
headers: &HM,
) -> Result<(), WebhookError> {
self.verify_inner(payload, headers, /* enforce_tolerance */ false)
}

fn verify_inner<HM: HeaderMap>(
&self,
payload: &[u8],
headers: &HM,
enforce_tolerance: bool,
) -> Result<(), WebhookError> {
let msg_id = Self::get_header(headers, SVIX_MSG_ID_KEY, UNBRANDED_MSG_ID_KEY, "id")?;
let msg_signature = Self::get_header(
headers,
Expand All @@ -72,7 +89,9 @@ impl Webhook {
)
.and_then(Self::parse_timestamp)?;

Self::verify_timestamp(msg_ts)?;
if enforce_tolerance {
Self::verify_timestamp(msg_ts)?;
}

let versioned_signature = self.sign(msg_id, msg_ts, payload)?;
let expected_signature = versioned_signature
Expand Down Expand Up @@ -317,22 +336,37 @@ mod tests {
let payload = br#"{"email":"[email protected]","username":"test_user"}"#;
let wh = Webhook::new(&secret).unwrap();

let signature = wh
.sign(msg_id, OffsetDateTime::now_utc().unix_timestamp(), payload)
.unwrap();

let mut headers = get_svix_headers(msg_id, &signature);
// Checks that timestamps that are in the future or too old are rejected by
// `verify` but okay for `verify_ignoring_timestamp`.
for ts in [
OffsetDateTime::now_utc().unix_timestamp() - (super::TOLERANCE_IN_SECONDS + 1),
OffsetDateTime::now_utc().unix_timestamp() + (super::TOLERANCE_IN_SECONDS + 1),
] {
let signature = wh.sign(msg_id, ts, payload).unwrap();
let mut headers = get_svix_headers(msg_id, &signature);
headers.insert(
super::SVIX_MSG_TIMESTAMP_KEY,
ts.to_string().parse().unwrap(),
);

assert!(wh.verify(payload, &headers,).is_err());
// Timestamp tolerance is not considered in this case.
assert!(wh.verify_ignoring_timestamp(payload, &headers,).is_ok());
}

let ts = OffsetDateTime::now_utc().unix_timestamp();
let signature = wh.sign(msg_id, ts, payload).unwrap();
let mut headers = get_svix_headers(msg_id, &signature);
headers.insert(
super::SVIX_MSG_TIMESTAMP_KEY,
// Timestamp mismatch!
(ts + 1).to_string().parse().unwrap(),
);

// Both versions should reject the timestamp if it's not the same one used to
// produce the signature.
assert!(wh.verify(payload, &headers,).is_err());
assert!(wh.verify_ignoring_timestamp(payload, &headers,).is_err());
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions svix-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
Loading