Skip to content

Commit

Permalink
Fix invalid cast with end script tag containing code (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb authored Feb 7, 2019
1 parent 26e8d86 commit 56fa17b
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1357,8 +1357,6 @@ private void ParseJavascriptAndEndScriptTag(in SyntaxListBuilder<RazorSyntaxNode
var tagStart = CurrentStart;
builder.Add(OutputAsMarkupLiteral());

SpanContext.EditHandler.AcceptedCharacters = endTagAcceptedCharacters;

var openAngleToken = EatCurrentToken(); // '<'
var forwardSlashToken = EatCurrentToken(); // '/'
var tagNameToken = EatCurrentToken(); // 'script'
Expand All @@ -1369,8 +1367,10 @@ private void ParseJavascriptAndEndScriptTag(in SyntaxListBuilder<RazorSyntaxNode
{
var miscAttributeBuilder = pooledResult.Builder;

ParseMarkupNodes(miscAttributeBuilder, ParseMode.Text, token => token.Kind == SyntaxKind.CloseAngle);
// We want to accept malformed end tags as content.
AcceptUntil(SyntaxKind.CloseAngle, SyntaxKind.OpenAngle);
miscAttributeBuilder.Add(OutputAsMarkupLiteral());

if (miscAttributeBuilder.Count > 0)
{
miscContent = SyntaxFactory.MarkupMiscAttributeContent(miscAttributeBuilder.ToList());
Expand All @@ -1390,6 +1390,8 @@ private void ParseJavascriptAndEndScriptTag(in SyntaxListBuilder<RazorSyntaxNode
}
}

SpanContext.EditHandler.AcceptedCharacters = endTagAcceptedCharacters;

endTag = SyntaxFactory.MarkupEndTag(
openAngleToken,
forwardSlashToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ public void Await_Runtime()
RunTimeTest();
}

[Fact]
public void Tags_Runtime()
{
RunTimeTest();
}

[Fact]
public void SimpleTagHelpers_Runtime()
{
Expand Down Expand Up @@ -707,6 +713,12 @@ public void Await_DesignTime()
DesignTimeTest();
}

[Fact]
public void Tags_DesignTime()
{
DesignTimeTest();
}

[Fact]
public void AddTagHelperDirective_DesignTime()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public void ScriptTag_Incomplete()
ParseDocumentTest("<script type=");
}

[Fact]
public void ScriptTag_Invalid()
{
ParseDocumentTest("@{ <script></script @ > }");
}

[Fact]
public void VoidElementFollowedByContent()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@{
<script type="text/javascript" @{} ></script @foo >
}
<script></script @
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles
{
#line hidden
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_DesignTime
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
public async System.Threading.Tasks.Task ExecuteAsync()
{
#line 1 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml"



#line default
#line hidden
#line 2 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml"


#line default
#line hidden
#line 2 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml"


#line default
#line hidden
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml(5,11): Error RZ1024: End of file or an unexpected character was reached before the "script" tag could be parsed. Elements inside markup blocks must be complete. They must either be self-closing ("<br />") or have matching end tags ("<p>Hello</p>"). If you intended to display a "<" character, use the "&lt;" HTML entity.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Document -
NamespaceDeclaration - - Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_DesignTime - -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
CSharpCode - (2:0,2 [6] Tags.cshtml)
IntermediateToken - (2:0,2 [6] Tags.cshtml) - CSharp - \n
HtmlContent - (8:1,4 [31] Tags.cshtml)
IntermediateToken - (8:1,4 [7] Tags.cshtml) - Html - <script
IntermediateToken - (15:1,11 [23] Tags.cshtml) - Html - type="text/javascript"
IntermediateToken - (38:1,34 [1] Tags.cshtml) - Html -
CSharpCode - (41:1,37 [0] Tags.cshtml)
IntermediateToken - (41:1,37 [0] Tags.cshtml) - CSharp -
HtmlContent - (42:1,38 [17] Tags.cshtml)
IntermediateToken - (42:1,38 [1] Tags.cshtml) - Html -
IntermediateToken - (43:1,39 [1] Tags.cshtml) - Html - >
IntermediateToken - (44:1,40 [15] Tags.cshtml) - Html - </script @foo >
CSharpCode - (59:1,55 [2] Tags.cshtml)
IntermediateToken - (59:1,55 [2] Tags.cshtml) - CSharp - \n
HtmlContent - (64:3,0 [20] Tags.cshtml)
IntermediateToken - (64:3,0 [2] Tags.cshtml) - Html - \n
IntermediateToken - (66:4,0 [7] Tags.cshtml) - Html - <script
IntermediateToken - (73:4,7 [1] Tags.cshtml) - Html - >
IntermediateToken - (74:4,8 [10] Tags.cshtml) - Html - </script @
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Source Location: (2:0,2 [6] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml)
|
|
Generated Location: (709:18,2 [6] )
|
|

Source Location: (41:1,37 [0] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml)
||
Generated Location: (865:24,37 [0] )
||

Source Location: (59:1,55 [2] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml)
|
|
Generated Location: (1033:29,55 [2] )
|
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "42fb758ed87c3709cfb3f3aeff7a9cf8d242b74c"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime), @"default", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml")]
namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles
{
#line hidden
[global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"42fb758ed87c3709cfb3f3aeff7a9cf8d242b74c", @"/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml")]
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime
{
#pragma warning disable 1998
public async System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral(" <script type=\"text/javascript\" ");
WriteLiteral(" ></script @foo >\r\n");
WriteLiteral("\r\n<script></script @");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Tags.cshtml(5,11): Error RZ1024: End of file or an unexpected character was reached before the "script" tag could be parsed. Elements inside markup blocks must be complete. They must either be self-closing ("<br />") or have matching end tags ("<p>Hello</p>"). If you intended to display a "<" character, use the "&lt;" HTML entity.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Document -
RazorCompiledItemAttribute -
NamespaceDeclaration - - Microsoft.AspNetCore.Razor.Language.IntegrationTests.TestFiles
RazorSourceChecksumAttribute -
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Tags_Runtime - -
MethodDeclaration - - public async - System.Threading.Tasks.Task - ExecuteAsync
CSharpCode - (2:0,2 [2] Tags.cshtml)
IntermediateToken - (2:0,2 [2] Tags.cshtml) - CSharp - \n
HtmlContent - (4:1,0 [35] Tags.cshtml)
IntermediateToken - (4:1,0 [4] Tags.cshtml) - Html -
IntermediateToken - (8:1,4 [7] Tags.cshtml) - Html - <script
IntermediateToken - (15:1,11 [23] Tags.cshtml) - Html - type="text/javascript"
IntermediateToken - (38:1,34 [1] Tags.cshtml) - Html -
CSharpCode - (41:1,37 [0] Tags.cshtml)
IntermediateToken - (41:1,37 [0] Tags.cshtml) - CSharp -
HtmlContent - (42:1,38 [19] Tags.cshtml)
IntermediateToken - (42:1,38 [1] Tags.cshtml) - Html -
IntermediateToken - (43:1,39 [1] Tags.cshtml) - Html - >
IntermediateToken - (44:1,40 [15] Tags.cshtml) - Html - </script @foo >
IntermediateToken - (59:1,55 [2] Tags.cshtml) - Html - \n
CSharpCode - (61:2,0 [0] Tags.cshtml)
IntermediateToken - (61:2,0 [0] Tags.cshtml) - CSharp -
HtmlContent - (64:3,0 [20] Tags.cshtml)
IntermediateToken - (64:3,0 [2] Tags.cshtml) - Html - \n
IntermediateToken - (66:4,0 [7] Tags.cshtml) - Html - <script
IntermediateToken - (73:4,7 [1] Tags.cshtml) - Html - >
IntermediateToken - (74:4,8 [10] Tags.cshtml) - Html - </script @
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Markup span at (0:0,0 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [25] )
Transition span at (0:0,0 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [25] )
MetaCode span at (1:0,1 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [25] )
Markup span at (2:0,2 [1] ) (Accepts:Any) - Parent: Markup block at (2:0,2 [22] )
Markup span at (3:0,3 [8] ) (Accepts:None) - Parent: Tag block at (3:0,3 [8] )
Markup span at (11:0,11 [12] ) (Accepts:None) - Parent: Tag block at (11:0,11 [12] )
Markup span at (23:0,23 [1] ) (Accepts:None) - Parent: Markup block at (2:0,2 [22] )
Code span at (24:0,24 [0] ) (Accepts:Any) - Parent: Statement block at (0:0,0 [25] )
MetaCode span at (24:0,24 [1] ) (Accepts:None) - Parent: Statement block at (0:0,0 [25] )
Markup span at (25:0,25 [0] ) (Accepts:Any) - Parent: Markup block at (0:0,0 [25] )
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
RazorDocument - [0..25)::25 - [@{ <script></script @ > }]
MarkupBlock - [0..25)::25
MarkupTextLiteral - [0..0)::0 - [] - Gen<Markup> - SpanEditHandler;Accepts:Any
Marker;[];
CSharpCodeBlock - [0..25)::25
CSharpStatement - [0..25)::25
CSharpTransition - [0..1)::1 - Gen<None> - SpanEditHandler;Accepts:None
Transition;[@];
CSharpStatementBody - [1..25)::24
RazorMetaCode - [1..2)::1 - Gen<None> - SpanEditHandler;Accepts:None
LeftBrace;[{];
CSharpCodeBlock - [2..24)::22
MarkupBlock - [2..24)::22
MarkupTextLiteral - [2..3)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
MarkupElement - [3..23)::20
MarkupStartTag - [3..11)::8 - [<script>] - Gen<Markup> - SpanEditHandler;Accepts:None
OpenAngle;[<];
Text;[script];
CloseAngle;[>];
MarkupEndTag - [11..23)::12 - [</script @ >] - Gen<Markup> - SpanEditHandler;Accepts:None
OpenAngle;[<];
ForwardSlash;[/];
Text;[script];
MarkupMiscAttributeContent - [19..22)::3
MarkupTextLiteral - [19..22)::3 - [ @ ] - Gen<Markup> - SpanEditHandler;Accepts:Any
Whitespace;[ ];
Transition;[@];
Whitespace;[ ];
CloseAngle;[>];
MarkupTextLiteral - [23..24)::1 - [ ] - Gen<Markup> - SpanEditHandler;Accepts:None
Whitespace;[ ];
CSharpStatementLiteral - [24..24)::0 - [] - Gen<Stmt> - SpanEditHandler;Accepts:Any
Marker;[];
RazorMetaCode - [24..25)::1 - Gen<None> - SpanEditHandler;Accepts:None
RightBrace;[}];
MarkupTextLiteral - [25..25)::0 - [] - Gen<Markup> - SpanEditHandler;Accepts:Any
Marker;[];

0 comments on commit 56fa17b

Please sign in to comment.