Skip to content

Commit fadc4b8

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 appends the object_id of the regex at the end, ensuring a unique string every time the regex is used.
1 parent a812d7c commit fadc4b8

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def instantiate_resource(scope, resource)
196196

197197
def name
198198
if type == :node && name_is_regex?
199-
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
199+
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}__#{@name.object_id}"
200200
else
201201
@name
202202
end

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

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

88-
it 'errors when two nodes with regexes collide after some regex syntax is removed' do
88+
it 'do not raise an error when two nodes with regexes collide after some regex syntax is removed' do
8989
expect do
9090
compile_to_catalog(<<-MANIFEST)
9191
node /a.*(c)?/ { }
9292
node /a.*c/ { }
9393
MANIFEST
94-
end.to raise_error(Puppet::Error, /Node '__node_regexp__a.c' is already defined/)
94+
end.not_to raise_error
9595
end
9696

9797
it 'provides captures from the regex in the node body' do

Diff for: spec/unit/resource/type_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@
6060
end
6161

6262
it "should return the regex converted to a string when asked for its name" do
63-
expect(Puppet::Resource::Type.new(:node, /ww/).name).to eq("__node_regexp__ww")
63+
expect(Puppet::Resource::Type.new(:node, /ww/).name).to match(/__node_regexp__ww__\d+/)
6464
end
6565

6666
it "should downcase the regex when returning the name as a string" do
67-
expect(Puppet::Resource::Type.new(:node, /W/).name).to eq("__node_regexp__w")
67+
expect(Puppet::Resource::Type.new(:node, /W/).name).to match(/__node_regexp__w__\d+/)
6868
end
6969

7070
it "should remove non-alpha characters when returning the name as a string" do

0 commit comments

Comments
 (0)