Skip to content

Commit

Permalink
Validate attribute removals, handle reserved failures separately from…
Browse files Browse the repository at this point in the history
… invalid ones (#440)
  • Loading branch information
kennethloeffler committed Aug 22, 2024
1 parent ff8c90d commit 0ca78c5
Showing 1 changed file with 38 additions and 16 deletions.
54 changes: 38 additions & 16 deletions rbx_dom_lua/src/customProperties.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ local TERRAIN_MATERIAL_COLORS = {
Enum.Material.Pavement,
}

local function isAttributeNameValid(attributeName)
-- For SetAttribute to succeed, the attribute name must be less than or
-- equal to 100 characters...
return #attributeName <= 100
-- ...and must only contain alphanumeric characters, periods, hyphens,
-- underscores, or forward slashes.
and attributeName:match("[^%w%.%-_/]") == nil
end

local function isAttributeNameReserved(attributeName)
-- For SetAttribute to succeed, attribute names must not use the RBX
-- prefix, which is reserved by Roblox.
return attributeName:sub(1, 3) ~= "RBX"
end

-- Defines how to read and write properties that aren't directly scriptable.
--
-- The reflection database refers to these as having scriptability = "Custom"
Expand All @@ -40,26 +55,33 @@ return {
local didAllWritesSucceed = true

for attributeName, attributeValue in pairs(value) do
local isNameValid =
-- For our SetAttribute to succeed, the attribute name must be
-- less than or equal to 100 characters...
#attributeName <= 100
-- ...must only contain alphanumeric characters, periods, hyphens,
-- underscores, or forward slashes...
and attributeName:match("[^%w%.%-_/]") == nil
-- ... and must not use the RBX prefix, which is reserved by Roblox.
and attributeName:sub(1, 3) ~= "RBX"

if isNameValid then
instance:SetAttribute(attributeName, attributeValue)
else
if isAttributeNameReserved(attributeName) then
-- If the attribute name is reserved, then we don't
-- really care about reporting any failures about
-- it.
continue
end

if not isAttributeNameValid(attributeName) then
didAllWritesSucceed = false
continue
end

instance:SetAttribute(attributeName, attributeValue)
end

for key in pairs(existing) do
if value[key] == nil then
instance:SetAttribute(key, nil)
for existingAttributeName in pairs(existing) do
if isAttributeNameReserved(existingAttributeName) then
continue
end

if not isAttributeNameValid(existingAttributeName) then
didAllWritesSucceed = false
continue
end

if value[existingAttributeName] == nil then
instance:SetAttribute(existingAttributeName, nil)
end
end

Expand Down

0 comments on commit 0ca78c5

Please sign in to comment.