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

fix: improve setDisplayName security & optimize #1919

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions skymp5-client/src/services/services/remoteServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,8 @@ export class RemoteServer extends ClientListener {

const replaceValue = refr.getBaseObject()?.getName();

if (replaceValue !== undefined && replaceValue !== "%original_name%") {
// SP doesn't support String.replaceAll because Chakracore doesn't
while (displayName.includes("%original_name%")) {
displayName = displayName.replace("%original_name%", replaceValue);
}
if (replaceValue !== undefined) {
displayName = displayName.replace(/%original_name%/g, replaceValue);
}
else {
logError(this, "Couldn't get a replaceValue for SetDisplayName, refr.getFormID() was", refr.getFormID().toString(16));
Expand Down
7 changes: 2 additions & 5 deletions skymp5-client/src/services/services/spSnippetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ export class SpSnippetService extends ClientListener {

const replaceValue = self?.getBaseObject()?.getName();

if (replaceValue !== undefined && replaceValue !== "%original_name%") {
// SP doesn't support String.replaceAll because Chakracore doesn't
while (newName.includes("%original_name%")) {
newName = newName.replace("%original_name%", replaceValue);
}
if (replaceValue !== undefined) {
newName = newName.replace(/%original_name%/g, replaceValue);
snippet.arguments[0] = newName;
}
else {
Expand Down
3 changes: 2 additions & 1 deletion skymp5-server/cpp/server_guest_lib/MpObjectReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ void MpObjectReference::SetNodeScale(const std::string& node, float scale,
});
}

void MpObjectReference::SetDisplayName(const std::string& newName)
void MpObjectReference::SetDisplayName(
const std::optional<std::string>& newName)
{
EditChangeForm(
[&](MpChangeForm& changeForm) { changeForm.displayName = newName; });
Expand Down
2 changes: 1 addition & 1 deletion skymp5-server/cpp/server_guest_lib/MpObjectReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class MpObjectReference
const espm::LookupResult& textureSet,
bool firstPerson);
void SetNodeScale(const std::string& node, float scale, bool firstPerson);
void SetDisplayName(const std::string& newName);
void SetDisplayName(const std::optional<std::string>& newName);

const std::set<MpObjectReference*>& GetListeners() const;
const std::set<MpObjectReference*>& GetEmitters() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,12 @@ VarValue PapyrusObjectReference::SetDisplayName(
throw std::runtime_error("SetDisplayName requires at least 2 arguments");
}
const char* displayName = static_cast<const char*>(arguments[0]);
selfRefr->SetDisplayName(displayName);

if (!strcmp(displayName, kOriginalNameExpression)) {
selfRefr->SetDisplayName(std::nullopt);
} else {
selfRefr->SetDisplayName(displayName);
}

bool force = static_cast<bool>(arguments[1]);
std::ignore = force;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class PapyrusObjectReference final
VarValue IsContainerEmpty(VarValue self,
const std::vector<VarValue>& arguments);

static constexpr auto kOriginalNameExpression = R"(%original_name%)";

// %original_name% will be replaced with the original localized name
// client-side
VarValue SetDisplayName(VarValue self,
Expand Down
Loading