continue icon indicating copy to clipboard operation
continue copied to clipboard

storing api keys in plain text

Open bachittle opened this issue 2 years ago • 26 comments

Validations

  • [X] I believe this is a way to improve. I'll try to join the Continue Discord for questions
  • [X] I'm not able to find an open issue that requests the same enhancement

Problem

I don't like that all the API keys are stored in plain text in a JSON file at the user root. I know that this is required for VSCode, and every other plugin that uses API has to do this. It just doesn't sit right with me.

Solution

At the very least, put a .gitignore at the root of .continue to make sure config.json doesn't get indexed. It may also be possible to write external scripts that encrypt the config.json if the user wants to be security-conscious.

bachittle avatar Jul 12 '24 15:07 bachittle

Ideally one should be able to use environment variables for API keys and keep them out of the config entirely.

noamross avatar Sep 11 '24 21:09 noamross

Another vote. Support for environment variables or alternative secure storage would be a significant improvement.

rashabarbar avatar Sep 14 '24 22:09 rashabarbar

I too would support the idea of retrieving the API keys from the environment variables.

diogovalada avatar Oct 25 '24 20:10 diogovalada

I also feel like .continuerc.json is pretty much impossible to use, if I can't pull in secrets. Otherwise I can't commit this, when the whole point of the file is to share setup with the team? Or is that not the point? Should I gitignore this and let everybody else copy/paste this?

boennemann avatar Oct 31 '24 17:10 boennemann

Putting this into config.ts worked for us, allowing us to write things like ${USERNAME} and ${OPENAI_API_KEY} into our config.json and project-specific .continuerc.json files.

 /**
   * Recursively replace environment variable placeholders in an object.
   * 
   * This function traverses through the provided object and looks for strings 
   * containing placeholders in the format `${VAR_NAME}`. It then replaces 
   * these placeholders with the corresponding values from `process.env`. 
   * If the environment variable is not found, it replaces the placeholder with an empty string.
   * 
   * @param obj - The object in which placeholders should be replaced. This can be a deeply nested object.
   */
  const replaceEnvVars = (obj: any) => {
    for (const key in obj) {
      if (typeof obj[key] === 'string') {
      // Replace placeholders in the format ${VAR_NAME} with the value from process.env
        obj[key] = obj[key].replace(/\$\{(.+?)\}/g, (_: any, varName: string | number) => process.env[varName] || '');
      } else if (typeof obj[key] === 'object' && obj[key] !== null) {
      // Recursively call replaceEnvVars for nested objects
        replaceEnvVars(obj[key]);
      }
    }
  };

  const newConfig = JSON.parse(JSON.stringify(config)); // Create a deep copy of the config object
  replaceEnvVars(newConfig);
  return newConfig;

➛ A good bit of that was written by Continue/GPT as I don't normally typescript. :-)

Probably this should be pulled into the codebase, not required of everyone's custom config. But I'm not sure where.

Note: Continue handles comments in config.json, but not .continuerc.json. I'll make a quick ticket for that.

ctwardy avatar Nov 08 '24 20:11 ctwardy

@bachittle @noamross @rashabarbar @boennemann @ctwardy thoughts on this implementation?

  • Environment variables will be checked for in some specific config inputs like URLs and API keys (any others?)
  • They can be injected into those strings using mustache format e.g. {{ ENV_VAR_NAME }}
  • by default, the IDE checks .env files at the root of the directory
  • or, if envFiles = ["URI_1", "URI_2"] etc. is specified in config.json it checks those
  • If a specified var is not found, will throw error on config load and show in config issues page

RomneyDa avatar Nov 12 '24 00:11 RomneyDa

Would note that this issue is solved way more robustly for teams by Continue for Teams (in beta) since in this case would still need to share config files around and stuff. But this would be good for

  • Individual developers with no team (common)
  • Cases where within one team, one developer would use one model and another another, etc. (possible edge case for now)

RomneyDa avatar Nov 12 '24 00:11 RomneyDa

@bachittle @noamross @rashabarbar @boennemann @ctwardy thoughts on this implementation?

Those look good - I'd neglected to support .env but it really should. envFiles is a great idea too, and moustache-format (mumbo-format?) would be a sensible change.

In addition to URLs and API keys, I'd suggest:

  • Common user vars like USERPROFILE, HOME, USERNAME. For example, we use "userToken": "${USERNAME}", and "caBundlePath": [ "${USERPROFILE}/.continue/cacert.pem" ]

Anything we do now can be done with those. And simple is good as extensions are boundless. But they suggest two common cases:

  • CONTINUE_* like CONTINUE_USERTOKEN
  • CA_BUNDLE_PATH or similar, as certs are likely to be a common issue.

We're tracking the team version and will be interested when we can run the server behind our firewall. Totally understand that's not your first priority. But add a vote for the backlog.

ctwardy avatar Nov 12 '24 14:11 ctwardy

@RomneyDa , the solution looks great. Only:

by default, the IDE checks .env files at the root of the directory

If directory meaning "current workspace / working directory", there might be a risk.

It seems like a trend that coding assistance tools, including Continue, would build a map of the repo and inject files as context as LLM sees fit (smart context), and it's a risk that .env file could be sent to some remote model provider when the user ask something like "introduce me the structure of this project". Let the tool ignore dot files is sometimes a good practice, but can't promise every tool will do this.

Joilence avatar Nov 21 '24 23:11 Joilence

@Joilence good callout, I think to answer your question, for indexing and all directory walking including repo map generation, these files are ignored by default including .env files and anything in .gitignores and .continueignores: https://github.com/continuedev/continue/blob/2c597764cac076a6342625273e43f203b74f2da6/core/indexing/ignore.ts also check out the walkDir function

.env would only be checked when parsing config in that solution

@ctwardy Only supporting CONTINUE_ env variables with mustache templating could be a pretty solid solution. And maybe a select few others like HOME_DIR etc., agree

RomneyDa avatar Nov 25 '24 04:11 RomneyDa

Thumb up this issue, as it will ease config.json sharing.

Update on the workaround provided above: With continue 0.9.244 I ended up with this (mostly written by sonnet, there is maybe a better way) working for me:

  • ~/.continue/config.ts:
const replaceEnvVars = <T>(obj: T): T => {
  if (Array.isArray(obj)) {
    return obj.map(item => replaceEnvVars(item)) as T;
  }

  const result = { ...obj };
  for (const key in result) {
    if (typeof result[key] === 'string') {
      // Replace placeholders in the format ${VAR_NAME} with the value from process.env
      result[key] = result[key].replace(/\$\{(.+?)\}/g, (_, varName: string) =>
        process.env[varName] ?? ''
      );
    } else if (result[key] && typeof result[key] === 'object') {
      // Recursively call replaceEnvVars for nested objects
      result[key] = replaceEnvVars(result[key]);
    }
  }
  return result;
};

export function modifyConfig(config: Config): Config {
  return replaceEnvVars(config);
}
  • ~/.continue/config.json:
{
  "models": [
    {
      "model": "claude-3-5-sonnet-latest",
      "provider": "anthropic",
      "apiKey": "${CONTINUE_ANTHROPIC_API_KEY}",
      "title": "Claude 3.5 Sonnet"
    }
  ],
  "..."
}

baipaiha avatar Dec 17 '24 14:12 baipaiha

Hi all, with the new https://hub.continue.dev/ you can manage secrets for both an individual and team 😁

More info here: https://docs.continue.dev/hub/secrets/secret-types

Patrick-Erichsen avatar Mar 12 '25 17:03 Patrick-Erichsen

Is the scheme used by @baipaiha already implemented?

diogovalada avatar Mar 16 '25 22:03 diogovalada

Curious if there's an easy solution with the config.yaml format? I've made attempts to get this working, but so far have been unsuccessful.

I was hoping this user inputs would be compatible, but so far have had no luck. Seems this might only be compatible with hub secrets? if so, i'm not sure why the description states

Blocks can be passed user inputs, including hub secrets and raw text values.

e.g. should (can) this work with a local environment variable?

name: myprofile/custom-model
models:
  - name: My Favorite Model
    provider: anthropic
    apiKey: ${{ inputs.CUSTOM_API_KEY }}

scottrbaxter avatar Apr 12 '25 03:04 scottrbaxter

  • ~/.continue/config.json:

@baipaiha pretty neat, where do you define CONTINUE_ANTHROPIC_API_KEY? Do you specifically launch the editor with some bash script to achieve that or some other way? Thanks for the script!

FelikZ avatar May 13 '25 09:05 FelikZ

@FelikZ is start code from terminal, where my shell have the env vars set, but a simple code.sh that set the vars before calling code should be fine

baipaiha avatar Jul 18 '25 20:07 baipaiha

This issue hasn't been updated in 90 days and will be closed after an additional 10 days without activity. If it's still important, please leave a comment and share any new information that would help us address the issue.

github-actions[bot] avatar Oct 17 '25 02:10 github-actions[bot]

This issue has not been resolved and users are still waiting on a solution, please do not auto-close.

scottrbaxter avatar Oct 18 '25 16:10 scottrbaxter

This issue hasn't been updated in 90 days and will be closed after an additional 10 days without activity. If it's still important, please leave a comment and share any new information that would help us address the issue.

github-actions[bot] avatar Jan 18 '26 02:01 github-actions[bot]

Has this issue been addressed formally, elsewhere yet? if not, it'd be great if devs could at least acknowledge whether/not this is planned for the roadmap so that it can be either be worked on or closed as not planned. would be unfortunate if something like this would simply get closed as stale simply due to no response.

scottrbaxter avatar Jan 18 '26 14:01 scottrbaxter