You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add modifications from reviews
Co-authored-by: Joas Schilling <213943+nickvergessen@users.noreply.github.com>
Co-authored-by: Kate <26026535+provokateurin@users.noreply.github.com>
Signed-off-by: Christian Wolf <github@christianwolf.email>
Copy file name to clipboardExpand all lines: developer_manual/digging_deeper/rest_apis.rst
+13-14Lines changed: 13 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ REST APIs
7
7
.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>
8
8
9
9
Offering a RESTful API is not different from creating a :doc:`route <../basics/routing>` and :doc:`controllers <../basics/controllers>` for the web interface.
10
-
It is recommended though to inherit from ApiController and add **@CORS** annotations to the methods so that `web applications will also be able to access the API <https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS>`_.
10
+
It is recommended though to inherit from ApiController and add ``#[CORS]`` attribute to the methods so that `web applications will also be able to access the API <https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS>`_.
11
11
12
12
.. code-block:: php
13
13
@@ -103,22 +103,21 @@ The following combinations of attributes might be relevant for various scenarios
103
103
.. warning::
104
104
Adding the ``#[NoCRSFRequired]`` attribute imposes a security risk.
105
105
You should not add this to your controller methods unless you understand the implications and be sure that you absolutely need the attribute.
106
-
Typically, you can instead use the ``OCS-APIRequest`` header for data requests, instead.
106
+
Typically, you can use the ``OCS-APIRequest`` header for data requests instead, in order to satisfy the CSRF checks in your API requests.
107
107
108
108
.. warning::
109
109
Adding the attribute ``#[CORS]`` alone is not sufficient to allow access using CORS with plain frontend routes.
110
110
Without further measures, the CSRF checker would fail.
111
111
So, enabling CORS for plain controllers is generally and highly discouraged.
112
112
113
-
You would have to disable the CSRF checker (one more security risk) or use the ``OCP-APIRequest`` header to successfully pass the checker.
113
+
You would have to disable the CSRF checks (one more security risk) or use the ``OCP-APIRequest`` header or send a CSRF token to successfully pass the checks.
114
114
The latter requires dedicated JS code on the importing page.
115
115
116
116
There are different ways a clients might interact with your APIs.
117
117
These ways depend on your API configuration (what you allow) and on which route the request is finally made.
118
118
119
-
- *Access from web frontend* means the user is browses the Nextcloud web frontend with a browser.
120
-
- *Access from non-browser* is if the user accesses the resource or page not using the default browser.
121
-
Thus can be e.g. a script using CURL or an Android app.
119
+
- *Access from web frontend* means the user is accessing the Nextcloud web frontend with a web browser.
120
+
- *Access from non-browser* is if the user accesses the resource or page using something that is not a web browser, like an Android app or a curl command.```
122
121
- *Access from external website* means that the user browses some third party web site and data from your Nextcloud server appears.
123
122
The other website has to embed/load/use images, JSON data, or other resources from a URL pointing to the Nextcloud server, to be able to do this.
124
123
@@ -147,8 +146,8 @@ These ways depend on your API configuration (what you allow) and on which route
147
146
- yes
148
147
- yes
149
148
* - Access from external website
150
-
- ---
151
-
- ---
149
+
- no
150
+
- no
152
151
- yes
153
152
* - Encapsulated data
154
153
- no
@@ -157,12 +156,12 @@ These ways depend on your API configuration (what you allow) and on which route
157
156
158
157
.. [#] The external app has to satisfy the CSRF checks.
159
158
That is, you need to have the ``OCS-APIRequest`` HTTP request header set to ``true``.
160
-
This is only possible for NC 30 onwards, older versions do not respect the header.
159
+
This is only possible for Nextcloud 30 onwards, older versions do not respect the header.
161
160
162
161
Methods from ``Controller`` classes can return ``DataResponse`` objects similar to ``OCSController`` class methods.
163
162
For methods of a ``Controller`` class, the data of this response is sent e.g. as JSON as you provide it.
164
163
Basically, the output is very similar to what ``json_encode`` would do.
165
-
In contrast, the OCS controller will encapsulate the data in an outer shell that provides some more (meta) information.
164
+
In contrast, the ``OCSController`` will encapsulate the data in an outer shell that provides some more (meta) information.
166
165
For example a status code (similar to the HTTP status code) is transmitted at top level.
167
166
The actual data is transmitted in the ``data`` property.
168
167
@@ -177,7 +176,7 @@ Historical options
177
176
.. deprecated:: 30
178
177
The information in this section are mainly for reference purposes. Do not use the approaches in new code.
179
178
180
-
Before NC server 30 the plain ``Controller`` classes' methods did not respect the ``OCS-APIRequest`` header.
179
+
Before Nextcloud 30 the plain ``Controller`` classes' methods did not respect the ``OCS-APIRequest`` header.
181
180
Thus, to provide access to this type of controller methods for external apps, it was necessary to use the ``#[NoCSRFRequired]`` attribute (or the corresponding ``@NoCSRFRequired`` annotation).
182
181
183
182
The following combinations of attributes were relevant for various scenarios:
@@ -214,12 +213,12 @@ The following combinations of attributes were relevant for various scenarios:
Copy file name to clipboardExpand all lines: developer_manual/prologue/security.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -262,8 +262,8 @@ CORS
262
262
263
263
`Cross-origin resource sharing (CORS) <https://en.wikipedia.org/wiki/Cross-origin_resource_sharing>`_ (see also on `MDN <https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS>`__) is a method implemented by browser to access resources from different domains at the same time.
264
264
Assume, there is a website published on host A.
265
-
The URL would for example be https://A/path/to/index.html.
266
-
If there is a _different_ host B that serves a resource (e.g. an image file) as https://B/assets/image.jpg, the index file on host A could simply link to the image on B.
265
+
The URL would for example be ``https://A/path/to/index.html``.
266
+
If there is a _different_ host B that serves a resource (e.g. an image file) as ``https://B/assets/image.jpg``, the index file on host A could simply link to the image on B.
267
267
However, to protect B and its property (the image), the browsers do not silently embed the image of B into the page of A.
268
268
Instead, B is kindly asked by the browser if embedding is allowed (the so-called `preflight <https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request>`_).
269
269
@@ -273,7 +273,7 @@ These define, what the browser is to be allowed to do.
273
273
Only if the destination server B confirms cross site resource sharing is allowed, the browser access the resource.
274
274
275
275
Basically, accessing foreign resources is not limited to embedding images.
276
-
Using JavaScript, arbitrary XHR/Ajax requests can be directed at arbitrary other hosts.
276
+
Using JavaScript, arbitrary XHR/Ajax requests can be directed at arbitrary other hosts, which might be used to call APIs that leak your data.
277
277
There are some safety measurements in place (especially about cookie handling), but one has still to be careful not to leak information unwillingly.
278
278
Especially, if the destination server B allows to sent credentials using ``Access-Control-Allow-Credentials: true``, cross site scripting is very critical.
279
279
You need :ref:`CSRF protection <csrf_introduction>` in place or your users are at relatively high risk.
0 commit comments