Summary: There’s no way, not that I’m pessimistic, but in fact there’s no way.
CSRF protection:
If you’re worried about someone reading the content, there are two "distinct":
- Get your
/json.json
on the client side, via Javascript/Ajax.
- Get your
/json.json
on the "server"/"client" side, via Curl/Wget/Webviewer (and "custom browsers").
The first situation is easier and in fact "there is something to do" to prevent:
- Add the header of
Access-Control-Allow-Origin
, strict to your website.
- (Optional) Add the
Access-Control-Allow-Headers
, limit headers (eg. X-CRSF-TOKEN
) that can be sent.
- (Optional) Add the
Access-Control-Allow-Methods
, limit the accepted methods (ex. GET
) so only this method will be accepted.
So you can use:
header('Access-Control-Allow-Origin: http://www.dominio.com http://m.dominio.com');
header('Access-Control-Allow-Methods: GET');
I recommend seeing this answer.
- Add a
CSRF Token
.
- CSRF Token must be valid only for a single session.
- (Recommended) The CSRF Token must be valid for a single IP.
- (Optional) The CSRF Token should expose after a single use.
- (Optional) The CSRF Token must be unique for each URL or each tracking.
You can read this answer, think of decompressing and not recommend the use of generateRandomString()
for being a LCG.
Measures that are inefficient but can help:
- Check the
Referrer
/Origin
, are easily forged.
The second situation is impossible to be fixed, literally there is no way to prevent this, everything listed above is not enough to prevent the use of Curl/Wget.
Create a Rate-Limit
, a limit on how many times the page can be accessed per second per IP (or Ipv6 range) is relatively efficient as it will require the use of multiple proxies if you want to obtain content constantly, but remember the CGNAT in Ipv4.
Block access via TOR and public proxies.
Much less efficient measures, but they can help:
- Create a "challenge" in Javascript, such as
jjencode
, Cloudflare uses this.
There may be other security problems, which is not the CSRF, for example the Mitm, XSS, Of (including the json_decode
, standard, is vulnerable by hash-dos) and among other problems like Side-Channel Attacks, but these are other things that have no relation to the question...
Did some testing using the header
Access-Control-Allow-Origin
. ?– Maurivan
I added an example of what I’m using to such @Maurivan
– Leo Letto
You’re right, the URL will be accessible.
$_SERVER['REMOTE_HOST'] ou $_SERVER['REMOTE_ADDR']
since only 2 hosts will have access.– Maurivan