Skip to content

Commit

Permalink
(PUP-11515) Negative Lookbehind Regex Causes Duplicate Node
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
imaqsood committed Jul 22, 2024
1 parent a812d7c commit fadc4b8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/puppet/resource/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def instantiate_resource(scope, resource)

def name
if type == :node && name_is_regex?
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}__#{@name.object_id}"
else
@name
end
Expand Down
4 changes: 2 additions & 2 deletions spec/integration/parser/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ class foo {
end.not_to raise_error
end

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

it 'provides captures from the regex in the node body' do
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/resource/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
end

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

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

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

0 comments on commit fadc4b8

Please sign in to comment.