r/GoogleAppsScript • • 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.

6 Upvotes

8 comments sorted by

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

1

u/leftabomb Mar 17 '21

The sheet will only be used internally, never shared outside the organisation. However, ideally I would like to hide the key from the user for IF they look.

The property service is a good idea, however some keys need to be generated by a script, so at the moment I might do something like let key = getKey().

Additionally, if keys or related set up changes, it would be useful to have this centralised so I don't need to update vars such as keys several times over. That's where I thought using a library could work. Perhaps by making a script with getKey() then having a given script fetch it as a library.

1

u/Strel0k Mar 17 '21

A library could work I just have no experience with how secure they are, you should look into it.

1

u/davchana Mar 17 '21

I think a library will work. It will be slower as compared to inline code, but still, it will work. A script user will just see the library name, & function names it provides, & output if those functions. Library can take the input or function name, & can return the response. Think of this as an hidden functions. You can use them, but can see their internal code.

1

u/leftabomb Mar 17 '21

Is your API handler suggestion like my library idea? How would that work exactly? Thanks

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