I’ll try my best to explain, but for the closest context I could think of, imagine that I am building an analytics platform that allows paying users to sign up, place a tracking script on their website and track their website visitors’ data. This would mean anyone who visits their site would need to be able to make an API request (without them even necessarily knowing - done via the tracking script) to send the analytics data to the database. E.g. users may install a script in the head like this:
<script data-domain="somedomain.com" src="path/to/tracking/script.js?id=SOMETHING" defer></script>
I also wouldn’t want users to have to ever update this script.
My scenario is somewhat similar, but to summarise, I worry that anyone (e.g. a hacker) would be able to insert a load of junk data into the database (since they could make API requests that require no auth).
What could be done to prevent this, but not prevent the script from tracking all users? I was thinking along these lines:
- IP rate throttling to cap the no. of requests someone can make
- Auth tokens send in request headers (but they expire, unless they could be updated automatically some way? From another script? But couldn’t a hacker also access this anyway?)
- Ensure that the API endpoint used by the tracking script is designed to only accept requests from the tracking script, and not directly from other sources (e.g. how? via CORS headers?)
But presumably authenticating requests is not an option since it would prevent tracking everyone else.
I could be wrong in my assumptions here, but any ideas would be greatly appreciated. It just feels weird, since anyone would be able to insert a load of junk data into the database. It may not be the signed-up user who misuses the API, but instead a random hacker visiting their site