Skip to content

Commit

Permalink
'#' is not treated as a reserved character in RelativePath's text for…
Browse files Browse the repository at this point in the history
…mat (#2264)

* updated reserve char check in RelativePath
* updated logic and added tests
  • Loading branch information
bhnaphade authored Aug 12, 2023
1 parent cb99f46 commit c7381cf
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 14 deletions.
43 changes: 29 additions & 14 deletions Stack/Opc.Ua.Core/Types/Utils/RelativePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ private static QualifiedName ParseName(

int last = reader.Peek();

for (int next = last; next != -1; next = reader.Peek(), last=next)
for (int next = last; next != -1; next = reader.Peek(), last = next)
{
if (!Char.IsDigit((char)next))
{
Expand Down Expand Up @@ -783,22 +783,37 @@ private static QualifiedName ParseName(
}
}

// check for invalid character.
if (next == '!' || next == ':' || next == '<' || next == '>' || next == '/' || next == '.')
{
throw new ServiceResultException(
StatusCodes.BadSyntaxError,
Utils.Format("Unexpected character '{0}' in browse path.", next));

}

// check for escape character.
if (next == '&')
{
next = reader.Read();
if (next == -1)
{
throw new ServiceResultException(
StatusCodes.BadSyntaxError,
"Unexpected end after escape character '&'.");
}
next = reader.Read();
buffer.Append((char)next);
continue;

if (next == '!' || next == ':' || next == '<' || next == '>' || next == '/' || next == '.' || next == '#' || next == '&')
{
buffer.Append((char)next);
continue;
}
else
{
throw new ServiceResultException(
StatusCodes.BadSyntaxError,
Utils.Format("Invalid escape sequence '&{0}' in browse path.", next));
}
}

// check for invalid character.
if (next == '!' || next == ':' || next == '<' || next == '>' || next == '/' || next == '.' || next == '#' || next == '&')
{
throw new ServiceResultException(
StatusCodes.BadSyntaxError,
Utils.Format("Unexpected character '{0}' in browse path.", next));
}

// append character.
Expand All @@ -812,8 +827,8 @@ private static QualifiedName ParseName(
if (last != '>')
{
throw new ServiceResultException(
StatusCodes.BadSyntaxError,
Utils.Format("Missing file '>' for reference type name in browse path."));
StatusCodes.BadSyntaxError,
Utils.Format("Missing closing '>' for reference type name in browse path."));
}
}

Expand Down
62 changes: 62 additions & 0 deletions Tests/Opc.Ua.Core.Tests/Types/Utils/UtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,68 @@ public void ValidateXmlReaderSettings()
Assert.AreEqual(false, settings.CloseInput);
}
#endregion

#region RelativePath.Parse Escaping

/// <summary>
/// Parse a path containing non-escaped hash character.
/// </summary>
[Test]
public void RelativePathParseNonEscapedHash()
{
TypeTable typeTable = new TypeTable(new NamespaceTable());
string str = "/abc#def";
Assert.Throws<ServiceResultException>(() => RelativePath.Parse(str, typeTable).Format(typeTable));
}

/// <summary>
/// Parse a path containing correctly escaped hash character.
/// </summary>
[Test]
public void RelativePathParseEscapedHash()
{
TypeTable typeTable = new TypeTable(new NamespaceTable());
string str = "/abc&#def";
string expected = "/abc#def";
Assert.AreEqual(expected, RelativePath.Parse(str, typeTable).Format(typeTable));
}

/// <summary>
/// Parse a path containing correctly escaped hash character folowed by exclamation.
/// </summary>
[Test]
public void RelativePathParseEscapedHashFollowedByExclamation()
{
TypeTable typeTable = new TypeTable(new NamespaceTable());
string str = "/abc&#!def";
Assert.Throws<ServiceResultException>(() => RelativePath.Parse(str, typeTable).Format(typeTable));
}

/// <summary>
/// Parse a path containing correctly escaped hash character by exclamation within the reference type delimeters.
/// </summary>
[Test]
public void RelativePathParseEscapedHashFollowedByExclamationInReferenceType()
{
TypeTable typeTable = new TypeTable(new NamespaceTable());
string str = "<abc&#!def>";
Assert.Throws<ServiceResultException>(() => RelativePath.Parse(str, typeTable).Format(typeTable));
}

/// <summary>
/// Parse a path containing incorrectly escaped character sequence.
/// </summary>
[Test]
public void RelativePathParseInvalidEscapeSequence()
{
TypeTable typeTable = new TypeTable(new NamespaceTable());
string str = "/abc&$!def";
Assert.Throws<ServiceResultException>(() => RelativePath.Parse(str, typeTable).Format(typeTable));
}


#endregion

}

}

0 comments on commit c7381cf

Please sign in to comment.