coolify icon indicating copy to clipboard operation
coolify copied to clipboard

[Bug]: Environmental variables: Unable to prevent escaping

Open AspireOne opened this issue 10 months ago • 1 comments

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 avatar Mar 28 '24 20:03 AspireOne

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}`);
  }
}

AspireOne avatar Mar 28 '24 20:03 AspireOne

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

andrasbacsai avatar Apr 26 '24 09:04 andrasbacsai