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

feat: Add allowed origins config #1408

Conversation

fredcarle
Copy link
Collaborator

@fredcarle fredcarle commented Apr 26, 2023

Relevant issue(s)

Resolves #1355

Description

This PR adds the option to set allowed origins for the HTTP API using the config file or a CLI flag.

Tasks

  • I made sure the code is well commented, particularly hard-to-understand areas.
  • I made sure the repository-held documentation is changed accordingly.
  • I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in tools/configs/chglog/config.yml).
  • I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ...

How has this been tested?

Unit tests and curl with allowedOrigins set to http://test.com

# this will result in an unsuccessful Preflight request
curl -i http://localhost:9181/api/v1/ping -H "Origin: http://no.com" -H "Access-Control-Request-Method: GET" -X OPTIONS

# this will result in an successful Preflight request
curl -i http://localhost:9181/api/v1/ping -H "Origin: http://test.com" -H "Access-Control-Request-Method: GET" -X OPTIONS

Specify the platform(s) on which this was tested:

  • MacOS

@fredcarle fredcarle added area/api Related to the external API component action/no-benchmark Skips the action that runs the benchmark. area/cli Related to the CLI binary labels Apr 26, 2023
@fredcarle fredcarle added this to the DefraDB v0.5.1 milestone Apr 26, 2023
@fredcarle fredcarle requested a review from a team April 26, 2023 16:11
@fredcarle fredcarle self-assigned this Apr 26, 2023
@AndrewSisley
Copy link
Contributor

Note: The default has been set to * which will allow all origins to send requests to the API. I feel like that's a good default for development but not for prod for obvious reasons. Do we want to be restrictive by default or open by default?

My vote would be for the opposite, and have it locked down by default. Forgetting (or not knowing about this) could be an expensive mistake. I also think that locked down by default is the norm (I remember having to mess with stuff like this a few times to enable CORS, but never to block it).

@codecov
Copy link

codecov bot commented Apr 26, 2023

Codecov Report

Merging #1408 (78e3de1) into develop (f0d7fe0) will decrease coverage by 0.05%.
The diff coverage is 80.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1408      +/-   ##
===========================================
- Coverage    72.07%   72.02%   -0.05%     
===========================================
  Files          185      185              
  Lines        18175    18185      +10     
===========================================
- Hits         13099    13098       -1     
- Misses        4043     4049       +6     
- Partials      1033     1038       +5     
Impacted Files Coverage Δ
cli/start.go 61.07% <66.66%> (+0.17%) ⬆️
config/config.go 73.30% <100.00%> (+0.05%) ⬆️

... and 6 files with indirect coverage changes

Copy link
Contributor

@AndrewSisley AndrewSisley left a comment

Choose a reason for hiding this comment

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

LGTM. Approving assuming that the default value conversation is resolved before merged :)

@fredcarle
Copy link
Collaborator Author

My vote would be for the opposite, and have it locked down by default. Forgetting (or not knowing about this) could be an expensive mistake. I also think that locked down by default is the norm (I remember having to mess with stuff like this a few times to enable CORS, but never to block it).

I'm happy with that 👍

README.md Outdated

When accessing DefraDB through a frontend interface, you may be confronted with a CORS error. That is because, by default, DefraDB will not have any allowed origins set. To specify which origins should be allowed to access your DefraDB endpoint, you can specify them when starting the database:
```shell
defradb start --allowedorigins=https://yourdomain.com
Copy link
Member

@shahzadlone shahzadlone Apr 26, 2023

Choose a reason for hiding this comment

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

suggestion:

How about just origins. Mostly because we don't have a blockedorigins or any other option like that. Also allowedorigins is less readable, perhaps allowedOrigins if you want to stick with the "allowed" word.

Suggested change
defradb start --allowedorigins=https://yourdomain.com
defradb start --origins=https://yourdomain.com

Copy link
Collaborator Author

@fredcarle fredcarle Apr 26, 2023

Choose a reason for hiding this comment

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

The thing with --origins is that it's a little vague and the http header is called With-Allowed-Origins. I feel like it makes more sense to have almost the full representation here as well. allowedOrigins could be nicer.

Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: I have a very minor preference for allowed-origins, but only if it we dont already camel case any other existing multi-word params/flags

Copy link
Member

Choose a reason for hiding this comment

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

I do like allowed-origins, would prefer that over allowedOrigins

Copy link
Contributor

@orpheuslummis orpheuslummis Apr 26, 2023

Choose a reason for hiding this comment

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

one idea is corsdomain, and perhaps alternatively using --api.corsdomain or http.corsdomain depending on which top-level key we want to represent the http system.

when determining defradb's parameters, we have to keep in mind that they have three points of determination: environment variables, config file, CLI command.

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'd rather we stay closer to what it represents. allowed-origins is quite good.

Copy link
Contributor

@orpheuslummis orpheuslummis Apr 26, 2023

Choose a reason for hiding this comment

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

so the fully qualified key from the config's perspective would be api.allowed-origins, so perhaps it can be --allowed-origins in the CLI but as an env. variable it would be DEFRA_API_ALLOWED_ORIGINS perhaps. i'm not sure yet about if it's necessary to quote such a key with a - in Yaml - it might have to be

[api]
"allowed-origins": ...

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 don't think it is according to what I see here https://www.cloudbees.com/blog/yaml-tutorial-everything-you-need-get-started

Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

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

LGTM

@fredcarle fredcarle force-pushed the fredcarle/feat/I1355-allowed-origin-config branch from 7f57249 to 78e3de1 Compare April 27, 2023 16:56
@fredcarle fredcarle merged commit 6c10804 into sourcenetwork:develop Apr 27, 2023
@fredcarle fredcarle deleted the fredcarle/feat/I1355-allowed-origin-config branch April 27, 2023 17:23
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
## Relevant issue(s)

Resolves sourcenetwork#1355 

## Description

This PR adds the option to set allowed origins for the HTTP API using
the config file or a CLI flag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/no-benchmark Skips the action that runs the benchmark. area/api Related to the external API component area/cli Related to the CLI binary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add configuration for allowed origins
4 participants