Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Environmental variables: Unable to prevent escaping #1919

Closed
AspireOne opened this issue Mar 28, 2024 · 2 comments
Closed

[Bug]: Environmental variables: Unable to prevent escaping #1919

AspireOne opened this issue Mar 28, 2024 · 2 comments

Comments

@AspireOne
Copy link

Description

Environmental variables containing a double quotation mark or a newline cannot be prevented from being escaped (wrapping it inside a single quotation mark does not work).

If I set the following environmental variable:

MY_JSON={"name": "John", "Address": "New York \n BigBen 173"}

It will get escaped, like this:

MY_JSON={\"name\": \"John\", \"Address\": \"New York \\n BigBen 173\"}

This is traditionally prevented by wrapping the value with a single quote ('), and it did work in last versions of Coolify. However, when I do that in the Developer View, it just removes the single quotes upon saving, and if I do it from the UI view of environmental variables, it keeps them there, but also escapes them, like so:

MY_JSON=\'{\"name\": \"John\", \"Address\": \"New York \\n BigBen 173\"}\'

This breaks e.g. apps that have a JSON inside theirs .env. If we were to parse this in code (e.g. JSON.parse(process.env.MY_JSON), it throws an error.

Minimal Reproduction (if possible, example repository)

  1. Set environmental variable MY_JSON={"name": "John", "Address": "New York \n BigBen 173"} in any project (I tested it on a NextJS instance)
  2. Retrieve the variable, e.g. via a project's "Command" section in Coolify by running "env"
  3. Observe the JSON being escaped, e.g. MY_JSON={\"name\": \"John\", \"Address\": \"New York \\n BigBen 173\"}

Exception or Error

No response

Version

v4.0.0-beta.248

@AspireOne
Copy link
Author

For anyone that needs a solution right now, since I failed to rectify it via Coolify environmental variables, I pre-processed the variable directly in code like so:

function unescapeJsonString(possiblyEscapedJsonString) {
  let correctedString = possiblyEscapedJsonString;

  // Check and conditionally remove leading and trailing single quotes
  if (correctedString.startsWith("'") && correctedString.endsWith("'")) {
    correctedString = correctedString.slice(1, -1);
  }

  // Replace escaped double quotes with actual double quotes only if needed
  if (correctedString.includes('\\"')) {
    correctedString = correctedString.replace(/\\"/g, '"');
  }

  // Replace escaped newlines with actual newline characters only if needed
  if (correctedString.includes("\\\\n")) {
    correctedString = correctedString.replace(/\\\\n/g, "\\n");
  }

  // Attempt to parse the corrected string into a JSON object
  try {
    return JSON.parse(correctedString);
  } catch (error) {
    throw new Error(`Error un-escaping JSON string: ${error}`);
  }
}

@andrasbacsai
Copy link
Member

I have added a literal environment variable option that will save the environment as is. Can you please try it?

@andrasbacsai andrasbacsai self-assigned this May 4, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2024
@linear linear bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants