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

src: remove comments from env file #52120

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ using v8::String;
*/
const std::regex LINE(
"\\s*(?:export\\s+)?([\\w.-]+)(?:\\s*=\\s*?|:\\s+?)(\\s*'(?:\\\\'|[^']"
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*(?"
":#.*)?"); // NOLINT(whitespace/line_length)
")*'|\\s*\"(?:\\\\\"|[^\"])*\"|\\s*`(?:\\\\`|[^`])*`|[^#\r\n]+)?\\s*");

std::vector<std::string> Dotenv::GetPathFromArgs(
const std::vector<std::string>& args) {
Expand Down Expand Up @@ -104,6 +103,7 @@ Local<Object> Dotenv::ToObject(Environment* env) {
void Dotenv::ParseContent(const std::string_view content) {
std::string lines = std::string(content);
lines = std::regex_replace(lines, std::regex("\r\n?"), "\n");
lines = RemoveComments(lines);

std::smatch match;
while (std::regex_search(lines, match, LINE)) {
Expand All @@ -126,7 +126,7 @@ void Dotenv::ParseContent(const std::string_view content) {
}

// Remove surrounding quotes
value = trim_quotes(value);
value = TrimQuotes(value);

store_.insert_or_assign(std::string(key), value);
lines = match.suffix();
Expand Down Expand Up @@ -179,7 +179,7 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
}
}

std::string_view Dotenv::trim_quotes(std::string_view str) {
std::string_view Dotenv::TrimQuotes(std::string_view str) {
static const std::unordered_set<char> quotes = {'"', '\'', '`'};
if (str.size() >= 2 && quotes.count(str.front()) &&
quotes.count(str.back())) {
Expand All @@ -188,4 +188,21 @@ std::string_view Dotenv::trim_quotes(std::string_view str) {
return str;
}

std::string Dotenv::RemoveComments(const std::string& content) {
std::istringstream iss(content);
std::string result;
std::string line;

while (std::getline(iss, line)) {
auto firstNonWhitespace = line.find_first_not_of(" \t");
// Check if line is empty or starts with '#'
if (firstNonWhitespace == std::string::npos ||
line[firstNonWhitespace] == '#') {
continue;
}
result.append(line + "\n");
}
return result;
}

} // namespace node
3 changes: 2 additions & 1 deletion src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Dotenv {

private:
std::map<std::string, std::string> store_;
std::string_view trim_quotes(std::string_view str);
std::string_view TrimQuotes(std::string_view str);
std::string RemoveComments(const std::string& str);
};

} // namespace node
Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/dotenv/valid.env
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ EXPAND_NEWLINES="expand\nnew\nlines"
DONT_EXPAND_UNQUOTED=dontexpand\nnewlines
DONT_EXPAND_SQUOTED='dontexpand\nnewlines'
# COMMENTS=work
#BASIC=basic2
#BASIC=basic3
INLINE_COMMENTS=inline comments # work #very #well
INLINE_COMMENTS_SINGLE_QUOTES='inline comments outside of #singlequotes' # work
INLINE_COMMENTS_DOUBLE_QUOTES="inline comments outside of #doublequotes" # work
Expand All @@ -37,7 +39,7 @@ TRIM_SPACE_FROM_UNQUOTED= some spaced out string
[email protected]
SPACED_KEY = parsed
EDGE_CASE_INLINE_COMMENTS="VALUE1" # or "VALUE2" or "VALUE3"

export EXAMPLE = ignore export
MULTI_DOUBLE_QUOTED="THIS
IS
A
Expand All @@ -58,4 +60,3 @@ STRING`
MULTI_NOT_VALID_QUOTE="
MULTI_NOT_VALID=THIS
IS NOT MULTILINE
export EXAMPLE = ignore export