Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Referer Header Required" error in couchdb when trying to use _find

I'm getting started with CouchDB, and have started building an application. I can write and read just fine using CURL, but i've started to try and search, using _find, and i get an error stating "Referer Header Required".

curl -X POST http://localhost:5984/mydb/_find -d '{"selector":{"member.email":"[email protected]"}}

returns:

{"error":"bad_request","reason":"Referer header required."}

When I try and add a "Referer" host in the call, it just says the referer needs to match the host, but I don't know what host it means.

curl -X POST http://localhost:5984/mydb/_find -d '{"selector":{"member.email":"matthew"}}' -H "Referer: localhost" {"error":"bad_request","reason":"Referer header must match host."}

I've tried all sorts of combinations, using the localhost, the with and without the port numbers, I've tried with X-Forwarded-Host, I've find a dozen related articles about POST vs PUT, and versioning, but I've come to a dead end, and just can't figure out what I'm missing.

nb. I'm running couchdb1.7.x on an OSX laptop.

like image 315
Matthew Knight Avatar asked Apr 08 '18 17:04

Matthew Knight


1 Answers

I managed to fix the Referer header required and the subsequent Referer header must match host by looking through the source code. Though the real problem was expecting _find to work in those ancient 1.X builds OS package managers default to. It's much better to upgrade to the latest version of couchdb (3.1.1).

You can workaround the error on old versions by adding vhosts to the config file /etc/couchdb/local.ini

[vhosts]
;localhost = /db/
localhost = /*
[httpd]
x_forwarded_host = Y-Forwarded-Host

and potentially also using a custom header name for X-Forwarded-Host, in my case the software fetching the API call does some overly clever things to X-Forwarded-Host and Host headers.

so when you call couchdb just include the new header with either 'localhost'

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer: localhost' \
-H 'Y-Forwarded-Host: localhost' \
-d '{"selector":{"member.email":"[email protected]"}}

or blank so they match

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer' \
-H 'X-Forwarded-Host' \
-d '{"selector":{"member.email":"[email protected]"}}

neither header is needed in version 2 or 3 onward, they came to their senses.

like image 103
bumkino Avatar answered Oct 03 '22 02:10

bumkino