Skip to content
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
3 changes: 2 additions & 1 deletion src/dotenv.net/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class Parser
private const string DoubleQuotes = "\"";

private static readonly Regex IsQuotedLineStart = new("^[a-zA-Z0-9_ .-]+=\\s*\".*$", RegexOptions.Compiled);
private static readonly Regex IsQuotedLineEnd = new("(?<!\\\\)\"\\s*$", RegexOptions.Compiled);
private static readonly Regex IsQuotedLineEnd = new(@"(?<!\\)(?:(?<=^|[^\\])(\\\\)*|^)\s*""\s*$", RegexOptions.Compiled);

internal static ReadOnlySpan<KeyValuePair<string, string>> Parse(ReadOnlySpan<string> rawEnvRows,
bool trimValues)
Expand Down Expand Up @@ -86,6 +86,7 @@ private static string StripQuotes(this string value)
modified = true;
}

trimmed = trimmed.Replace(@"\\", @"\").Replace("\\\"", "\"");;

return modified ? trimmed : value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/dotenv.net/dotenv.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<RepositoryUrl>https://github.com/bolorundurowb/dotenv.net</RepositoryUrl>
<LangVersion>default</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageVersion>3.2.1</PackageVersion>
<PackageVersion>3.3.0</PackageVersion>
<RepositoryType>git</RepositoryType>
<PackageTags>dotnet, environment, variables, env, dotenv, net core, autofac</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>3.2.1</AssemblyVersion>
<FileVersion>3.2.1</FileVersion>
<AssemblyVersion>3.3.0</AssemblyVersion>
<FileVersion>3.3.0</FileVersion>
<NeutralLanguage>en-NG</NeutralLanguage>
<TargetFrameworks>netstandard1.6;netstandard2.0;netstandard2.1</TargetFrameworks>
<Title>dotenv.net</Title>
Expand Down
29 changes: 29 additions & 0 deletions tests/dotenv.net.Tests/DotEnv.Fluent.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DotEnvFluentTests
private const string AsciiEnvFileName = "ascii.env";
private const string GenericEnvFileName = "generic.env";
private const string IncompleteEnvFileName = "incomplete.env";
private const string EscapedCharactersEnvFileName = "escape.env";

[Fact]
public void ConfigShouldThrowWithNonExistentEnvAndTrackedExceptions()
Expand Down Expand Up @@ -139,6 +140,9 @@ public void ConfigLoadsMultilineEnvs()
EnvReader.GetStringValue("DOUBLE_QUOTE")
.Should()
.Be("double");
EnvReader.GetStringValue("DOUBLE_QUOTE_EQUALS2")
.Should()
.Be("double=2");
EnvReader.GetStringValue("DOUBLE_QUOTE_MULTI_LINE")
.Should()
.Be($"dou{Environment.NewLine}bler");
Expand Down Expand Up @@ -168,4 +172,29 @@ public void ConfigShouldLoadEnvWithInvalidEnvEntries()
.Should()
.BeFalse();
}

[Fact]
public void ConfigLoadsEnvWithEscapedCharacters()
{
DotEnv.Fluent()
.WithEnvFiles(EscapedCharactersEnvFileName)
.WithoutTrimValues()
.Load();

EnvReader.GetStringValue("unescaped")
.Should()
.Be("unescaped Value\\");

EnvReader.GetStringValue("more_escaped")
.Should()
.Be("more escaped Value\\\\");

EnvReader.GetStringValue("escaped")
.Should()
.Be($"escaped Value\"{Environment.NewLine}secondLine");

EnvReader.GetStringValue("escape_in_the_middle")
.Should()
.Be("this time we \" escaped in the middle");
}
}
3 changes: 3 additions & 0 deletions tests/dotenv.net.Tests/dotenv.net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<None Update="ascii.env">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="escape.env">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="generic.env">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
6 changes: 6 additions & 0 deletions tests/dotenv.net.Tests/escape.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
unescaped="unescaped Value\\"
more_escaped="more escaped Value\\\\"
escaped="escaped Value\"
secondLine"
irrelevantValue=""
escape_in_the_middle="this time we \" escaped in the middle"
1 change: 1 addition & 0 deletions tests/dotenv.net.Tests/multi-lines.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DOUBLE_QUOTE="double"
DOUBLE_QUOTE_EQUALS2="double=2"
DOUBLE_QUOTE_MULTI_LINE="dou
bler"
DOUBLE_QUOTE_EVEN_MORE_LINES="dou
Expand Down