Skip to content

Improve string escaping for HCL interpolation #224

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

Merged
merged 3 commits into from
May 5, 2025
Merged
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
18 changes: 14 additions & 4 deletions hcl2/reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,21 @@ def _name_to_identifier(name: str) -> Tree:

@staticmethod
def _escape_interpolated_str(interp_s: str) -> str:
# begin by doing basic JSON string escaping, to add backslashes
interp_s = json.dumps(interp_s)

if interp_s.strip().startswith('<<-') or interp_s.strip().startswith('<<'):
# For heredoc strings, preserve their format exactly
return reverse_quotes_within_interpolation(interp_s)
# Escape backslashes first (very important to do this first)
escaped = interp_s.replace('\\', '\\\\')
# Escape quotes
escaped = escaped.replace('"', '\\"')
# Escape control characters
escaped = escaped.replace('\n', '\\n')
escaped = escaped.replace('\r', '\\r')
escaped = escaped.replace('\t', '\\t')
escaped = escaped.replace('\b', '\\b')
escaped = escaped.replace('\f', '\\f')
# find each interpolation within the string and remove the backslashes
interp_s = reverse_quotes_within_interpolation(interp_s)
interp_s = reverse_quotes_within_interpolation(f'"{escaped}"')
return interp_s

@staticmethod
Expand Down
20 changes: 20 additions & 0 deletions test/helpers/terraform-config-json/unicode_strings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"locals": [
{
"basic_unicode": "Hello, 世界! こんにちは Привет नमस्ते",
"unicode_escapes": "© ♥ ♪ ☠ ☺",
"emoji_string": "🚀 🌍 🔥 🎉",
"rtl_text": "English and العربية text mixed",
"complex_unicode": "Python (파이썬) es 很棒的! ♥ αβγδ",
"ascii": "ASCII: abc123",
"emoji": "Emoji: 🚀🌍🔥🎉",
"math": "Math: ∑∫√∞≠≤≥",
"currency": "Currency: £€¥₹₽₩",
"arrows": "Arrows: ←↑→↓↔↕",
"cjk": "CJK: 你好世界안녕하세요こんにちは",
"cyrillic": "Cyrillic: Привет мир",
"special": "Special: ©®™§¶†‡",
"mixed_content": "Line with interpolation: ${var.name}\nLine with emoji: 👨‍👩‍👧‍👦\nLine with quotes: \"quoted text\"\nLine with backslash: \\escaped"
}
]
}
21 changes: 21 additions & 0 deletions test/helpers/terraform-config/unicode_strings.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
locals {
basic_unicode = "Hello, 世界! こんにちは Привет नमस्ते"
unicode_escapes = "© ♥ ♪ ☠ ☺"
emoji_string = "🚀 🌍 🔥 🎉"
rtl_text = "English and العربية text mixed"
complex_unicode = "Python (파이썬) es 很棒的! ♥ αβγδ"
ascii = "ASCII: abc123"
emoji = "Emoji: 🚀🌍🔥🎉"
math = "Math: ∑∫√∞≠≤≥"
currency = "Currency: £€¥₹₽₩"
arrows = "Arrows: ←↑→↓↔↕"
cjk = "CJK: 你好世界안녕하세요こんにちは"
cyrillic = "Cyrillic: Привет мир"
special = "Special: ©®™§¶†‡"
mixed_content = <<-EOT
Line with interpolation: ${var.name}
Line with emoji: 👨‍👩‍👧‍👦
Line with quotes: "quoted text"
Line with backslash: \escaped
EOT
}