Skip to content

Commit

Permalink
Abort on rtf keywords with negative skip count params
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed Feb 23, 2024
1 parent c162df5 commit 9ffe506
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 4 deletions.
6 changes: 5 additions & 1 deletion AL_Common/RTF/RTFParserCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,11 @@ public enum RtfError
/// <summary>
/// A symbol table entry was malformed. Possibly one of its enum values was out of range.
/// </summary>
InvalidSymbolTableEntry
InvalidSymbolTableEntry,
/// <summary>
/// The rtf is malformed in such a way that it might be unsafe to continue parsing it (infinite loops, stack overflows, etc.)
/// </summary>
AbortedForSafety
}

#endregion
Expand Down
1 change: 1 addition & 0 deletions AL_Common/RTF/RtfDisplayedReadmeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ private RtfError DispatchSpecialKeyword(SpecialType specialType, Symbol symbol,
{
case SpecialType.SkipNumberOfBytes:
if (symbol.UseDefaultParam) param = symbol.DefaultParam;
if (param < 0) return RtfError.AbortedForSafety;
CurrentPos += param;
break;
case SpecialType.SkipDest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private RtfError HandleSkippableHexData()
{
// Prevent stack overflow from maliciously-crafted rtf files - we should never recurse back into here in
// a spec-conforming file.
if (_inHandleSkippableHexData) return RtfError.StackOverflow;
if (_inHandleSkippableHexData) return RtfError.AbortedForSafety;
_inHandleSkippableHexData = true;

int startGroupLevel = _ctx.GroupStack.Count;
Expand Down
3 changes: 2 additions & 1 deletion AL_Common/RTF/RtfToTextConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ private RtfError DispatchSpecialKeyword(SpecialType specialType, Symbol symbol,
{
case SpecialType.SkipNumberOfBytes:
if (symbol.UseDefaultParam) param = symbol.DefaultParam;
if (param < 0) return RtfError.AbortedForSafety;
CurrentPos += param;
break;
case SpecialType.HexEncodedChar:
Expand Down Expand Up @@ -1110,7 +1111,7 @@ private unsafe RtfError HandleFontTable()
{
// Prevent stack overflow from maliciously-crafted rtf files - we should never recurse back into here in
// a spec-conforming file.
if (_inHandleFontTable) return RtfError.StackOverflow;
if (_inHandleFontTable) return RtfError.AbortedForSafety;
_inHandleFontTable = true;

int fontTableGroupLevel = _ctx.GroupStack.Count;
Expand Down
2 changes: 1 addition & 1 deletion AL_Common/RTF/RtfToTextConverter_DupeSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private RtfError HandleSkippableHexData()
{
// Prevent stack overflow from maliciously-crafted rtf files - we should never recurse back into here in
// a spec-conforming file.
if (_inHandleSkippableHexData) return RtfError.StackOverflow;
if (_inHandleSkippableHexData) return RtfError.AbortedForSafety;
_inHandleSkippableHexData = true;

int startGroupLevel = _ctx.GroupStack.Count;
Expand Down

0 comments on commit 9ffe506

Please sign in to comment.