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

Unescape values correctly #209

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
10 changes: 7 additions & 3 deletions lib/dotenv/parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "dotenv/substitutions/variable"
require "dotenv/substitutions/command" if RUBY_VERSION > "1.8.7"
require "dotenv/substitutions/unescape"

module Dotenv
class FormatError < SyntaxError; end
Expand All @@ -8,8 +9,11 @@ class FormatError < SyntaxError; end
# and stored in the Environment. It allows for variable substitutions and
# exporting of variables.
class Parser
@substitutions =
Substitutions.constants.map { |const| Substitutions.const_get(const) }
@substitutions = [
Substitutions::Variable,
Substitutions.const_defined?(:Command) ? Substitutions::Command : nil,
Substitutions::Unescape
].compact

LINE = /
\A
Expand Down Expand Up @@ -79,7 +83,7 @@ def parse_value(value)
end

def unescape_characters(value)
value.gsub(/\\([^$])/, '\1')
value.gsub(/([^\\]|^)\\([^$\\])/, '\1\2')
end

def expand_newlines(value)
Expand Down
18 changes: 18 additions & 0 deletions lib/dotenv/substitutions/unescape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "English"

module Dotenv
module Substitutions
# Substitute "\$" to "$"
#
module Unescape
class << self
ESCAPED_DOLLARS = /\\\$/
ESCAPED_BACKSLASHES = /\\\\/

def call(value, _env)
value.gsub(ESCAPED_DOLLARS, '$').gsub(ESCAPED_BACKSLASHES, "\\")
end
end
end
end
end
16 changes: 8 additions & 8 deletions lib/dotenv/substitutions/variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ module Substitutions
module Variable
class << self
VARIABLE = /
(\\)? # is it escaped with a backslash?
(\$) # literal $
\{? # allow brace wrapping
([A-Z0-9_]+) # match the variable
\}? # closing brace
(([^\\]|^)\\)? # is it escaped with a backslash but not escaped backslash?
(\$) # literal $
\{? # allow brace wrapping
([A-Z0-9_]+) # match the variable
\}? # closing brace
/xi

def call(value, env)
value.gsub(VARIABLE) do |variable|
match = $LAST_MATCH_INFO

if match[1] == '\\'
variable[1..-1]
if match[1]
match[2] + variable[match[1].size .. -1]
else
env.fetch(match[3]) { ENV[match[3]] }
env.fetch(match[4]) { ENV[match[4]] }
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/dotenv/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def env(string)
.to eql("FOO" => "test", "BAR" => "foo${FOO} test")
end

it "unescapes correctly" do
expect(env("FOO=\"foo\\$.test\"")).to eql("FOO" => "foo$.test")
expect(env("FOO=\"foo\\$\"")).to eql("FOO" => "foo$")
expect(env("FOO=\"foo\\$.\"")).to eql("FOO" => "foo$.")
expect(env("BAR=bar\nFOO=\"foo\\\\$BAR\"")).to eql("BAR" => "bar", "FOO" => "foo\\bar")
end

it "parses yaml style options" do
expect(env("OPTION_A: 1")).to eql("OPTION_A" => "1")
end
Expand Down