Skip to content

fix: getstorage talkaction#3200

Closed
arrudaqs wants to merge 6 commits intoopentibiabr:mainfrom
arrudaqs:patch-2
Closed

fix: getstorage talkaction#3200
arrudaqs wants to merge 6 commits intoopentibiabr:mainfrom
arrudaqs:patch-2

Conversation

@arrudaqs
Copy link
Copy Markdown
Contributor

@arrudaqs arrudaqs commented Dec 30, 2024

Description

Fix the getstorage talkaction used by server admins, which helps imensely with quest debugging.

Behaviour

Actual

Whenever the talkaction /getstorage Player, storagename is called, the server receives a string (storage name) or a number (storage key) as parameters, the key works fine, but the name should not be a string, it should be converted to the actual global storage variable type.

Expected

Given the admin prepares the /getstorage talkaction to be sent;
When the admin sends the storage parameter as a string e.g (/getstorage GOD, Storage.Quest.U8_0.TheIceIslands.Questline);
Then the /getstorage talkaction accepts the string parameter correctly.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested

  • Tested the talkaction before my fix, all string typed parameters returned -1 (e.g Storage.Quest.U8_0.BarbarianTest.Questline returned -1)
  • Tested the talkaction after my fix, all string typed parameters returned their actual value (e.g Storage.Quest.U8_0.BarbarianTest.Questline returned 8)

Test Configuration:

  • Server Version: 3.1.2
  • Client: 13.40
  • Operating System: Windows 10

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I checked the PR checks reports

@majestyotbr majestyotbr changed the title Fix getstorage talkaction fix: getstorage talkaction Dec 30, 2024
@arrudaqs
Copy link
Copy Markdown
Contributor Author

@dudantas please have a look 🤭

@majestyotbr majestyotbr requested a review from dudantas January 2, 2025 17:10
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Jan 2, 2025

@arrudaqs arrudaqs requested a review from dudantas January 2, 2025 19:25
@dudantas
Copy link
Copy Markdown
Member

dudantas commented Jan 2, 2025

Using loadstring here introduces potential security and performance risks, especially if the input (split[2]) is user-provided or not sanitized. A safer and more efficient approach would be to directly attempt converting the value to a number or treating it as a string key if the conversion fails. This would eliminate the need for dynamically executed code and improve both the safety and clarity of the implementation.

Example alternative:

local storageKey = tonumber(split[2]) or split[2]
local storageValue = target:getStorageValue(storageKey)
self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The storage with id: " .. split[1] .. " is: " .. storageValue .. ".")

This ensures the same functionality without the risks associated with loadstring.

Copy link
Copy Markdown
Member

@dudantas dudantas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, address what I said in the previous comment, as it is very critical.

Copy link
Copy Markdown
Contributor

@kaleohanopahala kaleohanopahala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah...
This changes seems unsafe to me.
Could you test this?

function Player.getStorageValueTalkaction(self, param)
    -- Sanity check for parameters
    if not HasValidTalkActionParams(self, param, "Usage: /getstorage <playername>, <storage key or name>") then
        return true
    end

    local split = param:split(",")
    if not split[2] then
        self:sendCancelMessage("Insufficient parameters.")
        return true
    end

    local target = Player(split[1]:trim())
    if not target then
        self:sendCancelMessage("A player with that name is not online.")
        return true
    end

    -- Storage key Validation
    local storageKey = tonumber(split[2]) or split[2]:trim()
    if not storageKey then
        self:sendCancelMessage("Invalid storage key or name.")
        return true
    end

    -- Get the storage key
    local storageValue = target:getStorageValue(storageKey)
    if storageValue == nil then
        self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The storage with id: " .. split[2] .. " does not exist or is not set for player " .. target:getName() .. ".")
    else
        self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "The storage with id: " .. split[2] .. " from player " .. target:getName() .. " is: " .. storageValue .. ".")
    end

    return true
end

local storageGet = TalkAction("/getstorage")

function storageGet.onSay(player, words, param)
    return player:getStorageValueTalkaction(param)
end

storageGet:separator(" ")
storageGet:groupType("gamemaster")
storageGet:register()

@majestyotbr majestyotbr marked this pull request as draft January 8, 2025 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants