Skip to content

Commit 9ac0db5

Browse files
Xavraxmohitpubnubpubnub-release-bot
authored
Handle unencrypted message while getting messages with crypto (#198)
* 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]>
1 parent 895c7f5 commit 9ac0db5

File tree

11 files changed

+176
-22
lines changed

11 files changed

+176
-22
lines changed

.pubnub.yml

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
name: c-sharp
2-
version: "6.19.3"
2+
version: "6.19.4"
33
schema: 1
44
scm: github.com/pubnub/c-sharp
55
changelog:
6+
- date: 2023-11-28
7+
version: v6.19.4
8+
changes:
9+
- type: bug
10+
text: "Handle unencrypted message while getting messages with crypto."
611
- date: 2023-10-31
712
version: v6.19.3
813
changes:
@@ -756,7 +761,7 @@ features:
756761
- QUERY-PARAM
757762
supported-platforms:
758763
-
759-
version: Pubnub 'C#' 6.19.3
764+
version: Pubnub 'C#' 6.19.4
760765
platforms:
761766
- Windows 10 and up
762767
- Windows Server 2008 and up
@@ -766,7 +771,7 @@ supported-platforms:
766771
- .Net Framework 4.5
767772
- .Net Framework 4.6.1+
768773
-
769-
version: PubnubPCL 'C#' 6.19.3
774+
version: PubnubPCL 'C#' 6.19.4
770775
platforms:
771776
- Xamarin.Android
772777
- Xamarin.iOS
@@ -786,7 +791,7 @@ supported-platforms:
786791
- .Net Core
787792
- .Net 6.0
788793
-
789-
version: PubnubUWP 'C#' 6.19.3
794+
version: PubnubUWP 'C#' 6.19.4
790795
platforms:
791796
- Windows Phone 10
792797
- Universal Windows Apps
@@ -810,7 +815,7 @@ sdks:
810815
distribution-type: source
811816
distribution-repository: GitHub
812817
package-name: Pubnub
813-
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
818+
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
814819
requires:
815820
-
816821
name: ".Net"
@@ -1093,7 +1098,7 @@ sdks:
10931098
distribution-type: source
10941099
distribution-repository: GitHub
10951100
package-name: PubNubPCL
1096-
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
1101+
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
10971102
requires:
10981103
-
10991104
name: ".Net Core"
@@ -1452,7 +1457,7 @@ sdks:
14521457
distribution-type: source
14531458
distribution-repository: GitHub
14541459
package-name: PubnubUWP
1455-
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.3.0
1460+
location: https://github.com/pubnub/c-sharp/releases/tag/v6.19.4.0
14561461
requires:
14571462
-
14581463
name: "Universal Windows Platform Development"

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
v6.19.4 - November 28 2023
2+
-----------------------------
3+
- Fixed: handle unencrypted message while getting messages with crypto.
4+
15
v6.19.3 - October 31 2023
26
-----------------------------
37
- Fixed: fixes issue of applying default serializer settings.

src/Api/PubnubApi/EndPoint/Files/DownloadFileOperation.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ private void ProcessFileDownloadRequest(Dictionary<string, object> externalQuery
173173
}
174174
catch (Exception ex)
175175
{
176-
System.Diagnostics.Debug.WriteLine(ex.ToString());
176+
System.Diagnostics.Debug.WriteLine("{0}\nMessage might be not encrypted, returning as is...", ex.ToString());
177+
outputBytes = item1Bytes;
177178
}
178179
}
179180
PNDownloadFileResult result = new PNDownloadFileResult();
@@ -241,7 +242,9 @@ private async Task<PNResult<PNDownloadFileResult>> ProcessFileDownloadRequest(Di
241242
}
242243
catch (Exception ex)
243244
{
244-
System.Diagnostics.Debug.WriteLine(ex.ToString());
245+
System.Diagnostics.Debug.WriteLine("{0}\nMessage might be not encrypted, returning as is...", ex.ToString());
246+
outputBytes = item1Bytes;
247+
ret.Status = new PNStatus { Error = true, ErrorData = new PNErrorData("Decryption error", ex) };
245248
}
246249
}
247250

src/Api/PubnubApi/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
[assembly: AssemblyProduct("Pubnub C# SDK")]
1212
[assembly: AssemblyCopyright("Copyright © 2021")]
1313
[assembly: AssemblyTrademark("")]
14-
[assembly: AssemblyVersion("6.19.3.0")]
15-
[assembly: AssemblyFileVersion("6.19.3.0")]
14+
[assembly: AssemblyVersion("6.19.4.0")]
15+
[assembly: AssemblyFileVersion("6.19.4.0")]
1616
// Setting ComVisible to false makes the types in this assembly not visible
1717
// to COM components. If you need to access a type in this assembly from
1818
// COM, set the ComVisible attribute to true on that type.

src/Api/PubnubApi/PubnubApi.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
<PropertyGroup>
1616
<PackageId>Pubnub</PackageId>
17-
<PackageVersion>6.19.3.0</PackageVersion>
17+
<PackageVersion>6.19.4.0</PackageVersion>
1818
<Title>PubNub C# .NET - Web Data Push API</Title>
1919
<Authors>Pandu Masabathula</Authors>
2020
<Owners>PubNub</Owners>
2121
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2222
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
2323
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2424
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
25-
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
2626
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
2727
<!--<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>-->
2828
<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>

src/Api/PubnubApi/PubnubCoreBase.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,19 @@ private void ResponseToUserCallback<T>(List<object> result, PNOperationType type
724724
}
725725

726726
Announce(status);
727+
728+
LoggingMethod.WriteToLog(
729+
currentLog,
730+
string.Format(
731+
CultureInfo.InvariantCulture,
732+
"Failed to decrypt message on channel {0} in ResponseToUserCallback due to exception={1}.\nMessage might be not encrypted, returning as is...",
733+
currentMessageChannel,
734+
ex
735+
),
736+
currentConfig.LogVerbosity
737+
);
727738
}
728-
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
739+
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? payload.ToString() : decryptMessage);
729740

730741
payloadContainer.Add(decodeMessage);
731742
}

src/Api/PubnubApi/Security/SecureMessage.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public List<object> HistoryDecodeDecryptLoop<T>(PNOperationType type, List<objec
7171

7272
errorCallback.OnResponse(default(T), status);
7373
}
74-
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
74+
pubnubLog.WriteToLog("Failed to decrypt message!\nMessage might be not encrypted, returning as is...");
75+
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? element.ToString() : decryptMessage);
7576
receivedMsg.Add(decodeMessage);
7677
}
7778
returnMessage.Add(receivedMsg);
@@ -154,7 +155,8 @@ public List<object> FetchHistoryDecodeDecryptLoop<T>(PNOperationType type, Dicti
154155
errorCallback.OnResponse(default(T), status);
155156
#endregion
156157
}
157-
object decodeMessage = (decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : jsonLib.DeserializeToObject(decryptMessage);
158+
pubnubLog.WriteToLog("Failed to decrypt message!\nMessage might be not encrypted, returning as is...");
159+
object decodeMessage = jsonLib.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? decryptMessage : decryptMessage);
158160
dicDecrypt.Add(kvpValue.Key, decodeMessage);
159161
}
160162
else

src/Api/PubnubApiPCL/PubnubApiPCL.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
<PropertyGroup>
1717
<PackageId>PubnubPCL</PackageId>
18-
<PackageVersion>6.19.3.0</PackageVersion>
18+
<PackageVersion>6.19.4.0</PackageVersion>
1919
<Title>PubNub C# .NET - Web Data Push API</Title>
2020
<Authors>Pandu Masabathula</Authors>
2121
<Owners>PubNub</Owners>
2222
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2323
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
2424
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2525
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
26-
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
26+
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
2727
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
2828
<!--<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>-->
2929
<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>

src/Api/PubnubApiUWP/PubnubApiUWP.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
<PropertyGroup>
1818
<PackageId>PubnubUWP</PackageId>
19-
<PackageVersion>6.19.3.0</PackageVersion>
19+
<PackageVersion>6.19.4.0</PackageVersion>
2020
<Title>PubNub C# .NET - Web Data Push API</Title>
2121
<Authors>Pandu Masabathula</Authors>
2222
<Owners>PubNub</Owners>
2323
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
2424
<PackageIconUrl>http://pubnub.s3.amazonaws.com/2011/powered-by-pubnub/pubnub-icon-600x600.png</PackageIconUrl>
2525
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2626
<RepositoryUrl>https://github.com/pubnub/c-sharp/</RepositoryUrl>
27-
<PackageReleaseNotes>Fixes issue of applying default serializer settings.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Handle unencrypted message while getting messages with crypto.</PackageReleaseNotes>
2828
<PackageTags>Web Data Push Real-time Notifications ESB Message Broadcasting Distributed Computing</PackageTags>
2929
<!--<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>-->
3030
<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>

src/Api/PubnubApiUnity/PubnubApiUnity.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<PropertyGroup>
1717
<PackageId>PubnubApiUnity</PackageId>
18-
<PackageVersion>6.19.3.0</PackageVersion>
18+
<PackageVersion>6.19.4.0</PackageVersion>
1919
<Title>PubNub C# .NET - Web Data Push API</Title>
2020
<Authors>Pandu Masabathula</Authors>
2121
<Owners>PubNub</Owners>

src/UnitTests/PubnubApi.Tests/EncryptionTests.cs

+130-1
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,135 @@ public void TestPAMv3Signature()
793793
Assert.AreEqual("v2.k80LsDMD-sImA8rCBj-ntRKhZ8mSjHY8Ivngt9W3Yc4", signature);
794794
}
795795

796+
[Test]
797+
async public void TestSubscribeDecryption()
798+
{
799+
bool done = false;
800+
PNConfiguration config = CreateTestConfig();
801+
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });
802+
803+
Pubnub sut = new Pubnub(config);
804+
805+
sut.AddListener(new SubscribeCallbackExt(
806+
(pb, message) =>
807+
{
808+
Assert.AreEqual("test", message.Message);
809+
done = true;
810+
},
811+
(pb, presence) => {},
812+
(pb, status) => {}
813+
)
814+
);
815+
816+
sut.Subscribe<string>().Channels(new[] { "test" }).Execute();
817+
818+
Pubnub sender = new Pubnub(CreateTestConfig());
819+
820+
// Rust generated encrypted message
821+
await sender.Publish()
822+
.Channel("test")
823+
.Message("UE5FRAFBQ1JIEALf+E65kseYJwTw2J6BUk9MePHiCcBCS+8ykXLkBIOA")
824+
.ExecuteAsync();
825+
826+
// It will wait until the message is received or unit test timeout is reached
827+
while (!done)
828+
{
829+
await System.Threading.Tasks.Task.Delay(100);
830+
}
831+
}
832+
833+
[Test]
834+
async public void TestSubscribeDecryptionOnNonEncryptedMessage()
835+
{
836+
bool done = false;
837+
PNConfiguration config = CreateTestConfig();
838+
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });
839+
840+
Pubnub sut = new Pubnub(config);
841+
842+
sut.AddListener(new SubscribeCallbackExt(
843+
(pb, message) =>
844+
{
845+
Assert.AreEqual("test", message.Message);
846+
done = true;
847+
},
848+
(pb, presence) => {},
849+
(pb, status) => {}
850+
)
851+
);
852+
853+
sut.Subscribe<string>().Channels(new[] { "test" }).Execute();
854+
855+
Pubnub sender = new Pubnub(CreateTestConfig());
856+
857+
// Rust generated encrypted message
858+
await sender.Publish()
859+
.Channel("test")
860+
.Message("test")
861+
.ExecuteAsync();
862+
863+
// It will wait until the message is received or unit test timeout is reached
864+
while (!done)
865+
{
866+
await System.Threading.Tasks.Task.Delay(100);
867+
}
868+
}
869+
870+
[Test]
871+
async public void TestHistoryDecryption()
872+
{
873+
PNConfiguration config = CreateTestConfig();
874+
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });
875+
876+
Pubnub sut = new Pubnub(config);
877+
878+
Pubnub sender = new Pubnub(CreateTestConfig());
879+
880+
// Rust generated encrypted message
881+
await sender.Publish()
882+
.Channel("test")
883+
.Message("UE5FRAFBQ1JIEALf+E65kseYJwTw2J6BUk9MePHiCcBCS+8ykXLkBIOA")
884+
.ExecuteAsync();
885+
886+
PNResult<PNHistoryResult> result = await sut.History()
887+
.Channel("test")
888+
.Count(1)
889+
.ExecuteAsync();
890+
891+
Assert.AreEqual("test", result.Result.Messages[0].Entry);
892+
}
893+
894+
[Test]
895+
async public void TestHistoryDecryptionOnNonEncryptedMessage()
896+
{
897+
PNConfiguration config = CreateTestConfig();
898+
config.CryptoModule = new CryptoModule(new AesCbcCryptor("enigma"), new List<ICryptor> { new LegacyCryptor("enigma") });
899+
900+
Pubnub sut = new Pubnub(config);
901+
902+
Pubnub sender = new Pubnub(CreateTestConfig());
903+
904+
// Rust generated encrypted message
905+
await sender.Publish()
906+
.Channel("test")
907+
.Message("test")
908+
.ExecuteAsync();
909+
910+
PNResult<PNHistoryResult> result = await sut.History()
911+
.Channel("test")
912+
.Count(1)
913+
.ExecuteAsync();
796914

915+
Assert.AreEqual("test", result.Result.Messages[0].Entry);
916+
}
917+
918+
private PNConfiguration CreateTestConfig()
919+
{
920+
PNConfiguration config = new PNConfiguration(new UserId("test"));
921+
config.SubscribeKey = "demo";
922+
config.PublishKey = "demo";
923+
924+
return config;
925+
}
797926
}
798-
}
927+
}

0 commit comments

Comments
 (0)