r/GoogleAppsScript • u/leftabomb • Mar 17 '21
Resolved How to (reasonably) secure an API key?
Let's say I have a Sheets bound script that does an API request with UrlFetchApp.
Is there a way to have variables such as keys stored in like a library or something to keep them centralised as well as secret. Assuming that is the right way to do it, I understand there would be nothing stopping somebody from changing the code to Logger.log(key) for example but that is an acceptable risk.
What is a reasonably secure best practice way to keep keys hidden? I have in my head that it would be possible to have a separate script that can be referenced with as a library and that way also have the benefit of only needing to update the key once.
2
u/leftabomb Mar 18 '21
So the method I'm going with for now is to have a separate script called, for example, 'CENTRAL', and have private functions like this:
function auth_(){
//do stuff
return apiKey;
}
function genericUrlFetchApp(url,payload,method)
//does a normal UrlFetchApp with set params such as apiKey and variable params
var res = UrlFetchApp.fetch(url, params);
return res;
}Then on my script I include that as a library, and cannot see the auth() function. So would call the fetch like this:
const getResult = CENTRAL.genericUrlFetchApp('www','some JSON','GET');
const myObj = JSON.parse(getResult .getContentText());
const myValues= myObj .values;This keeps the API/auth stuff centralised and reasonably secure, but also keeps the code nice and tidy. The suggestion that loading a library takes longer is only applicable for UI scripts, for something like mucking about with an array or whatever isn't that noticeable.
Any feedback welcome!
3
u/Graphicnapkin Feb 12 '23
I've spent some time thinking about how to make AppsScript more secure and I built a small AppsScript library that fetches secrets from GCP Secrets Manager:
https://github.com/graphicnapkin/Google-Workspace-AppsScript-Utilities/tree/main/GASM.
I'm interested in any feedback on how useful / secure this solution is.
1
u/gigamosh57 Feb 26 '25 edited Feb 26 '25
I know this comment is 2 years old, but this was incredibly helpful and did exactly what it claims to do. Setup was easy and secrets are kept secret via Google's user auth schemes.
One update for others who find this is that your repository has been renamed and is now here:
https://github.com/graphicnapkin/Google-Workspace-AppsScript-Utilities/tree/main/ASM
3
u/Strel0k Mar 17 '21
Is the Sheet going to be publicly shared or something?
Lowest form of security would be to set and store the keys using the Properties Service. But this would only make them not be hard-coded and easily readable. Anyone with access to the code and a bit of knowledge would very quickly figure out what's going on.
One option would be to have a separate "API handler" script project that would make the API call and pass back the response. You could set it up to only make certain requests and sanitize the response if it contains any API / account keys.
This way if the Sheets bound script is compromised the API handler serves as a firewall