Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findPlayer should search by 1 type at a time (bug/exploit fix) #3261

Merged
merged 2 commits into from
Jun 16, 2024

Conversation

lionbryce
Copy link
Contributor

Someone noticed that setting your steam name to someone else's in game name (or a portion of it) can goof up targeting due to the order that it scans

example:

player 1's nickname is "bob", steamname is "ABC123 OtherWords"
player 2's nickname is "123xyz", steamname is "The Crush Inator (idk)"
doing "/pm 123 AHHHHHH!" would target player1 as their steamname has 123 in it

I threw on some extra conditionals to avoid ever doing 4 loops of player.GetAll(), worst case is gonna be 3

could throw in a length check as 33 characters is too long for any of them and steam64s have a fixed length (or at least a minimum length)

@lionbryce lionbryce changed the title findPlayer info search by 1 type at a time (bug/exploit fix) findPlayer should search by 1 type at a time (bug/exploit fix) May 24, 2024
@FPtje
Copy link
Owner

FPtje commented May 26, 2024

Thanks for the PR. Since this function has quite precise behavior, it'll need a close review. I'll take a look when I have time.

@FPtje
Copy link
Owner

FPtje commented Jun 16, 2024

This PR gives a very interesting insight. I was under the impression that findPlayer would always first match by UserID, then by SteamID, then by nickname, and then by steam name. This is not the case, though, because it checks everything per player. The order in which players are iterated over can decide who gets chosen! That's not desirable behavior, because that's hard to predict.

If I were to write a unit test for this function, it would be that it should always return the same player, regardless of the order of players.

I think your PR is great and really solves a problem with findPlayer Let me clean it up and merge it!

Copy link
Owner

@FPtje FPtje left a comment

Choose a reason for hiding this comment

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

I think this won't even be (much) slower than the old code. The old code did one loop, with 4 things inside the loop. This thing does 3-4 loops at most, and does one thing per loop.

I think in some cases (e.g. when matching a UserID), this version might actually be faster, because it shortcuts the other loops.

Doing a for-loop like this is cheap, even in the worst case I think this function will have very good performance.

Thanks for this PR!

end

if info == v:SteamID() then
return v
for k = 1, count do -- this loop could likely be combined with the above loop
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this loop should be combined with the above loop, because then the order of players can influence the answer again

end
end

if string.StartsWith(lowerInfo, "steam_") then
Copy link
Owner

Choose a reason for hiding this comment

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

Good idea to add this check, it saves a loop

end

if info == v:SteamID() then
return v
for k = 1, count do
Copy link
Owner

Choose a reason for hiding this comment

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

A future optimization here could be to only run this loop when info is a valid SteamID64. SteamID64s have a specific format to them, and not nearly every int is a valid SteamID64.

A function isValidSteamID64(some_number) is a bit of a challenge to write, though, as it requires a thorough understanding of what makes a valid SteamID64.

@FPtje FPtje merged commit d0da7bc into FPtje:master Jun 16, 2024
2 checks passed
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.

2 participants