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

Add request validation #3

Merged
merged 4 commits into from
Jul 16, 2019
Merged

Add request validation #3

merged 4 commits into from
Jul 16, 2019

Conversation

thatportugueseguy
Copy link
Collaborator

@thatportugueseguy thatportugueseguy commented Jul 11, 2019

Builds on top of, and is blocked by #2.

Implemented request validation.

Renamed the config module due to conflict with Httpaf.Config

| _ -> Error ""

let validate_header_env_var optional_env_var header =
Option.value_map ~default:(Error "")
Copy link
Contributor

Choose a reason for hiding this comment

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

If you don't want to have details about the error, use Error ()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do care later down on line 50, when i return an error message and am coerced on line 49 to return also of type (event_name, string) result

Copy link
Contributor

Choose a reason for hiding this comment

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

empty string is not acceptable nonetheless, it should have relevant information

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not returning empty strings anymore


let validate_notification_type notification_type =
match notification_type with
| "push" -> Ok Push_event
| "pull_request" -> Ok Pull_request_event
| "check_suite" -> Ok CI_run_event
| _ -> Error "Notification type not accepted"
| _ -> Error ""

Choose a reason for hiding this comment

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

You are discarding information that we can later use for debug

Choose a reason for hiding this comment

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

Also, wouldn't you want to stop everything if something wrong with this header?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've made changes to accomodate @ygrek suggestions, so the code is simpler now and more straightforward. I'm not raising an exception, but no more operations will be done on the request after it's determined that the github event is unsupported.

let user_agent = extract_header validate_github_user_agent headers "User-agent" in
match notification_signature, user_agent, github_event with
| Ok _, Ok _, Ok _ -> github_event
| _, _, _ -> Error "Headers validation failed"

Choose a reason for hiding this comment

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

You could add info about why they failed here

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

| _ -> Error ""

let validate_header_env_var optional_env_var header =
Option.value_map ~default:(Error "")
Copy link
Contributor

Choose a reason for hiding this comment

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

empty string is not acceptable nonetheless, it should have relevant information

let github_event = extract_header validate_notification_type headers "X-GitHub-Event" in
github_event
let validate_github_request_signature = validate_header_env_var Configuration.Env.github_sha1_signature in
Copy link
Contributor

Choose a reason for hiding this comment

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

this looks overcomplicated (with passing callbacks around) for no reason - simple pattern match will be enough

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

with exn ->
( match Headers.get headers "X-Github-Event" with
| Some event -> Error (Printf.sprintf "While parsing the %s payload: %s" event (Exn.to_string exn))
| None -> Error (Printf.sprintf "While parsing the payload: %s" (Exn.to_string exn)) )
Copy link
Contributor

Choose a reason for hiding this comment

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

remove duplication : sprintf "xxx %s payload" (match Headers.get with Some s -> s | None -> "")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👍

@thatportugueseguy thatportugueseguy changed the base branch from feat/parse-json to master July 16, 2019 08:22
@ygrek ygrek merged commit 2b4b2f0 into master Jul 16, 2019
@ygrek ygrek deleted the feat/add-request-validation branch July 31, 2019 05:31
yasunariw pushed a commit that referenced this pull request Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants