Skip to content

Commit d5f5e94

Browse files
Adding CORS back into the mix
Signed-off-by: Christian Wolf <[email protected]>
1 parent ca6794c commit d5f5e94

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

developer_manual/digging_deeper/rest_apis.rst

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,29 @@ Both provide a way to transmit data between the backend of the app in the Nextcl
9393
The following combinations of attributes might be relevant for various scenarios:
9494

9595
#. Plain frontend route: ``Controller`` class
96-
#. Plain Frontend with CRSF checks disabled: ``Controller`` class and ``#[NoCSRFRequired]`` attribute on the method
96+
#. Plain frontend with CRSF checks disabled: ``Controller`` class and ``#[NoCSRFRequired]`` attribute on the method
97+
#. REST route with CORS enabled: ``Controller`` class and ``#[CORS]`` and ``#[NoCSRFRequired]`` attributes on the route
9798
#. OCS-based route: ``OCSController`` class
98-
#. OCS-based route with CSRF disabled: ``OCSController`` class and ``#[NoCSRFRequired]`` attribute on the method
99+
#. OCS-based route with CORS enabled: ``OCSController`` class and ``#[CORS]`` attribute on the method
99100

100101
.. warning::
101102
Adding the ``#[NoCRSFRequired]`` attribute imposes a security risk.
102103
You should not add this to your controller methods unless you understand the implications and be sure that you absolutely need the attribute.
103104

105+
.. warning::
106+
Adding the attribute ``#[CORS]`` alone is not sufficient to allow access using CORS.
107+
The CSRF checker will typically fail, so enabling CORS enforces you to disable the CSRF checker as well.
108+
Although the disabled CSRF checker in itself is a security issue to consider, adding CORS opens up this even more.
109+
You should make sure, that you understand the implications completely when enabling CORS and do so only when there is a good use case.
110+
104111
There are different ways a clients might interact with your APIs.
105112
These ways depend on your API configuration (what you allow) and on which route the request is finally made.
106113

107114
- *Access from web frontend* means the user is browses the Nextcloud web frontend with a browser.
108115
- *Access from an external app* indicates that the user is not using the normal browser (as logged in) but directly navigates a certain URL.
109116
This can be in a new browser tab or an external program (like an Android app or simply a curl command line).
117+
- *Access from external website* means that the user browses some third party web site and *magically* data from your app appears.
118+
Technically, the other website would embed/load/use images, JSON data, or other resources from a URL pointing to the Nextcloud server.
110119

111120
.. list-table:: Comparison of different API types
112121
:header-rows: 1
@@ -115,24 +124,35 @@ These ways depend on your API configuration (what you allow) and on which route
115124
* - Description
116125
- 1 (plain)
117126
- 2 (w/o CSRF)
127+
- 3 (CORS)
118128
- 4 (OCS)
119-
- 4 (OCS w/o CSRF)
129+
- 5 (OCS+CORS)
120130
* - URL prefix (relative to server)
131+
- ``/apps/<appid>/``
121132
- ``/apps/<appid>/``
122133
- ``/apps/<appid>/``
123134
- ``/ocs/v2.php/apps/<appid>/``
124135
- ``/ocs/v2.php/apps/<appid>/``
125136
* - Access from web frontend
126137
- yes
127138
- yes (CSRF risk)
128-
- yes
129139
- yes (CSRF risk)
140+
- yes
141+
- yes (CSRF risk [#]_)
130142
* - Access from external app
131143
- ---
132144
- yes
145+
- yes
133146
- yes (with header [#]_)
134147
- yes
148+
* - Access from external website
149+
- ---
150+
- ---
151+
- yes
152+
- ---
153+
- yes
135154
* - Encapsulated data
155+
- no
136156
- no
137157
- no
138158
- yes (JSON or XML)
@@ -149,4 +169,7 @@ As a rule of thumb one can conclude that OCS provides a good way to handle most
149169
The only exception to this is if you want to provide an API for external usage where you have to comply with an externally defined API scheme.
150170
Here, the encapsulation introduced in OCS and CSRF checks might be in your way.
151171

172+
.. [#] Only if you have set ``#[NoCSRFRequired]``.
173+
OCS controllers have other CSRF checks in place that might with CORS without disabling the CSRF checks completely.
174+
Using the ``OCS-APIREQUEST`` header is also a CSRF protection but is compatible with CORS.
152175
.. [#] The OCS controller needs the request header ``OCS-APIREQUEST`` to be set to ``true``.

0 commit comments

Comments
 (0)