Use UnsafeRelaxed JSON serialization globally

Replaces all usages of JsonSerializer.Serialize with the UnsafeRelaxed option via a new JsonOptions.UnsafeRelaxed static property. This ensures consistent relaxed escaping for all JSON output, improving compatibility with consumers expecting unescaped JSON. Refactors affected command, service, and static classes to use the new option.
This commit is contained in:
Madpeter 2025-10-05 20:07:42 +01:00
parent 811baa74bc
commit 008cb44e24
20 changed files with 69 additions and 67 deletions

View file

@ -95,7 +95,7 @@ namespace SecondBotEvents.Commands
BetterNearMe.Add(details);
}
}
return BasicReply(JsonSerializer.Serialize(BetterNearMe));
return BasicReply(JsonSerializer.Serialize(BetterNearMe, JsonOptions.UnsafeRelaxed));
}
[About("Requests the given avatars profile image")]
@ -228,7 +228,7 @@ namespace SecondBotEvents.Commands
NearMe.Add(av.ID, av.Name);
}
}
return BasicReply(JsonSerializer.Serialize(NearMe));
return BasicReply(JsonSerializer.Serialize(NearMe, JsonOptions.UnsafeRelaxed));
}
[About("searchs the AV database if not found triggers a lookup")]

View file

@ -12,7 +12,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object LocalChatHistory()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetLocalChat()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetLocalChat(), JsonOptions.UnsafeRelaxed));
}
[About("sends a message to localchat (Normal chat)")]
@ -108,7 +108,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object chatwindows()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindows()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindows(), JsonOptions.UnsafeRelaxed));
}
[About("gets a list of chat windows from avatars with unread messages")]
@ -116,7 +116,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object listwithunread()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindowsUnread()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindowsUnread(), JsonOptions.UnsafeRelaxed));
}
[About("gets if there are any unread im messages from avatars at all")]
@ -139,7 +139,7 @@ namespace SecondBotEvents.Commands
{
return Failure("avatar UUID invaild");
}
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindow(avatarUUID)));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAvatarImWindow(avatarUUID), JsonOptions.UnsafeRelaxed));
}
}

View file

@ -63,7 +63,7 @@ namespace SecondBotEvents.Commands
{
return Failure(reply.Value.First());
}
return BasicReply(JsonSerializer.Serialize(reply.Value));
return BasicReply(JsonSerializer.Serialize(reply.Value, JsonOptions.UnsafeRelaxed));
}
[About("Clears messages on the server sent by the member in the last 13 days, 22hours 59mins")]

View file

@ -380,7 +380,7 @@ namespace SecondBotEvents.Commands
EstateName = result.EstateName.ToString(),
EstateOwnerID = result.EstateOwnerID.ToString(),
CovenantText = covenantAssetRead.Value
}));
}, JsonOptions.UnsafeRelaxed));
}
[About("Sets the extended region info for the sim you are on")]
@ -638,7 +638,7 @@ namespace SecondBotEvents.Commands
{ "X", region.X.ToString() },
{ "Y", region.Y.ToString() }
};
return BasicReply(JsonSerializer.Serialize(reply));
return BasicReply(JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed));
}
[About("Requests the estate banlist")]
@ -646,7 +646,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object GetEstateBanList()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetEstateBans()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetEstateBans(), JsonOptions.UnsafeRelaxed));
}
[About("Attempts to add/remove the avatar to/from the Estate banlist")]

View file

@ -34,7 +34,7 @@ namespace SecondBotEvents.Commands
{
return Failure("no map access or updating", [avatar]);
}
return BasicReply(JsonSerializer.Serialize(loc), [avatar]);
return BasicReply(JsonSerializer.Serialize(loc, JsonOptions.UnsafeRelaxed), [avatar]);
}
[About("Gets the friendslist")]
@ -56,7 +56,7 @@ namespace SecondBotEvents.Commands
CleanedFriendsList.Add(entry);
index++;
}
return BasicReply(JsonSerializer.Serialize(CleanedFriendsList));
return BasicReply(JsonSerializer.Serialize(CleanedFriendsList, JsonOptions.UnsafeRelaxed));
}
[About("Updates the friend perms for avatar avatar to State \n if true grants (Online/Map/Modify) perms")]

View file

@ -41,7 +41,7 @@ namespace SecondBotEvents.Commands
{
return Failure("Invaild group UUID", [group]);
}
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupMembers(groupuuid)));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupMembers(groupuuid), JsonOptions.UnsafeRelaxed));
}
[About("Attempts to ban/unban a given avatar from a group")]
@ -328,7 +328,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object GetGroupList()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroups()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroups(), JsonOptions.UnsafeRelaxed));
}
[About("Forces the bot to reload the groups list")]
@ -357,7 +357,7 @@ namespace SecondBotEvents.Commands
{
return Failure("Unknown group", [group]);
}
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupRoles(groupuuid)));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupRoles(groupuuid), JsonOptions.UnsafeRelaxed));
}
[About("fetchs a list of all groups with unread messages")]
@ -365,7 +365,7 @@ namespace SecondBotEvents.Commands
[CmdTypeGet()]
public object GroupchatListAllUnreadGroups()
{
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAllGroupsChatWithUnread()));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetAllGroupsChatWithUnread(), JsonOptions.UnsafeRelaxed));
}
[About("checks if a given group has any unread messages")]
@ -416,7 +416,7 @@ namespace SecondBotEvents.Commands
{
return Failure("Group UUID invaild", [group]);
}
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupChatHistory(groupUUID)));
return BasicReply(JsonSerializer.Serialize(master.DataStoreService.GetGroupChatHistory(groupUUID), JsonOptions.UnsafeRelaxed));
}
[About("sends a message to the groupchat")]

View file

@ -82,7 +82,7 @@ namespace SecondBotEvents.Commands
{
return Failure("No objects found");
}
return BasicReply(JsonSerializer.Serialize(reply));
return BasicReply(JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed));
}
[About("Fetchs the current bot")]
@ -158,7 +158,7 @@ namespace SecondBotEvents.Commands
{ "y", (int)Math.Round(GetClient().Self.SimPosition.Y) },
{ "z", (int)Math.Round(GetClient().Self.SimPosition.Z) }
};
return BasicReply(JsonSerializer.Serialize(pos));
return BasicReply(JsonSerializer.Serialize(pos, JsonOptions.UnsafeRelaxed));
}
}

View file

@ -1112,7 +1112,7 @@ namespace SecondBotEvents.Commands
public object InventoryContentsCatched()
{
InventoryNodeEntry root = LoadInventoryNode(GetClient().Inventory.Store.RootFolder.UUID,"My Inventory");
return BasicReply(JsonSerializer.Serialize(root));
return BasicReply(JsonSerializer.Serialize(root, JsonOptions.UnsafeRelaxed));
}
protected InventoryNodeEntry LoadInventoryNode(UUID folder, string name="")
@ -1284,7 +1284,7 @@ namespace SecondBotEvents.Commands
}
public string ToJson()
{
return JsonSerializer.Serialize(this);
return JsonSerializer.Serialize(this, JsonOptions.UnsafeRelaxed);
}
}
}

View file

@ -33,20 +33,20 @@ namespace SecondBotEvents.Commands
};
if (UUID.TryParse(notecardInventoryUUID, out var uuid) == false)
{
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply), "NotecardRead");
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed), "NotecardRead");
return Failure(reply.content, [notecardInventoryUUID]);
}
reply.content = "Unable to find inventory item";
InventoryItem item = GetClient().Inventory.FetchItem(uuid, GetClient().Self.AgentID, new System.TimeSpan(0, 1, 30));
if (item == null)
{
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply), "NotecardRead");
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed), "NotecardRead");
return Failure(reply.content, [notecardInventoryUUID]);
}
reply.content = "Inventory item is not a notecard";
if (item.InventoryType != InventoryType.Notecard)
{
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply), "NotecardRead");
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed), "NotecardRead");
return Failure(reply.content, [notecardInventoryUUID]); ;
}
InventoryNotecard notecard = (InventoryNotecard)item;
@ -55,7 +55,7 @@ namespace SecondBotEvents.Commands
if (transfer.Success == false)
{
reply.content = "!ERROR! - unable to read notecard";
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply), "NotecardRead");
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed), "NotecardRead");
}
else
{
@ -77,7 +77,7 @@ namespace SecondBotEvents.Commands
reply.content = contents;
reply.name = notecard.Name;
reply.status = true;
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply), "NotecardRead");
master.CommandsService.SmartCommandReply(replytarget, JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed), "NotecardRead");
}
}
);

View file

@ -106,7 +106,7 @@ namespace SecondBotEvents.Commands
{
reply.Add(A.Name, A.SnapshotID.ToString());
}
return BasicReply(JsonSerializer.Serialize(reply));
return BasicReply(JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed));
}
[About("Requests the current parcel size")]
@ -245,7 +245,7 @@ namespace SecondBotEvents.Commands
};
reply.parcels.Add(R);
}
return BasicReply(JsonSerializer.Serialize(reply));
return BasicReply(JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed));
}
[About("Cancels the current parcel the bot is in sale")]
@ -341,7 +341,7 @@ namespace SecondBotEvents.Commands
collection.Add("ForSale", targetparcel.Flags.HasFlag(ParcelFlags.ForSale).ToString());
collection.Add("Cost", targetparcel.SalePrice.ToString());
collection.Add("SaleTarget", targetparcel.AuthBuyerID.ToString());
return BasicReply(JsonSerializer.Serialize(collection));
return BasicReply(JsonSerializer.Serialize(collection, JsonOptions.UnsafeRelaxed));
}
[About("Make a request to the landsale directory")]
@ -407,7 +407,7 @@ namespace SecondBotEvents.Commands
int counter = (100 * GetpageNum);
foreach(DirectoryParcel parcel in e.DirParcels)
{
collection.Add(JsonSerializer.Serialize(parcel));
collection.Add(JsonSerializer.Serialize(parcel, JsonOptions.UnsafeRelaxed));
counter++;
}
landresultsTimeout.Set();
@ -422,7 +422,7 @@ namespace SecondBotEvents.Commands
return Failure("Timed out while doing land search");
}
GetClient().Directory.DirLandReply -= LandSearchResultsReply;
string reply = JsonSerializer.Serialize(collection);
string reply = JsonSerializer.Serialize(collection, JsonOptions.UnsafeRelaxed);
return BasicReply(reply, [filterRegion, filterPrice, filterArea, pageNum, timeout]);
}
@ -537,7 +537,7 @@ namespace SecondBotEvents.Commands
{
collection.Add(cfg.ToString(), targetparcel.Flags.HasFlag(cfg).ToString());
}
return BasicReply(JsonSerializer.Serialize(collection));
return BasicReply(JsonSerializer.Serialize(collection, JsonOptions.UnsafeRelaxed));
}
[About("Ejects an avatar")]
@ -785,7 +785,7 @@ namespace SecondBotEvents.Commands
collection.Add("OtherPrims", targetparcel.OtherPrims.ToString());
collection.Add("MaxPrims", targetparcel.MaxPrims.ToString());
collection.Add("ParcelPrimBonus", targetparcel.ParcelPrimBonus.ToString());
return BasicReply(JsonSerializer.Serialize(collection));
return BasicReply(JsonSerializer.Serialize(collection, JsonOptions.UnsafeRelaxed));
}
[About("Controls who can trigger audio on this parcel\n if everyone is set to true then grouponly will switch to false")]
[ReturnHints("ok")]
@ -1064,7 +1064,7 @@ namespace SecondBotEvents.Commands
reply.delay = delays * 1000;
reply.parcelName = targetparcel.Name;
reply.regionName = GetClient().Network.CurrentSim.Name;
return BasicReply(JsonSerializer.Serialize(reply));
return BasicReply(JsonSerializer.Serialize(reply, JsonOptions.UnsafeRelaxed));
}
[About("Returns a rezzed object")]
@ -1131,7 +1131,7 @@ namespace SecondBotEvents.Commands
{
return Failure("No objects found in the parcel");
}
return BasicReply(JsonSerializer.Serialize(objects));
return BasicReply(JsonSerializer.Serialize(objects, JsonOptions.UnsafeRelaxed));
}
[About("Updates the current parcels media settings \n" +

View file

@ -218,7 +218,7 @@ namespace SecondBotEvents.Commands
List<string> reply = [];
foreach (CommandHistory A in master.DataStoreService.GetCommandHistory())
{
reply.Add(JsonSerializer.Serialize(A));
reply.Add(JsonSerializer.Serialize(A, JsonOptions.UnsafeRelaxed));
if (reply.Count >= 5)
{
break;

View file

@ -1,11 +1,12 @@
using System.Text.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using SecondBotEvents.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime;
using System.Text;
using System.Text.Json;
namespace SecondBotEvents.Config
@ -76,7 +77,7 @@ namespace SecondBotEvents.Config
SimpleIO IO = new();
IO.ChangeRoot(fromFolder);
string writeFile = filename + ".json";
IO.WriteFile(writeFile, JsonSerializer.Serialize(saveMe));
IO.WriteFile(writeFile, JsonSerializer.Serialize(saveMe, JsonOptions.UnsafeRelaxed));
}
protected void LoadSettingsFromFile(string fromFolder)

View file

@ -431,7 +431,7 @@ namespace SecondBotEvents.Services
RedisKey writekey = new(myConfig.GetRedisPrefix() + store.Guid.ToString());
try
{
string savestring = JsonSerializer.Serialize(history);
string savestring = JsonSerializer.Serialize(history, JsonOptions.UnsafeRelaxed);
if (savestring == null)
{
LogFormater.Warn("Redis failed to convert history into savable format");

View file

@ -326,11 +326,7 @@ namespace SecondBotEvents.Services
reply = "Error";
if (processed != null)
{
var options = new JsonSerializerOptions
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
reply = JsonSerializer.Serialize(processed, options);
reply = JsonSerializer.Serialize(processed, JsonOptions.UnsafeRelaxed);
}
CommandNotice(C.command, source, String.Join("@@@", C.args), true, reply);
return new KeyValuePair<bool, string>(status, reply);
@ -448,17 +444,13 @@ namespace SecondBotEvents.Services
BotCommandNotice e = new(command, args, source, accepted, results);
EventHandler<BotCommandNotice> handler = BotclientCommandEventNotices;
handler?.Invoke(this, e);
if (myConfig.GetCommandHistoryLogResults() == false)
{
return;
}
if (master.BotClient.basicCfg.GetLogCommands() == true)
{
if(e.results.Length >= 100)
if (myConfig.GetCommandHistoryLogResults() == false)
{
e.results = e.results.Substring(0, 100) + "...";
e.results = "~hidden~";
}
LogFormater.Info("Command log:" + JsonSerializer.Serialize(e));
LogFormater.Info("Command log:" + JsonSerializer.Serialize(e, JsonOptions.UnsafeRelaxed));
}
}
}
@ -618,4 +610,12 @@ namespace SecondBotEvents.Services
return (T)ret;
}
}
public static class JsonOptions
{
public static readonly JsonSerializerOptions UnsafeRelaxed = new()
{
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
}
}

View file

@ -190,7 +190,7 @@ namespace SecondBotEvents.Services
objectname = e.ObjectName,
expires = expiresAt
};
string eventMessage = JsonSerializer.Serialize(window);
string eventMessage = JsonSerializer.Serialize(window, JsonOptions.UnsafeRelaxed);
foreach(int a in DialogRelayChannels)
{
if(a >= 0)

View file

@ -37,7 +37,7 @@ namespace SecondBotEvents.Services
{
foreach (string A in targets)
{
string output = JsonSerializer.Serialize(detail);
string output = JsonSerializer.Serialize(detail, JsonOptions.UnsafeRelaxed);
if (UUID.TryParse(A, out UUID OutputAvatar) == true)
{
if (OutputAvatar != UUID.Zero)
@ -264,7 +264,7 @@ namespace SecondBotEvents.Services
args,
myConfig.GetOutputSecret()
);
string output = JsonSerializer.Serialize(e);
string output = JsonSerializer.Serialize(e, JsonOptions.UnsafeRelaxed);
if(myConfig.GetOutputChannel() >= 0)
{
GetClient().Self.Chat(output, myConfig.GetOutputChannel(), ChatType.Normal);

View file

@ -159,7 +159,7 @@ namespace SecondBotEvents.Services
{
return "Command request rejected";
}
return JsonSerializer.Serialize(master.CommandsService.RunCommand("HTTP", C));
return JsonSerializer.Serialize(master.CommandsService.RunCommand("HTTP", C), JsonOptions.UnsafeRelaxed);
}
public static async Task SerializationCallback(IHttpContext context, object? data)
{
@ -384,7 +384,7 @@ namespace SecondBotEvents.Services
{
return "Command request rejected";
}
return JsonSerializer.Serialize(master.CommandsService.RunCommand("HTTP",C));
return JsonSerializer.Serialize(master.CommandsService.RunCommand("HTTP",C), JsonOptions.UnsafeRelaxed);
}
}

View file

@ -145,7 +145,7 @@ namespace SecondBotEvents.Services
{
if(myConfig.GetEnableDebug() == true)
{
LogFormater.Warn("Interaction debug:" + JsonSerializer.Serialize(new InteractionEvent(from, eventype, status, why, setMisc)));
LogFormater.Warn("Interaction debug:" + JsonSerializer.Serialize(new InteractionEvent(from, eventype, status, why, setMisc), JsonOptions.UnsafeRelaxed));
}
if (myConfig.GetEnableJsonOutputEvents() == false)
{
@ -153,7 +153,7 @@ namespace SecondBotEvents.Services
}
master.CommandsService.SmartCommandReply(
myConfig.GetJsonOutputEventsTarget(),
JsonSerializer.Serialize(new InteractionEvent(from, eventype, status, why, setMisc)),
JsonSerializer.Serialize(new InteractionEvent(from, eventype, status, why, setMisc), JsonOptions.UnsafeRelaxed),
"interactions"
);
}
@ -171,7 +171,7 @@ namespace SecondBotEvents.Services
{
if (myConfig.GetEnableDebug() == true)
{
LogFormater.Warn("Interaction debug:" + JsonSerializer.Serialize(misc));
LogFormater.Warn("Interaction debug:" + JsonSerializer.Serialize(misc, JsonOptions.UnsafeRelaxed));
}
if (myConfig.GetEnableJsonOutputEvents() == false)
{
@ -180,7 +180,7 @@ namespace SecondBotEvents.Services
misc.Add("eventsource", source);
master.CommandsService.SmartCommandReply(
myConfig.GetJsonOutputEventsTarget(),
JsonSerializer.Serialize(misc),
JsonSerializer.Serialize(misc, JsonOptions.UnsafeRelaxed),
"interactions"
);
}

View file

@ -218,7 +218,7 @@ namespace SecondBotEvents.Services
sourcetype = source,
sourceoption = filter
};
string eventMessage = JsonSerializer.Serialize(a);
string eventMessage = JsonSerializer.Serialize(a, JsonOptions.UnsafeRelaxed);
relayEventExtended relayEventExtended = new()
{
source = source,
@ -230,7 +230,7 @@ namespace SecondBotEvents.Services
message = message,
unixtime = SecondbotHelpers.UnixTimeNow().ToString()
};
string eventMessageExtended = JsonSerializer.Serialize(relayEventExtended);
string eventMessageExtended = JsonSerializer.Serialize(relayEventExtended, JsonOptions.UnsafeRelaxed);
while (loop <= myConfig.GetRelayCount())
{

View file

@ -1,9 +1,10 @@
using System.Text.Json;
using OpenMetaverse;
using OpenMetaverse;
using OpenMetaverse.ImportExport.Collada14;
using SecondBotEvents.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace BetterSecondBot.Static
{
@ -146,7 +147,7 @@ namespace BetterSecondBot.Static
}
public static string MapFolderInventoryJson(GridClient bot,UUID folder)
{
return JsonSerializer.Serialize(getfolderinventory(bot, folder));
return JsonSerializer.Serialize(getfolderinventory(bot, folder), JsonOptions.UnsafeRelaxed);
}
public static KeyValuePair<string,string> MapFolderInventoryHumanReadable(GridClient bot, UUID folder)
@ -192,7 +193,7 @@ namespace BetterSecondBot.Static
}
if (topLevel != null)
{
return JsonSerializer.Serialize(DoMapFolderJson(bot, topLevel, subfolders));
return JsonSerializer.Serialize(DoMapFolderJson(bot, topLevel, subfolders), JsonOptions.UnsafeRelaxed);
}
}
}