-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Support for cross-domain WebDAV access (CORS) #3131
Comments
Also related to this as a future step »Dropbox like file picker javascript integration #2028« |
@LukasReschke what are the next steps here? |
I'd like to help as much as I can with this issue - my end goal is to be able to parse the calendar data out with some JS so my personal blog can detail conferences I've spoken at and places I will be speaking soon. Hit me up if you need to bounce ideas for the solution off someone, or need example use cases etc. I don't have a dev environment for nextcloud set up, but if the problem is a shortage of hands I can take a stab at implementing it if I get some guidance. |
@doleraj most often the reason is a shortage of hands, so your help would be much appreciated! :) For any guidance best join our IRC channel #nextcloud-dev or ask here. Also cc @perry-mitchell for his work on connecting Nextcloud to web apps: https://nextcloud.com/blog/using-webdav-fs-to-access-files-in-nextcloud/ and cc @nextcloud/javascript |
We're currently using webdav+Nextcloud to read and write our password archives both in the browser and in NodeJS, using this project (disclaimer: I'm the author). You can see here that we write to the storage without any specifics (besides auth headers).
So it seems that at least in out browser extension, it does indeed look like requests to Nextcloud servers (at least the demo ones) are failing with perm errors: Also seems that I was not so successful in trying to add the I've been trying with the following JS: var request = new Request("https://demo.nextcloud.com/vaeshah9/remote.php/webdav/test.txt", {
method: "GET",
credentials: "include",
headers: new Headers({
Accept: "text/plain",
Authorization: "Basic " + btoa("admin:admin")
})
});
fetch(request).then(function(res) {
console.log(res);
}) At the moment these do look like CORS issues 😕 |
The "with authentication" thing may be the trick. I'm trying to get at a public calendar without auth since the code'll be running browser-side. As an example, hitting
causes a CORS error: |
Also cc @georgehrke @tcitworld of the Calendar app for @doleraj's specific question. :) |
... oops! I apologize, I'd forgotten that Calendar is actually a separate app. |
@doleraj But public-calendars is part of the dav app which belongs to the server ;) @LukasReschke might know more security wise. |
We've had a similar issue in bookmarks and ended up having an internal controller being sort of a facade for the public one which incorporates the |
I discovered that owncloud/core#28457 is merged upstream, maybe we can integrate this. |
@tcitworld mind opening a pull request to downstream that change? :) Or @LukasReschke what do you say? |
Any updates on this? |
@perry-mitchell hey, could you solve it? |
@mehrdaad We have Nextcloud somewhat working in our password management software, but I believe it's a bit buggy still. The NodeJS side works it seems (no credentials or cookies) |
Can I add a sub domain like app.domain.tld to trusted domain for Nextcloud and use that app with webdav without having the CORS issue? |
We were finally able to get Nextcloud working in the browser, by simply using |
Any update on this? Is there anyone looking into this at the moment? Or is this almost a lost cause? |
Same question here, I want to use some features of the Nextcloud on a different client with a different view(limited view), I keep getting 'unauthorized 401' error, any solutions? |
Adding the code from https://stackoverflow.com/questions/8719276/cors-with-php-headers/9866124#9866124 to remote.php (just after the already present header-function call) solves the issue for me. Update: Example code (remote.php): try {
require_once __DIR__ . '/lib/base.php';
// All resources served via the DAV endpoint should have the strictest possible
// policy. Exempted from this is the SabreDAV browser plugin which overwrites
// this policy with a softer one if debug mode is enabled.
header("Content-Security-Policy: default-src 'none';");
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, PUT, MOVE, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
if (\OCP\Util::needUpgrade()) {
// [...] (end of snippet) |
It could be useful to also add the POPFIND to the Access-Control-Allow-Methods, like:
|
Any update on this? Over at Buttercup we see issues pop up now and then about users not being able to access their Nextcloud servers via WebDAV, which we provide using my library webdav-client. It also has a couple of issues regarding Nextcloud access from the browser. Are there any plans on supporting this? Will OAuth be the only supported access method via websites? Some official feedback would be much appreciated here. |
Is there a way for whitelisting a specific origin from which I want to access a public calendar link like If not I think there should be one. If I have a website/webapp that I'm controlling myself I want to have the possibility to let it access NC data without running into CORS issues. |
Guys, could you simply always enable CORS by default in all Nextcloud instances? Authors of any webapp can always create a server-side "mirroring" of the HTTP requests, and their server does not care about the CORS. So not having CORS headers does not add any security, it only makes everything more complicated. |
Also interested in this. If others are stuck but use NextCloud behind nginx as a reverse proxy I can recommend https://www.williamjbowman.com/blog/2021/05/13/enabling-cors-for-nginx-webdav-and-caldav-reverse-proxy/ which worked for me for https://github.com/bfren/docker-nginx-webdav @photopea do you mean something like https://github.com/sebastienvercammen/node-cors-proxy-server and if not do you have a recommendation until this is implemented? |
There still is https://github.com/digital-blueprint/webapppassword. |
Hello everyone, I have created a NextCloud app which adds a file menu item in First step I did was using the hardcoded As a second step, I replaced the hardcoded Is there a way to avoid CORS and preflight issues in login v2 flow or OAuth flow? |
EDIT : It works... I do not have create the .json file... Thanks |
I was also trying to upload a file from a browser client to I have work around in place now locally with my Traefik proxy that responds to the
It would be really nice to not be forced to do it like that. |
That's a great workaround! |
The problem is, that I as a client developer can not rely on having this in place for random Nextcloud instances. |
...nor random Nextcloud Apps for that matter. |
The Apps have probably different methods for pushing files. But idk. |
I meant "the permissions to install an app that sets headers", like https://github.com/digital-blueprint/webapppassword. 😉 |
I don't understand why simply allowing access for login v2 is such an issue, NextCloud already rejects unauthorized requests, so setting CORS for the WebDAV gateway shouldn't be an issue either, but if that is the blocker oh well, SSR web apps can get around that easily enough. The main issue for web apps, as far as I can tell, is the |
I have a similar problem with using the download link for a markdown file from a js app on a separate domain: the default CORS policy blocks it. I don't see any reason those would be… Should I open a separate issue? |
It does work using WebAppPasswordSolution:
Example:
|
Does it work for public shares?Unfortunately not. The above solution only works for "private" shares that are being accessed via "^/remote.php/dav/files...". If you also want to access public shares "^/public.php/webdav/...", then the WebAppPassword does nto help you, as it does not take care of the cors headers there. You can see that, because all preflight requests (done by the browser automatically before every fetch) will fail because of "missing basic authentication header". And just to be clear: This is a nextcloud bug, as based on specs, there must not be authentication headers in the preflight options request. Can it work for public shares?Yes, it can, but you need to modify the .htaccess for this (in the nextcloud root folder). There should be already a couple of rewrites there ;-) If you are using a reverse proxy, you can add the rewrites there as well, even though that might me more ressource-intensive, as the proxy needs to cache the outgoing response to be able to rewrite headers (you can use nginX/LUA for that). Just add following code (adjust to your paths etc.) to the .htaccess:
This enforces/overwrite the headers to tell the browser to move forward with the actual fetch request. Keep in mind, that this is not increasing any security risk, as the actual request (PROPFIND) will be checked correctly for the authorization headers. The problem you are facing is, that the browser never tries the fetch request (PROPFIND), as the preflight (OPTIONS) failes, due to this Nextcloud bug. Have fun! |
I tried the htaccess rewrite to use a public share but the PROPFIND request still fails with a CORS error (OPTION passes with 204) Am I correct to use "myNextCloud/public.php/webdav" with the share token as user and an empty password? |
Almost. When you create a public share, then nextcloud creates something like "https://mydomain/basepath/index.php/s/ideFb6rGMjgJa8y" and password "password". |
It still didn't work. In my case the file is a public share without password, it just needs the token as login and an empty password. Everything is correct because I can see the XML file listing in the browser XHR console but it just isn't passed to my script. From what I see in the browser inspector, with the PROPFIND request, the "always set" header lines are not set. Fortunately, here is the solution I found and it works: It might be related to my hosting provider and an apache/fastcgi bug as stated here. Using one of the solution in the above article, namely adding the same headers through php directly, worked for me. There is a file called .user.ini in the hosting root and it allows to set php properties or settings. If you add the following line it will prepend a php file to every php file request.
The myheaders.php file looks like this:
It now seems to work without problem, I can retrieve the file listing and also GET a single file directly. Many thanks pkuegler for pointing me in the right direction and taking the time to explain it in detail, I've been looking for weeks about a way to achieve this! |
For anyone running Nextcloud behind Caddy, this is how I got it working. nextcloud.example.com {
# Matcher for CORS preflight request from my app.
@myapp {
method OPTIONS
header Origin https://myapp.example.com
}
# Set CORS response headers for the preflight request.
header @myapp Access-Control-Allow-Origin "https://myapp.example.com"
header @myapp Access-Control-Allow-Methods "PUT"
header @myapp Access-Control-Allow-Headers "content-type"
# Respond successfully the preflight request.
respond @myapp 204
reverse_proxy 127.0.0.1:11000 {
# Optionally set the CORS headers in the Nextcloud response.
header_down Access-Control-Allow-Origin "https://myapp.example.com"
header_down Access-Control-Allow-Methods "PUT"
header_down Access-Control-Allow-Headers "content-type"
}
} |
It would be great if web applications hosted on a different domain could access Nextcloud files via WebDAV. This would enable web app developers to offer users to store personal application data in their Nextcloud without the need to provide a dedicated Nextcloud app.
Currently this is not possible as the necessary CORS headers are not set.
@LukasReschke
The text was updated successfully, but these errors were encountered: