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

REL-4242 - .NET SDK - Fix JSON serializer to handle accented characters #529

Merged
merged 2 commits into from
Sep 15, 2023
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
4 changes: 4 additions & 0 deletions sdk/dotNet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

# Change Log

## 16.6.3

* KSM-462 - Fixed JSON serializer that replaces characters with accents. Closes [Issue #523](https://github.com/Keeper-Security/secrets-manager/issues/523)

## 16.6.2

* KSM-456 - Added .NET 4.7 as an additional build target
Expand Down
26 changes: 25 additions & 1 deletion sdk/dotNet/SecretsManager.Test.Core/JsonUtils.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,34 @@ public class JsonUtilsTests
[Test]
public void ParseAndSerializeShouldNotChangeTheData()
{
const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[]}";
var rec = new KeeperRecordData {
type = "Login2",
title = "MyHomeLogin",
notes = "MyNotes",
fields = new KeeperRecordField[] {
new KeeperRecordField { type = "login", label = "Login", value = new string[] { "Login 1" }, required=true, privacyScreen=true },
new KeeperRecordField { type = "password", label = "Password", value = new string[] { "3[OJ%sc7n].wX6+k5GY)6" }, required=true, privacyScreen=true, enforceGeneration=true, complexity=new PasswordComplexity{ length=21, caps=5, lowercase=5, digits=5, special=5} },
new KeeperRecordField { type = "url", label = "URL", value = new string[] { "https://asdfjkasdfkdsa.com" }, required=true, privacyScreen=true },
new KeeperRecordField { type = "securityQuestion", label = "Security Question & Answer", value = new SecurityQuestion[] { new SecurityQuestion { question= "asdf", answer= "asdf" } }, required=true, privacyScreen=true },
new KeeperRecordField { type = "fileRef", label = "File or Photo", value = new object[] { } },
new KeeperRecordField { type = "oneTimeCode", label = "Two-Factor Code", value = new object[] { } },
},
custom = new KeeperRecordField[] { }
};
var jsonIn = CryptoUtils.BytesToString(JsonUtils.SerializeJson(rec));
//const string jsonIn = "{\"title\":\"MyHomeLogin\",\"type\":\"Login2\",\"fields\":[{\"type\":\"login\",\"label\":\"Login\",\"value\":[\"Login 1\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"password\",\"label\":\"Password\",\"value\":[\"3[OJ%sc7n].wX6+k5GY)6\"],\"required\":true,\"privacyScreen\":true,\"enforceGeneration\":true,\"complexity\":{\"length\":21,\"caps\":5,\"lowercase\":5,\"digits\":5,\"special\":5}},{\"type\":\"url\",\"label\":\"URL\",\"value\":[\"https://asdfjkasdfkdsa.com\"],\"required\":true,\"privacyScreen\":true},{\"type\":\"securityQuestion\",\"label\":\"Security Question & Answer\",\"value\":[{\"question\":\"asdf\",\"answer\":\"asdf\"}],\"required\":true,\"privacyScreen\":true},{\"type\":\"fileRef\",\"label\":\"File or Photo\",\"value\":[]},{\"type\":\"oneTimeCode\",\"label\":\"Two-Factor Code\",\"value\":[]}],\"custom\":[],\"notes\":\"MyNotes\"}";
var recordData = JsonUtils.ParseJson<KeeperRecordData>(CryptoUtils.StringToBytes(jsonIn));
var jsonOut = CryptoUtils.BytesToString(JsonUtils.SerializeJson(recordData));
Assert.AreEqual(jsonIn, jsonOut);
Assert.AreEqual(recordData.fields[1].value[0].ToString(), rec.fields[1].value[0]);
}
[Test]
public void ParseAndSerializeShouldPreserveDiacritics()
{
string recordTitle = "MySpéciàlHomèL°gin";
var krdin = new KeeperRecordData { title = recordTitle, type = "login" };
var krdout = JsonUtils.ParseJson<KeeperRecordData>(JsonUtils.SerializeJson(krdin));
Assert.AreEqual(krdin.title, krdout.title);
}
}
}
6 changes: 3 additions & 3 deletions sdk/dotNet/SecretsManager/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public static class JsonUtils
{
private static readonly JsonSerializerOptions Options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
//Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, // bad for strings with diacritics
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};

public static T ParseJson<T>(byte[] json)
{
return JsonSerializer.Deserialize<T>(json);
return JsonSerializer.Deserialize<T>(json, Options);
}

public static byte[] SerializeJson<T>(T obj)
Expand Down
6 changes: 3 additions & 3 deletions sdk/dotNet/SecretsManager/SecretsManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<LangVersion>9</LangVersion>
<Company>Keeper Security Inc.</Company>
<Product>SecretsManager .Net SDK</Product>
<AssemblyVersion>16.6.2</AssemblyVersion>
<FileVersion>16.6.2</FileVersion>
<PackageVersion>16.6.2</PackageVersion>
<AssemblyVersion>16.6.3</AssemblyVersion>
<FileVersion>16.6.3</FileVersion>
<PackageVersion>16.6.3</PackageVersion>
<NeutralLanguage>en-US</NeutralLanguage>
<PackageId>Keeper.SecretsManager</PackageId>
<Authors>Sergey Aldoukhov</Authors>
Expand Down
Loading