Open
Description
Right now, anyone who knows the API URL can send a request and add data to our Google Sheet. This means unauthorized users could add fake or incorrect data just by making a POST request.
Why is this happening?
The API doesn’t have any security checks in place. There’s nothing stopping someone from sending data, even if they’re not supposed to.
Solution
- We should require a SECRET_KEY with every request. Here’s how it will work:
- The frontend will send a SECRET_KEY along with the request in body.
- The Apps Script function will check if the key matches the one stored on the backend.
- If the key is correct, the request is processed. If not, it gets rejected immediately.
Here is the code to solve the problem
const SECRET_KEY = "OUR_SECRET_KEY";
const postData = JSON.parse(e.postData.contents);
const receivedSecret = postData.secret_key;
// Check if the secret key is provided and matches
if (!receivedSecret || receivedSecret !== SECRET_KEY) {
return ContentService.createTextOutput(
JSON.stringify({ status: "error", message: "Unauthorized access" })
).setMimeType(ContentService.MimeType.JSON);
}
Here is the video of current behavior