Skip to content

Commit

Permalink
Merge pull request #217 from nHapiNET/FuzzBadInputFixes3
Browse files Browse the repository at this point in the history
Add changes to fix bad input from fuzzing, Fixes #210
  • Loading branch information
milkshakeuk committed Jun 1, 2021
2 parents 0b678ff + ea59932 commit 87ced83
Show file tree
Hide file tree
Showing 30 changed files with 172 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-status.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.405
dotnet-version: 3.1.409

- name: Restore nHapi
run: |
Expand Down Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.405
dotnet-version: 3.1.409

- name: Restore nHapi
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/receive-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.405
dotnet-version: 3.1.409
- name: Restore nHapi
run: |
dotnet restore nHapi.sln --configfile build\.nuget\NuGet.config
Expand Down Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.405
dotnet-version: 3.1.409

- name: Restore nHapi
run: |
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "3.1.301",
"version": "3.1.409",
"rollForward": "latestFeature"
},
"projects": []
Expand Down
24 changes: 22 additions & 2 deletions src/NHapi.Base/Parser/EncodingCharacters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
namespace NHapi.Base.Parser
{
using System;
using System.Linq;

using NHapi.Base.Model;

Expand All @@ -52,13 +53,22 @@ public class EncodingCharacters : object, ICloneable
/// Component Separator, Repetition Separator, Escape Character, and
/// Subcomponent Separator (in that order).
/// </param>
/// <exception cref="HL7Exception">If encoding characters are not unique.</exception>
public EncodingCharacters(char fieldSeparator, string encodingCharacters)
{
if (char.IsWhiteSpace(fieldSeparator) || fieldSeparator == char.MinValue)
{
throw new HL7Exception("Field Seperator must be a printable character.");
}

FieldSeparator = fieldSeparator;

encChars = new char[4];

if (encodingCharacters == null)
#if NET35
if (string.IsNullOrEmpty(encodingCharacters) || encodingCharacters.Trim().Length == 0)
#else
if (string.IsNullOrWhiteSpace(encodingCharacters))
#endif
{
encChars[0] = '^';

Expand All @@ -70,6 +80,16 @@ public EncodingCharacters(char fieldSeparator, string encodingCharacters)
}
else
{
if (encodingCharacters.Any(@char => char.IsWhiteSpace(@char) || @char == char.MinValue))
{
throw new HL7Exception("Encoding characters must be printable characters.");
}

if (!SupportClass.CharsAreUnique(encodingCharacters))
{
throw new HL7Exception("Encoding characters must be unique.");
}

SupportClass.GetCharsFromString(encodingCharacters, 0, 4, encChars, 0);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/NHapi.Base/SupportClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace NHapi.Base
using System.Collections;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -2130,6 +2131,25 @@ public static object Pop(ArrayList stack)
}
}

/// <summary>
/// Evaluates if a string is comprised of unique characters.
/// </summary>
/// <param name="input">string to evaluate.</param>
/// <returns>True if all characters are unique, otherwise False.</returns>
/// <exception cref="ArgumentException">If <paramref name="input"/> is null or empty.</exception>
public static bool CharsAreUnique(string input)
{
if(string.IsNullOrEmpty(input))
{
throw new ArgumentException("Argument cannot be null or empty", nameof(input));
}

var chars = input.ToCharArray();
var distinct = chars.Distinct().ToArray();

return chars.Length == distinct.Length;
}

/*******************************/

/// <summary>
Expand Down
53 changes: 53 additions & 0 deletions tests/NHapi.Base.NUnit/SupportClassTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace NHapi.Base.NUnit
{
using System;

using global::NUnit.Framework;

using NHapi.Base;

[TestFixture]
public class SupportClassTests
{
[TestCase("^~\\&")]
[TestCase("@#&^")]
[TestCase("1234")]
[TestCase("qwer")]
[TestCase("abcd")]
[TestCase("efgh")]
[TestCase("!\"£$")]
public void CharsAreUnique_InputCharsAreUnique_ReturnsTrue(string input)
{
// Arrange / Act
var actual = SupportClass.CharsAreUnique(input);

// Assert
Assert.IsTrue(actual);
}

[TestCase("^^^^")]
[TestCase("~~~~")]
[TestCase("^~\\\\")]
[TestCase("^\\&&")]
[TestCase("0000")]
[TestCase("@#$$")]
[TestCase("****")]
public void CharsAreUnique_InputCharsAreNotUnique_ReturnsFalse(string input)
{
// Arrange / Act
var actual = SupportClass.CharsAreUnique(input);

// Assert
Assert.IsFalse(actual);
}

[TestCase(null)]
[TestCase("")]
public void CharsAreUnique_InputIsNullOrEmpty_ThrowsArgumentException(string input)
{
// Arrange / Act / Assert
Assert.Throws<ArgumentException>(
() => SupportClass.CharsAreUnique(input));
}
}
}
57 changes: 57 additions & 0 deletions tests/NHapi.NUnit/Parser/EncodingCharactersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,63 @@ public void Constructor_ValidInput_ReturnsExpectedResult(char fieldSeperator, st
Assert.AreEqual(encodingCharacters[3], sut.SubcomponentSeparator);
}

[TestCase(null)]
[TestCase("")]
[TestCase("\r")]
[TestCase("\t")]
[TestCase("\n")]
[TestCase(" ")]
public void Constructor_EncodingCharactersAreNullEmptyOrWhiteSpace_SetsDefaultValues(string encodingCharacters)
{
// Arrange / Act
var sut = new EncodingCharacters('|', encodingCharacters);

// Assert
Assert.AreEqual('|', sut.FieldSeparator);
Assert.AreEqual('^', sut.ComponentSeparator);
Assert.AreEqual('~', sut.RepetitionSeparator);
Assert.AreEqual('\\', sut.EscapeCharacter);
Assert.AreEqual('&', sut.SubcomponentSeparator);
}

[TestCase("^~\\ ")]
[TestCase("]@/\t")]
[TestCase("\"£\n*")]
[TestCase("\"\r£*")]
[TestCase("\"\0£*")]
public void Constructor_EncodingCharactersContainWhiteSpaceCharactersOrNullCharacter_ThrowsHl7Exception(string encodingCharacters)
{
// Arrange / Act / Assert
Assert.Throws<HL7Exception>(
() => new EncodingCharacters('|', encodingCharacters));
}

[TestCase("^^^^")]
[TestCase("~~~~")]
[TestCase("^~\\\\")]
[TestCase("^\\&&")]
[TestCase("0000")]
[TestCase("@#$$")]
[TestCase("****")]
public void Constructor_EncodingCharactersAreNotUnique_ThrowsHl7Exception(string encodingCharacters)
{
// Arrange / Act / Assert
Assert.Throws<HL7Exception>(
() => new EncodingCharacters('|', encodingCharacters));
}

[TestCase(' ')]
[TestCase('\r')]
[TestCase('\n')]
[TestCase('\t')]
[TestCase('\0')]
public void Constructor_FieldSeperatorIsWhiteSpaceOrNullCharacter_ThrowsHl7Exception(char fieldSeperator)
{
// Arrange / Act / Assert
Assert.Throws<HL7Exception>(
() => new EncodingCharacters(fieldSeperator, "^~\\&"));
}

[TestCase('|', '^', '~', '\\', '&')]
[TestCase('?', ']', '@', '/', '$')]
[TestCase('>', '\\', '£', '^', '*')]
Expand Down
4 changes: 2 additions & 2 deletions tests/NHapi.NUnit/Parser/LegacyPipeParserBadInputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
[TestFixture]
public class LegacyPipeParserBadInputTests
{
[TestCaseSource(nameof(TestPaths), new object[] { "EncodingNotSupportedException", typeof(EncodingNotSupportedException) })]
[TestCaseSource(nameof(TestPaths), new object[] { "HL7Exception", typeof(HL7Exception) })]
[TestCaseSource(nameof(TestPaths), new object[] { nameof(EncodingNotSupportedException), typeof(EncodingNotSupportedException) })]
[TestCaseSource(nameof(TestPaths), new object[] { nameof(HL7Exception), typeof(HL7Exception) })]
public void TestBadInputsThrowException(string path, Type expectedExceptionType)
{
// Arrange
Expand Down
4 changes: 2 additions & 2 deletions tests/NHapi.NUnit/Parser/PipeParserBadInputTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
[TestFixture]
public class PipeParserBadInputTests
{
[TestCaseSource(nameof(TestPaths), new object[] { "EncodingNotSupportedException", typeof(EncodingNotSupportedException) })]
[TestCaseSource(nameof(TestPaths), new object[] { "HL7Exception", typeof(HL7Exception) })]
[TestCaseSource(nameof(TestPaths), new object[] { nameof(EncodingNotSupportedException), typeof(EncodingNotSupportedException) })]
[TestCaseSource(nameof(TestPaths), new object[] { nameof(HL7Exception), typeof(HL7Exception) })]
public void TestBadInputsThrowException(string path, Type expectedExceptionType)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000||H|H|||||||2.80HHHHH|0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.7.1OBX||PPN|||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.7.1TXA||||||||||||||||||||||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.8.1OBX||PPN|||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.8.1TXA||||||||||||||||||||||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|^000|||||||ADT^A03*|||2.7000|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.8.1IN2||||||||||||||||||||||||||||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.8.1OBX||RMC|||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0000|||||||00|||2.8.1OBX||PTA|||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0~00|||||||000|||2.7.1IN2||||||||||||||||||||||||||||0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MSH|0~00||0|||||000|||2.8.10000000000000000000000000000000000000000000000000OBX||RMC|||~
Expand Down

0 comments on commit 87ced83

Please sign in to comment.