-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: added WIP apikey request guard #185
Draft
ErikBjare
wants to merge
1
commit into
master
Choose a base branch
from
dev/apikey
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/// On most systems we do not concern ourselves with local security [1], however on Android | ||
/// apps have their storage isolated yet ActivityWatch happily exposes its API and database | ||
/// through the HTTP API when the server is running. This could be considered a severe | ||
/// security flaw, and fixing it would significantly improve security on Android (as mentioned in [2]). | ||
/// | ||
/// Requiring an API key can also be useful in other scenarios where an extra level of security is | ||
/// desired. | ||
/// | ||
/// Based on the ApiKey example at [3]. | ||
/// | ||
/// [1]: https://docs.activitywatch.net/en/latest/security.html#activitywatch-is-only-as-secure-as-your-system | ||
/// [2]: https://forum.activitywatch.net/t/rest-api-supported-with-android-version-of-activity-watch/854/6?u=erikbjare | ||
/// [3]: https://api.rocket.rs/v0.4/rocket/request/trait.FromRequest.html | ||
use rocket::http::Status; | ||
use rocket::request::{self, FromRequest, Request}; | ||
use rocket::{Outcome, State}; | ||
|
||
use crate::config::AWConfig; | ||
|
||
struct ApiKey(Option<String>); | ||
|
||
#[derive(Debug)] | ||
enum ApiKeyError { | ||
BadCount, | ||
Missing, | ||
Invalid, | ||
} | ||
|
||
// TODO: Use guard on endpoints | ||
// TODO: Add tests for protected endpoints (important to ensure security) | ||
impl<'a, 'r> FromRequest<'a, 'r> for ApiKey { | ||
type Error = ApiKeyError; | ||
|
||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> { | ||
// TODO: How will this key be configured by the user? | ||
let config = request.guard::<State<AWConfig>>().unwrap(); | ||
match &config.apikey { | ||
None => Outcome::Success(ApiKey(None)), | ||
Some(apikey) => { | ||
// TODO: How will this header be set in the browser? | ||
let keys: Vec<_> = request.headers().get("x-api-key").collect(); | ||
match keys.len() { | ||
0 => Outcome::Failure((Status::BadRequest, ApiKeyError::Missing)), | ||
1 if apikey == keys[0] => Outcome::Success(ApiKey(Some(keys[0].to_string()))), | ||
1 => Outcome::Failure((Status::BadRequest, ApiKeyError::Invalid)), | ||
_ => Outcome::Failure((Status::BadRequest, ApiKeyError::BadCount)), | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ pub struct ServerState { | |
|
||
#[macro_use] | ||
mod util; | ||
mod auth; | ||
mod bucket; | ||
mod cors; | ||
mod export; | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a very convoluted way of expressing it.
Better to just have a single case for one and have the of statement inside a block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, just picked it straight from the example.