Skip to content

Commit 5d09d7f

Browse files
committed
(PUP-11515) Negative Lookbehind Regex Causes Duplicate Node
Before this commit, two nodes could collide due to the lookaround Regex pattern. This issue arose because the regex was converted to a regular string by ignoring characters other than a-z, 0-9, _, and -. For example, /(?<!a)sync/ was converted to "__node_regexp__async," and /async/ was also converted to "__node_regexp__async," causing an error that the node was already defined. To prevent such duplication, this commit normalize the lookarround pattern from the regex, ensuring a unique string in case of lookarround pattern conflict. For example, /(?<!a)sync/ will converted to "__node_regexp__LPQULTEXaRPsync" replacing (?<!a) to "LPQULTEXaRP" while converting to the string.
1 parent bda54a7 commit 5d09d7f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: lib/puppet/resource/type.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class Puppet::Resource::Type
3333
DOUBLE_COLON = '::'
3434
EMPTY_ARRAY = [].freeze
3535

36+
LOOKAROUND_OPERATORS = {
37+
"(" => 'LP',
38+
"?" => "QU",
39+
"<" => "LT",
40+
">" => "GT",
41+
"!" => "EX",
42+
"=" => "EQ",
43+
")" => 'RP'
44+
}.freeze
45+
3646
attr_accessor :file, :line, :doc, :code, :parent, :resource_type_collection, :override
3747
attr_reader :namespace, :arguments, :behaves_like, :module_name
3848

@@ -196,7 +206,11 @@ def instantiate_resource(scope, resource)
196206

197207
def name
198208
if type == :node && name_is_regex?
199-
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
209+
# Normalize lookarround regex patthern
210+
internal_name = @name.source.downcase.gsub(/\(\?[^)]*\)/) do |str|
211+
str.gsub(/./) { |ch| LOOKAROUND_OPERATORS[ch] || ch }
212+
end
213+
"__node_regexp__#{internal_name.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
200214
else
201215
@name
202216
end

Diff for: spec/integration/parser/node_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ class foo {
8585
end.not_to raise_error
8686
end
8787

88+
it 'does not raise an error with 2 regex node names are the same due to lookarround pattern' do
89+
expect do
90+
compile_to_catalog(<<-MANIFEST, Puppet::Node.new("async"))
91+
node /(?<!a)sync/ { }
92+
node /async/ { }
93+
MANIFEST
94+
end.not_to raise_error
95+
end
96+
8897
it 'errors when two nodes with regexes collide after some regex syntax is removed' do
8998
expect do
9099
compile_to_catalog(<<-MANIFEST)

0 commit comments

Comments
 (0)