-
Notifications
You must be signed in to change notification settings - Fork 188
CLI: rewrite in rust #1577
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
Merged
Merged
CLI: rewrite in rust #1577
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
dc2ee9d
Cli: many/most API calls, signature verification
svix-onelson dbcfbce
Cli: add support for `listen` command
svix-onelson dfabfc9
CLI: rename command, `svix-signature` -> `signature`
svix-onelson 9f816cf
CLI: add trailing newline to json output
svix-onelson 86f2a4a
CLI: add `SVIX_DEBUG_URL` fallback
svix-onelson d57bae6
CLI: customize the JSON color styler to have less "white"
svix-onelson 00908c1
CLI: remove default for server url
svix-onelson a6caf67
CLI: cargo update, remove unused deps in manifest
svix-onelson db3cef9
CLI: switch to `native-tls` (non-vendored) for `tokio-tungstenite`
svix-onelson ba9ab41
CLI: use path dep for rust SDK
svix-onelson d2cc040
CLI: generate a new relay token on the fly if configured is in use
svix-onelson 4b65259
CLI: implement `open` command
svix-onelson 0ef53d6
Libs(Rust): add `verify_ignoring_timestamp` akin to Go lib
svix-onelson a740f27
CLI: make `signature verify` use `verify_ignoring_timestamp`
svix-onelson 34cfa1c
CLI: simplify authentication logout client usage
svix-onelson 8a305e3
CLI: mods then consts
svix-onelson 93dcb45
CLI: update to latest codegen, add message-attempt support (and more)
svix-onelson 3bcc036
CLI: remove `import`/`export`
svix-onelson d0fc43b
CLI: code review fixups
svix-onelson 9c0b41e
CLI: add to `bump_version.js`
svix-onelson 3fa47ef
CLI: add CI for lint, security, reformat with nightly
svix-onelson 8a8ff36
CLI: add license, keywords, category to manifest
svix-onelson d4ed432
CLI: grant exception for EPL-2.0 license in colored_json
svix-onelson 2261987
CLI: better support config dir lookup on Windows
svix-onelson 603e4ee
CLI: trigger CLI lints when `rust/` changes
svix-onelson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
name: CLI Lint | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'svix-cli/**' | ||
- 'rust/**' | ||
- '.github/workflows/cli-lint.yml' | ||
pull_request: | ||
paths: | ||
- 'svix-cli/**' | ||
- 'rust/**' | ||
- '.github/workflows/cli-lint.yml' | ||
- 'openapi.json' | ||
|
||
# When pushing to a PR, cancel any jobs still running for the previous head commit of the PR | ||
concurrency: | ||
# head_ref is only defined for pull requests, run_id is always unique and defined so if this | ||
# workflow was not triggered by a pull request, nothing gets cancelled. | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
check-fmt: | ||
name: Check formatting | ||
runs-on: ubuntu-24.04 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: dtolnay/rust-toolchain@nightly | ||
with: | ||
components: rustfmt | ||
|
||
- name: rustfmt | ||
run: cargo fmt -- --check | ||
working-directory: svix-cli | ||
|
||
test-versions: | ||
name: CLI Lint | ||
runs-on: ubuntu-24.04 | ||
strategy: | ||
matrix: | ||
rust: [stable, beta] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Regen openapi libs | ||
run: | | ||
yarn | ||
./regen_openapi.sh | ||
|
||
- uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: ${{ matrix.rust }} | ||
components: clippy | ||
|
||
- uses: Swatinem/rust-cache@v2 | ||
with: | ||
workspaces: "svix-cli -> target" | ||
# only save the cache on the main branch | ||
# cf https://github.com/Swatinem/rust-cache/issues/95 | ||
save-if: ${{ github.ref == 'refs/heads/main' }} | ||
# include relevant information in the cache name | ||
prefix-key: "cli-${{ matrix.rust }}" | ||
|
||
- uses: taiki-e/install-action@nextest | ||
|
||
- name: Clippy | ||
run: cargo clippy --all-targets --all-features -- -D warnings | ||
working-directory: svix-cli | ||
|
||
- name: Run tests | ||
run: cargo nextest run | ||
working-directory: svix-cli |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: CLI Security | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'svix-cli/**/Cargo.toml' | ||
- 'svix-cli/**/Cargo.lock' | ||
- '.github/workflows/cli-security.yml' | ||
pull_request: | ||
paths: | ||
- 'rust/**/Cargo.toml' | ||
- 'rust/**/Cargo.lock' | ||
- '.github/workflows/cli-security.yml' | ||
|
||
jobs: | ||
security_audit: | ||
runs-on: ubuntu-24.04 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: EmbarkStudios/cargo-deny-action@v2 | ||
with: | ||
manifest-path: svix-cli/Cargo.toml |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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] | ||
|
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
target/ |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
imports_granularity = "Crate" | ||
group_imports = "StdExternalCrate" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.