mrene / minidsp-rs

MiniDSP Controller
http://minidsp-rs.pages.dev
Apache License 2.0
113 stars 15 forks source link

CORS Header for REST API #571

Closed kettenbach-it closed 1 year ago

kettenbach-it commented 1 year ago

Hi Mathieu,

I started playing around with Angular to build a web ui (SPA) for the MiniDSP Flex. Thanks to your openapi.json and openapi-generator, this is pretty easy. Thanks for that good work!

I have a request: would it be possible to have the API server send a CORS header: Access-Control-Allow-Origin: *

Otherwise, a browser with an SPA will deny access to the API and awkward constructions such as a proxy are required, which significantly degrade the UX of the ui-project.

Best regards Volker

kettenbach-it commented 1 year ago

I fixed it. Please merge PR https://github.com/mrene/minidsp-rs/pull/573

mrene commented 1 year ago

I see a few options for this:

kettenbach-it commented 1 year ago
mrene commented 1 year ago

I can look at adding cors options to the configuration.

Regarding CORS and security, I want to point out that security is always relative to a threat model. If we assume that you have direct network access to the device, then it is true that CORS does not bring any extra security, because it is not designed to mitigate against local attackers. When deploying systems, we make assumptions like "local network devices cannot be accessed by remote attackers because they are behind NAT and firewalls, etc."

The web, however, is designed for to load websites that are basically untrusted programs running in a sandbox. CORS is a mechanism by which browsers validate that third party servers are designed to accept requests from such untrusted websites. Without Access-Control-Allow-Origin, a website serving you advertisements (for example) would have the permission to call into your minidsp-rs instance and change your configuration, even if you do not expose its API to the internet, because the code runs in your browser, and your browser essentially acts as a reverse proxy allowing the third party website access to your local network resources. (Of course it needs to know the local IP, and some browsers mitigate against this because it's been used to exploit vulnerabilites in home routers and IOT devices with poor security, but it's a possible attack surface).

In the non-hypothetical real world this would most likely happen due to some phishing attempt, and while your home theather system might not be a valuable target for a sophisticated cyber attack, it's nice to have some same defaults so that things aren't completely wide opened, and to let users control these parameters so they can make informed decisions about the security of their networks.

This being said, this is a hobby project that doesn't have much resources, so the features around security are pretty limited, but at least there are sane defaults, the API is not exposed to the local network in the default configuration, etc.

Thanks for coming to my TED talk :sweat_smile:

PS: Mobile application frameworks that wrap web-based application code usually have a native way to bypass CORS altogether

kettenbach-it commented 1 year ago

I can look at adding cors options to the configuration.

Thx!