vrtmrz / obsidian-livesync

MIT License
3.93k stars 132 forks source link

Cannot connect due to CORS error #352

Open thany opened 6 months ago

thany commented 6 months ago

Abstract

Suddenly, I can no longer connect to the couchdb server, but only on desktop. On mobile it still works perfectly fine. So with that, the server is configured correctly, afaict.

On desktop though, I'm getting this:

18/12/2023, 10:57:22->HTTP:GET to:/ -> failed
18/12/2023, 10:57:22->TypeError:Failed to fetch
18/12/2023, 10:57:22->TypeError:Failed to fetch
18/12/2023, 10:57:22->could not connect to https://***** : obsidian 
(TypeError:Failed to fetch
**Note** This error caused by many reasons. The only sure thing is you didn't touch the server.
To check details, open inspector.)

Inspector: image

Expected behaviour

Actually happened

See error above.

Reproducing procedure

  1. Let it sync.

Honestly, I don't know what caused this. If I knew I could provide repro steps more accurately. That's kind of a catch-22, isn't it.

Report from the LiveSync

For more information, please refer to Making the report.

Report from hatch ``` ----remote config---- chttpd: bind_address: any port: "5984" admins: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 vendor: name: The Apache Software Foundation feature_flags: partitioned||*: "true" chttpd_auth: hash_algorithms: sha256, sha secret: a51e965b21a73de53a48ff1933e6afb7 indexers: couch_mrview: "true" prometheus: additional_port: "false" bind_address: 127.0.0.1 port: "17986" httpd: bind_address: 127.0.0.1 port: "5986" smoosh: state_dir: ./data couch_httpd_auth: authentication_db: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 secret: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 authentication_redirect: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 couchdb_engines: couch: couch_bt_engine couchdb: database_dir: ./data uuid: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 view_index_dir: ./data ---- Plug-in config --- version:0.19.23 couchDB_URI: self-hosted couchDB_USER: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 couchDB_PASSWORD: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 couchDB_DBNAME: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 liveSync: false syncOnSave: false syncOnStart: false savingDelay: 200 lessInformationInLog: false gcDelay: 0 versionUpFlash: "" minimumChunkSize: 20 longLineThreshold: 250 showVerboseLog: true suspendFileWatching: false trashInsteadDelete: true periodicReplication: false periodicReplicationInterval: 60 syncOnFileOpen: false encrypt: false passphrase: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 usePathObfuscation: false doNotDeleteFolder: false resolveConflictsByNewerFile: false batchSave: false deviceAndVaultName: "" usePluginSettings: false showOwnPlugins: false showStatusOnEditor: true usePluginSync: false autoSweepPlugins: false autoSweepPluginsPeriodic: false notifyPluginOrSettingUpdated: false checkIntegrityOnSave: false batch_size: 50 batches_limit: 40 useHistory: true disableRequestURI: true skipOlderFilesOnSync: true checkConflictOnlyOnOpen: false syncInternalFiles: false syncInternalFilesBeforeReplication: false syncInternalFilesIgnorePatterns: \/node_modules\/, \/\.git\/, \/obsidian-livesync\/ syncInternalFilesInterval: 60 additionalSuffixOfDatabaseName: "" ignoreVersionCheck: false lastReadUpdates: 19 deleteMetadataOfDeletedFiles: false syncIgnoreRegEx: "" syncOnlyRegEx: "" customChunkSize: 100 readChunksOnline: true watchInternalFileChanges: true automaticallyDeleteMetadataOfDeletedFiles: 0 disableMarkdownAutoMerge: false writeDocumentsIfConflicted: false useDynamicIterationCount: false syncAfterMerge: false configPassphraseStore: "" encryptedPassphrase: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 encryptedCouchDBConnection: 𝑅𝐸𝐷𝐴𝐢𝑇𝐸𝐷 permitEmptyPassphrase: false useIndexedDBAdapter: true useTimeouts: false writeLogToTheFile: false doNotPaceReplication: false hashCacheMaxCount: 300 hashCacheMaxAmount: 50 concurrencyOfReadChunksOnline: 100 minimumIntervalOfReadChunksOnline: 333 hashAlg: xxhash64 suspendParseReplicationResult: false doNotSuspendOnFetching: false useIgnoreFiles: false ignoreFiles: .gitignore syncOnEditorSave: false pluginSyncExtendedSetting: {} ```

Obsidian debug info

Debug info ``` SYSTEM INFO: Obsidian version: v1.4.16 Installer version: v1.4.16 Operating system: Windows 10 Pro 10.0.19045 Login status: not logged in Insider build toggle: off Live preview: off Legacy editor: off Base theme: dark Community theme: none Snippets enabled: 0 Restricted mode: off Plugins installed: 1 Plugins enabled: 1 1: Self-hosted LiveSync v0.19.23 RECOMMENDATIONS: Community plugins: for bugs, please first try updating all your plugins to latest. If still not fixed, please try to make the issue happen in the Sandbox Vault or disable community plugins. ```

Plug-in log

See above.

Network log

See above.

Other information, insights and intuition.

Obsidian itself is up-to-date, the plugin is up-to-date, and couchdb is at 3.3.3. This problem happens with an admin account as well as with a normal user account.

Just as a heads-up: "Check database configuration" cannot work, because it cannot connect. But again, I didn't touch neither the config nor the server, so the config should still work. And it works on mobile, so it's good.

dlphnkck commented 6 months ago

Hi. I am not familiar with the LiveSync plugin, but I have a question. Is the LiveSync plugin version up-to-date?

Debug info 1: Self-hosted LiveSync v0.19.23

The current LiveSync plugin latest is v0.21.5. (Releases Β· vrtmrz/obsidian-livesync)

I apologize if this is not relevant.

thany commented 6 months ago

It is up-to-date now πŸ˜€

But it didn't help to resolve the issue, unfortunately.

tecix commented 6 months ago

@thany I had similar issue and found out it was because of the attachment files. You should check the configuration with nginx to allow the sync for bigger file size.

Try to put this in nginx conf and restart the service: client_max_body_size 100M;

thany commented 6 months ago

I have couchdb running in a docker container, and cannot find a nginx config anywhere. Can you point out where I'm supposed to add that config line exactly?

At the filesystem root, find | grep nginx yields 0 results.

Btw, couchdb is an Apache project, so why would they use nginx for the webserver part? πŸ˜€

Oh and also, if filesize is the problem, then should it not be able to connect to couchdb in the first place? Because before that happens, it has no chance of even trying to send any file.

tecix commented 6 months ago

I use Nginx for the reverse proxy to access livesync server via a domain name. Maybe this is a difference issue of mine.

thany commented 6 months ago

And sure I can enable CORS in couchdb itself, but then two more questions would arise:

  1. What domain should I fill in to allow? Because I'm obviously not going to allow everything. Couchdb admin says to has to start with http/https, but Obsidian is not a website.
  2. Obsidian is a desktop application, so why would it need CORS in the first place? cmiiw, but CORS is meant communication from javascript on a website to another domain. But in this case, there's no domain to be had...
vrtmrz commented 6 months ago

I am really sorry, and, thank you all for your patience and cooperation!

And sure I can enable CORS in couchdb itself, but then two more questions would arise:

We should enable CORS in CouchDB and leave handlings to it. CouchDB will pick a suitable origin for each request from configured values. Each platform uses a different origin.

  1. app://obsidian.md, capacitor://localhost and http://localhost should be set. This can be configurable by the Check button; from the Check database configuration of the setting dialogue in Self-hosted LiveSync on Obsidian. (I think that this button could be Doctor as in some traditions). Note: it could not be configurable from the Web UI. We have to configure it by using REST API or editing INI files.
  2. I have been so surprised as same on you. Obsidian is built on the web technology. The desktop version uses app://obsidian.md for the origin. (app is not the actual protocol scheme, but a real one.) This can be revealed by evaluating window.location.origin in DevTools. As noted above, this varies from platform to platform.
thany commented 6 months ago
  1. This can be configurable by the Check button; from the Check database configuration of the setting dialogue in Self-hosted LiveSync on Obsidian.

Except the problem is that it too, will throw the same CORS error. But it can be added forcibly on the "Main config" page in the couchdb admin config. Not sure if that's a "healthy" thing to do, because the requirement for http/https is probably there for a reason.

Either way, I don't believe Obsidian should behave like it's a faux website. I'm not sure where this should be fixed if possible. Is it a bug in Obisidan, or maybe in Electron?

vrtmrz commented 5 months ago

Thank you for your good point! I have looked into it again, to find what should it be to the origin. Then, I collected some information. Let me share it.

CouchDB can accept origins which have non-HTTP schemes neither via HTTP API nor the ini file. A CORS origin could have them, this would be based on old-fashioned CORS specification (RFC-6454 and RFC-3986). RFC-3986 seems to be refined as URL Living Standard, these are defined as special-scheme in it. Therefore, this could be the problem of Fauxton; which is the web-frontend of CouchDB. However, it is web-frontend and may be designed that way as far as I looking at issues on their repo.

On the other hand, iOS and Electron use non special-scheme. iOS (Based on Capacitor) seems to use capacitor://localhost as this. I could not find the exact source of the information, but Electron uses app:// in their sample

This might mean that it seems to be well-designed in each their area; even though if they makes very complicated situation for us.

I hope that my information will help us!