Skip to content

Commit

Permalink
Add regex check to gates and e2 find (#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 authored Mar 30, 2024
1 parent 6d7f570 commit c889b71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lua/entities/gmod_wire_expression2/core/find.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local function replace_match(a,b)
return string.match( string.Replace(a,"-","__"), string.Replace(b,"-","__") )
end

-- String used to check regex complexities
local sample_string = string.rep(" ", 40)

-- -- some generic filter criteria -- --

local function filter_all() return true end
Expand Down Expand Up @@ -516,12 +519,14 @@ end

--- Exclude entities with this model (or partial model name) from future finds
e2function void findExcludeModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
self.data.find.bl_model[string.lower(model)] = true
invalidate_filters(self)
end

--- Exclude entities with this class (or partial class name) from future finds
e2function void findExcludeClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", nil) end
self.data.find.bl_class[string.lower(class)] = true
invalidate_filters(self)
end
Expand Down Expand Up @@ -574,12 +579,14 @@ end

--- Remove entities with this model (or partial model name) from the blacklist
e2function void findAllowModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
self.data.find.bl_model[string.lower(model)] = nil
invalidate_filters(self)
end

--- Remove entities with this class (or partial class name) from the blacklist
e2function void findAllowClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", nil) end
self.data.find.bl_class[string.lower(class)] = nil
invalidate_filters(self)
end
Expand Down Expand Up @@ -632,12 +639,14 @@ end

--- Include entities with this model (or partial model name) in future finds, and remove others not in the whitelist
e2function void findIncludeModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
self.data.find.wl_model[string.lower(model)] = true
invalidate_filters(self)
end

--- Include entities with this class (or partial class name) in future finds, and remove others not in the whitelist
e2function void findIncludeClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", nil) end
self.data.find.wl_class[string.lower(class)] = true
invalidate_filters(self)
end
Expand Down Expand Up @@ -690,12 +699,14 @@ end

--- Remove entities with this model (or partial model name) from the whitelist
e2function void findDisallowModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", nil) end
self.data.find.wl_model[string.lower(model)] = nil
invalidate_filters(self)
end

--- Remove entities with this class (or partial class name) from the whitelist
e2function void findDisallowClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", nil) end
self.data.find.wl_class[string.lower(class)] = nil
invalidate_filters(self)
end
Expand Down Expand Up @@ -865,6 +876,7 @@ end

--- Filters the list of entities by removing all entities that are NOT of this class
e2function number findClipToClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", 0) end
class = string.lower(class)
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
Expand All @@ -874,6 +886,7 @@ end

--- Filters the list of entities by removing all entities that are of this class
e2function number findClipFromClass(string class)
if not pcall(WireLib.CheckRegex, sample_string, class) then return self:throw("Search string too complex!", 0) end
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
return not replace_match(string.lower(ent:GetClass()), class)
Expand All @@ -882,6 +895,7 @@ end

--- Filters the list of entities by removing all entities that do NOT have this model
e2function number findClipToModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", 0) end
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
return replace_match(string.lower(ent:GetModel() or ""), model)
Expand All @@ -890,6 +904,7 @@ end

--- Filters the list of entities by removing all entities that do have this model
e2function number findClipFromModel(string model)
if not pcall(WireLib.CheckRegex, sample_string, model) then return self:throw("Search string too complex!", 0) end
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
return not replace_match(string.lower(ent:GetModel() or ""), model)
Expand All @@ -898,6 +913,7 @@ end

--- Filters the list of entities by removing all entities that do NOT have this name
e2function number findClipToName(string name)
if not pcall(WireLib.CheckRegex, sample_string, name) then return self:throw("Search string too complex!", 0) end
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
return replace_match(string.lower(ent:GetName()), name)
Expand All @@ -906,6 +922,7 @@ end

--- Filters the list of entities by removing all entities that do have this name
e2function number findClipFromName(string name)
if not pcall(WireLib.CheckRegex, sample_string, name) then return self:throw("Search string too complex!", 0) end
return applyClip(self, function(ent)
if !IsValid(ent) then return false end
return not replace_match(string.lower(ent:GetName()), name)
Expand Down
1 change: 1 addition & 0 deletions lua/wire/gates/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ GateActions["string_replace"] = {
if not B then B = "" end
if not C then C = "" end
if #A + #B + #C > MAX_LEN then return false end
if not pcall(WireLib.CheckRegex, A, B) then return false end
return string.gsub(A,B,C)
end,
label = function(Out, A, B, C)
Expand Down

0 comments on commit c889b71

Please sign in to comment.