/    Sign up×
Community /Pin to ProfileBookmark

I’ve calling JSON files via JS fetch method.

How can I make it so only I can call these and get the response?

My concern is people can find these URL’s by looking into my page source and then directly linking to them and getting the JSON.

I’ve thought, having a secret key in my PHP somewhere, that’s then checked on my page which returns the JSON?

Not sure how this could work, sessions won’t work as they can be found?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 06.2021 — Do users have to be logged in to access the page in the first place? If not, I don't know of any sure-fire way to prevent them from accessing anything the page accesses if they're knowledgeable enough. If, on the other hand, they do have to be logged in, then you can set up some sort of PHP endpoint that serves up the JSON after validating that they're logged in.
<i>
</i>&lt;?php
session_start();
if(!empty($_SESSION['something'])) {
header('Content-Type: application/json'); // or whatever the correct header is?
readfile('/path/outside/of/public_html/file_of_interest.json');
}
else {
header('HTTP/1.0 404 Not Found');
}
Copy linkTweet thisAlerts:
@kiwisauthorMay 07.2021 — What if, on my index page I placed generic ""username"" and ""password"" credentials, just keys really and set a session.

On my page which creates the JSON data, I checked if the session was set and check those values?
Copy linkTweet thisAlerts:
@kiwisauthorMay 07.2021 — Or given the JSON file is being called from my own server, check the referrer? Could that be manipulated easily enough?
Copy linkTweet thisAlerts:
@NogDogMay 07.2021 — Either/both of those (session cookie, referrer) could be worked around, if by nothing else than first sending a cURL request the web page, capturing all the headers and HTML from the response, then building the direct request to the JSON data with whatever headers and such are needed.

Perhaps that's all you need, if it's enough to stop ninety-something percent of whatever small percentage of your site's users might be inclined to try to access that JSON directly. If your desire is 100% blockage, that's probably going to be difficult to do (at least based on my imperfect knowledge); but maybe making it difficult enough will stop most, and be "good enough"?
Copy linkTweet thisAlerts:
@kiwisauthorMay 08.2021 — @NogDog#1631386

I realize it's a bit different but how do some API services restrict access to paid services etc?
Copy linkTweet thisAlerts:
@NogDogMay 08.2021 — > @kiwis80#1631387 how do some API services restrict access to paid services etc

Typically there's something like having an API key that you get issued with your account. You could layer SSL/TLS certs on top of that, and other stuff I only slightly understand. (Working on something right now where we have an api key, a secret key, TLS cert from Digicert, then first have to acquire a limited-time JWT with that stuff from one service, then submit the actual API request with that JWT along with the TLS cert -- makes my head spin.)
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...