Skip to content

Commit

Permalink
Handle unencrypted message while getting messages with crypto (#198)
Browse files Browse the repository at this point in the history
* fix file crypto

* This should return as expected

* needed log

* add ut for encryptions

* clean up. todo: use of TestHarness for avoiding prod call

* PubNub SDK v6.19.4.0 release.

---------

Co-authored-by: Mohit Tejani <[email protected]>
Co-authored-by: PubNub Release Bot <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2023
1 parent 895c7f5 commit 9ac0db5
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 22 deletions.
19 changes: 12 additions & 7 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
name: c-sharp
version: "6.19.3"
version: "6.19.4"
schema: 1
scm: github.com/pubnub/c-sharp
changelog:
- date: 2023-11-28
version: v6.19.4
changes:
- type: bug
text: "Handle unencrypted message while getting messages with crypto."
- date: 2023-10-31
version: v6.19.3
changes:
Expand Down Expand Up @@ -756,7 +761,7 @@ features:
- QUERY-PARAM
supported-platforms:
-
version: Pubnub 'C#' 6.19.3
version: Pubnub 'C#' 6.19.4
platforms:
- Windows 10 and up
- Windows Server 2008 and up
Expand All @@ -766,7 +771,7 @@ supported-platforms:
- .Net Framework 4.5
- .Net Framework 4.6.1+
-
version: PubnubPCL 'C#' 6.19.3
version: PubnubPCL 'C#' 6.19.4
platforms:
- Xamarin.Android
- Xamarin.iOS
Expand All @@ -786,7 +791,7 @@ supported-platforms:
- .Net Core
- .Net 6.0
-
version: PubnubUWP 'C#' 6.19.3
version: PubnubUWP 'C#' 6.19.4
platforms:
- Windows Phone 10
- Universal Windows Apps
Expand All @@ -810,7 +815,7 @@ sdks:
distribution-type: source
distribution-repository: GitHub
package-name: Pubnub
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
requires:
-
name: ".Net"
Expand Down Expand Up @@ -1093,7 +1098,7 @@ sdks:
distribution-type: source
distribution-repository: GitHub
package-name: PubNubPCL
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
requires:
-
name: ".Net Core"
Expand Down Expand Up @@ -1452,7 +1457,7 @@ sdks:
distribution-type: source
distribution-repository: GitHub
package-name: PubnubUWP
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
requires:
-
name: "Universal Windows Platform Development"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v6.19.4 - November 28 2023
-----------------------------
- Fixed: handle unencrypted message while getting messages with crypto.

v6.19.3 - October 31 2023
-----------------------------
- Fixed: fixes issue of applying default serializer settings.
Expand Down
7 changes: 5 additions & 2 deletions src/Api/PubnubApi/EndPoint/Files/DownloadFileOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ private void ProcessFileDownloadRequest(Dictionary<string, object> externalQuery
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
System.Diagnostics.Debug.WriteLine("{0}\nMessage might be not encrypted, returning as is...", ex.ToString());
outputBytes = item1Bytes;
}
}
PNDownloadFileResult result = new PNDownloadFileResult();
Expand Down Expand Up @@ -241,7 +242,9 @@ private async Task<PNResult<PNDownloadFileResult>> ProcessFileDownloadRequest(Di
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
System.Diagnostics.Debug.WriteLine("{0}\nMessage might be not encrypted, returning as is...", ex.ToString());
outputBytes = item1Bytes;
ret.Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Decryption error", ex) };
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
[assembly: AssemblyProduct("Pubnub C# SDK")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyVersion("6.19.3.0")]
[assembly: AssemblyFileVersion("6.19.3.0")]
[assembly: AssemblyVersion("6.19.4.0")]
[assembly: AssemblyFileVersion("6.19.4.0")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/PubnubApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

<PropertyGroup>
<PackageId>Pubnub</PackageId>
<PackageVersion>6.19.3.0</PackageVersion>
<PackageVersion>6.19.4.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Summary>-->
<Description>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Description>
Expand Down
13 changes: 12 additions & 1 deletion src/Api/PubnubApi/PubnubCoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,19 @@ private void ResponseToUserCallback<T>(List<object> result, PNOperationType type
}

Announce(status);

LoggingMethod.WriteToLog(
currentLog,
string.Format(
CultureInfo.InvariantCulture,
"Failed to decrypt message on channel {0} in ResponseToUserCallback due to exception={1}.\nMessage might be not encrypted, returning as is...",
currentMessageChannel,
ex
),
currentConfig.LogVerbosity
);
}
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? payload.ToString() : decryptMessage);

payloadContainer.Add(decodeMessage);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Api/PubnubApi/Security/SecureMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public List<object> HistoryDecodeDecryptLoop<T>(PNOperationType type, List<objec

errorCallback.OnResponse(default(T), status);
}
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
pubnubLog.WriteToLog("Failed to decrypt message!\nMessage might be not encrypted, returning as is...");
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? element.ToString() : decryptMessage);
receivedMsg.Add(decodeMessage);
}
returnMessage.Add(receivedMsg);
Expand Down Expand Up @@ -154,7 +155,8 @@ public List<object> FetchHistoryDecodeDecryptLoop<T>(PNOperationType type, Dicti
errorCallback.OnResponse(default(T), status);
#endregion
}
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
pubnubLog.WriteToLog("Failed to decrypt message!\nMessage might be not encrypted, returning as is...");
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : decryptMessage);
dicDecrypt.Add(kvpValue.Key, decodeMessage);
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApiPCL/PubnubApiPCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

<PropertyGroup>
<PackageId>PubnubPCL</PackageId>
<PackageVersion>6.19.3.0</PackageVersion>
<PackageVersion>6.19.4.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Summary>-->
<Description>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Description>
Expand Down
4 changes: 2 additions & 2 deletions src/Api/PubnubApiUWP/PubnubApiUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

<PropertyGroup>
<PackageId>PubnubUWP</PackageId>
<PackageVersion>6.19.3.0</PackageVersion>
<PackageVersion>6.19.4.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
<!--<Summary>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Summary>-->
<Description>PubNub is a Massively Scalable Web Push Service for Web and Mobile Games. This is a cloud-based service for broadcasting messages to thousands of web and mobile clients simultaneously</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/Api/PubnubApiUnity/PubnubApiUnity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<PropertyGroup>
<PackageId>PubnubApiUnity</PackageId>
<PackageVersion>6.19.3.0</PackageVersion>
<PackageVersion>6.19.4.0</PackageVersion>
<Title>PubNub C# .NET - Web Data Push API</Title>
<Authors>Pandu Masabathula</Authors>
<Owners>PubNub</Owners>
Expand Down
131 changes: 130 additions & 1 deletion src/UnitTests/PubnubApi.Tests/EncryptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,135 @@ public void TestPAMv3Signature()
Assert.AreEqual("v2.k80LsDMD-sImA8rCBj-ntRKhZ8mSjHY8Ivngt9W3Yc4", signature);
}

[Test]
async public void TestSubscribeDecryption()
{
bool done = false;
PNConfiguration config = CreateTestConfig();
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });

Pubnub sut = new Pubnub(config);

sut.AddListener(new SubscribeCallbackExt(
(pb, message) =>
{
Assert.AreEqual("test", message.Message);
done = true;
},
(pb, presence) => {},
(pb, status) => {}
)
);

sut.Subscribe<string>().Channels(new[] { "test" }).Execute();

Pubnub sender = new Pubnub(CreateTestConfig());

// Rust generated encrypted message
await sender.Publish()
.Channel("test")
.Message("UE5FRAFBQ1JIEALf+E65kseYJwTw2J6BUk9MePHiCcBCS+8ykXLkBIOA")
.ExecuteAsync();

// It will wait until the message is received or unit test timeout is reached
while (!done)
{
await System.Threading.Tasks.Task.Delay(100);
}
}

[Test]
async public void TestSubscribeDecryptionOnNonEncryptedMessage()
{
bool done = false;
PNConfiguration config = CreateTestConfig();
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });

Pubnub sut = new Pubnub(config);

sut.AddListener(new SubscribeCallbackExt(
(pb, message) =>
{
Assert.AreEqual("test", message.Message);
done = true;
},
(pb, presence) => {},
(pb, status) => {}
)
);

sut.Subscribe<string>().Channels(new[] { "test" }).Execute();

Pubnub sender = new Pubnub(CreateTestConfig());

// Rust generated encrypted message
await sender.Publish()
.Channel("test")
.Message("test")
.ExecuteAsync();

// It will wait until the message is received or unit test timeout is reached
while (!done)
{
await System.Threading.Tasks.Task.Delay(100);
}
}

[Test]
async public void TestHistoryDecryption()
{
PNConfiguration config = CreateTestConfig();
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });

Pubnub sut = new Pubnub(config);

Pubnub sender = new Pubnub(CreateTestConfig());

// Rust generated encrypted message
await sender.Publish()
.Channel("test")
.Message("UE5FRAFBQ1JIEALf+E65kseYJwTw2J6BUk9MePHiCcBCS+8ykXLkBIOA")
.ExecuteAsync();

PNResult<PNHistoryResult> result = await sut.History()
.Channel("test")
.Count(1)
.ExecuteAsync();

Assert.AreEqual("test", result.Result.Messages[0].Entry);
}

[Test]
async public void TestHistoryDecryptionOnNonEncryptedMessage()
{
PNConfiguration config = CreateTestConfig();
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });

Pubnub sut = new Pubnub(config);

Pubnub sender = new Pubnub(CreateTestConfig());

// Rust generated encrypted message
await sender.Publish()
.Channel("test")
.Message("test")
.ExecuteAsync();

PNResult<PNHistoryResult> result = await sut.History()
.Channel("test")
.Count(1)
.ExecuteAsync();

Assert.AreEqual("test", result.Result.Messages[0].Entry);
}

private PNConfiguration CreateTestConfig()
{
PNConfiguration config = new PNConfiguration(new UserId("test"));
config.SubscribeKey = "demo";
config.PublishKey = "demo";

return config;
}
}
}
}

0 comments on commit 9ac0db5

Please sign in to comment.