Skip to content

Commit

Permalink
Add sound path checking (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 authored Sep 15, 2024
1 parent 73fea61 commit b63a112
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 1 addition & 2 deletions lua/starfall/libs_sh/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ end
-- @param number channel Default CHAN_AUTO or CHAN_WEAPON for weapons
function ents_methods:emitSound(snd, lvl, pitch, volume, channel)
checkluatype(snd, TYPE_STRING)
if #snd>260 then SF.Throw("Sound path too long!") end
if string.match(snd, "[\"?]") then SF.Throw("Sound path contains invalid characters!") end
snd = SF.CheckSound(snd)

local ent = getent(self)
checkpermission(instance, ent, "entities.emitSound")
Expand Down
4 changes: 1 addition & 3 deletions lua/starfall/libs_sh/sound.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ end)
function sound_library.create(ent, path, nofilter)
checkluatype(path, TYPE_STRING)
if nofilter~=nil then checkluatype(nofilter, TYPE_BOOL) end

if #path>260 then SF.Throw("Sound path too long!") end
if string.match(path, "[\"?]") then SF.Throw("Sound path contains invalid characters!") end
path = SF.CheckSound(path)

checkpermission(instance, { ent, path }, "sound.create")

Expand Down
22 changes: 21 additions & 1 deletion lua/starfall/sflib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,6 @@ function SF.CheckMaterial(material)
return mat
end


function SF.CheckModel(model, player, prop)
if #model > 260 then SF.Throw("Model path too long!", 3) end
model = SF.NormalizePath(string.lower(model))
Expand All @@ -1451,6 +1450,27 @@ function SF.CheckModel(model, player, prop)
return model
end

function SF.CheckSound(path)
-- Limit length and remove invalid chars
if #path>260 then SF.Throw("Sound path too long!", 3) end
path = SF.NormalizePath(string.gsub(path, "[\"?']", ""))

-- Extract sound flags. Only allowed flags are '<', '>', '^', ')'
local flags
flags, path = string.match(path, "^([^%w_/%.]*)(.*)")
if #flags==0 then
flags = nil
elseif #flags>2 or string.match(flags, "[^<>%^%)]") then
SF.Throw("Invalid sound flags! "..flags, 3)
end

if not (istable(sound.GetProperties(path)) or file.Exists("sound/" .. path, "GAME")) then
SF.Throw("Invalid sound path! "..path, 3)
end

return flags and (flags..path) or path
end

function SF.CheckRagdoll(model)
if #model > 260 then return false end
model = SF.NormalizePath(string.lower(model))
Expand Down

0 comments on commit b63a112

Please sign in to comment.