@@ -13,6 +13,9 @@ import 'package:universal_io/io.dart' hide HttpClient;
13
13
/// Base path used on the server
14
14
final webdavBase = PathUri .parse ('/remote.php/webdav' );
15
15
16
+ // ignore: do_not_use_environment
17
+ const bool _kIsWeb = bool .fromEnvironment ('dart.library.js_util' );
18
+
16
19
@internal
17
20
class WebDavRequest extends http.BaseRequest {
18
21
WebDavRequest (
@@ -55,6 +58,8 @@ class WebDavClient {
55
58
// ignore: public_member_api_docs
56
59
final DynamiteClient rootClient;
57
60
61
+ String ? _token;
62
+
58
63
Future <http.StreamedResponse > _send (
59
64
String method,
60
65
Uri url, {
@@ -75,6 +80,29 @@ class WebDavClient {
75
80
...? rootClient.authentications? .firstOrNull? .headers,
76
81
});
77
82
83
+ // On web we need to send a CSRF token because we also send the cookies. In theory this should not be required as
84
+ // long as we send the OCS-APIRequest header, but the server has a bug that only triggers when you also send the
85
+ // cookies. On non-web platforms we don't send the cookies so we are fine, but on web the browser always does it
86
+ // and therefore we need this workaround.
87
+ // TODO: Fix this bug in server.
88
+ if (_kIsWeb) {
89
+ if (_token == null ) {
90
+ final response = await rootClient.httpClient.get (Uri .parse ('${rootClient .baseURL }/index.php' ));
91
+ if (response.statusCode >= 300 ) {
92
+ throw DynamiteStatusCodeException (
93
+ response.statusCode,
94
+ );
95
+ }
96
+
97
+ _token = RegExp ('data-requesttoken="([^"]*)"' ).firstMatch (response.body)! .group (1 );
98
+ }
99
+
100
+ request.headers.addAll ({
101
+ 'OCS-APIRequest' : 'true' ,
102
+ 'requesttoken' : _token! ,
103
+ });
104
+ }
105
+
78
106
final response = await rootClient.httpClient.send (request);
79
107
80
108
if (response.statusCode >= 300 ) {
0 commit comments