Skip to content

Commit d46028f

Browse files
authored
Merge pull request github#15542 from tamasvajk/feature/relative-line-pragma
C#: Try resolve relative paths in line mappings
2 parents 31cb308 + 1c7e6e7 commit d46028f

33 files changed

+120
-94
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.IO;
23
using Microsoft.CodeAnalysis;
4+
using Semmle.Util.Logging;
35

46
namespace Semmle.Extraction.CSharp.Entities
57
{
@@ -25,7 +27,8 @@ public override void Populate(TextWriter trapFile)
2527
var mapped = Symbol.GetMappedLineSpan();
2628
if (mapped.HasMappedPath && mapped.IsValid)
2729
{
28-
var mappedLoc = Create(Context, Location.Create(mapped.Path, default, mapped.Span));
30+
var path = TryAdjustRelativeMappedFilePath(mapped.Path, Position.Path, Context.Extractor.Logger);
31+
var mappedLoc = Create(Context, Location.Create(path, default, mapped.Span));
2932

3033
trapFile.locations_mapped(this, mappedLoc);
3134
}
@@ -61,5 +64,25 @@ private class SourceLocationFactory : CachedEntityFactory<Location, NonGenerated
6164

6265
public override NonGeneratedSourceLocation Create(Context cx, Location init) => new NonGeneratedSourceLocation(cx, init);
6366
}
67+
68+
public static string TryAdjustRelativeMappedFilePath(string mappedToPath, string mappedFromPath, ILogger logger)
69+
{
70+
if (!Path.IsPathRooted(mappedToPath))
71+
{
72+
try
73+
{
74+
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(mappedFromPath)!, mappedToPath));
75+
logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");
76+
77+
mappedToPath = fullPath;
78+
}
79+
catch (Exception e)
80+
{
81+
logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
82+
}
83+
}
84+
85+
return mappedToPath;
86+
}
6487
}
6588
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineOrSpanDirective.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ protected override void PopulatePreprocessor(TextWriter trapFile)
2525
{
2626
trapFile.directive_lines(this, kind);
2727

28-
if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
28+
var path = Symbol.File.ValueText;
29+
if (!string.IsNullOrWhiteSpace(path))
2930
{
30-
var file = File.Create(Context, Symbol.File.ValueText);
31+
path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(path, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
32+
var file = File.Create(Context, path);
3133
trapFile.directive_line_file(this, file);
3234
}
3335
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ private PragmaChecksumDirective(Context cx, PragmaChecksumDirectiveTriviaSyntax
1212

1313
protected override void PopulatePreprocessor(TextWriter trapFile)
1414
{
15-
var file = File.Create(Context, Symbol.File.ValueText);
15+
var path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(Symbol.File.ValueText, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
16+
var file = File.Create(Context, path);
1617
trapFile.pragma_checksums(this, file, Symbol.Guid.ToString(), Symbol.Bytes.ToString());
1718
}
1819

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Areas_TestArea_Views_Shared_Test18.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Areas_TestArea_Views_Shared_Test18 : global::Microsoft.AspNetCore.M
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "Areas/TestArea/Views/Shared/Test18.cshtml"
27+
#line 6 "../Areas/TestArea/Views/Shared/Test18.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class Areas_TestArea_Views_Shared_Test18 : global::Microsoft.AspNetCore.M
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "Areas/TestArea/Views/Shared/Test18.cshtml"
36+
#line 8 "../Areas/TestArea/Views/Shared/Test18.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "Areas/TestArea/Views/Shared/Test18.cshtml"
44+
#line 9 "../Areas/TestArea/Views/Shared/Test18.cshtml"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Areas_TestArea_Views_Test4_Test17.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Areas_TestArea_Views_Test4_Test17 : global::Microsoft.AspNetCore.Mv
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "Areas/TestArea/Views/Test4/Test17.cshtml"
27+
#line 6 "../Areas/TestArea/Views/Test4/Test17.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class Areas_TestArea_Views_Test4_Test17 : global::Microsoft.AspNetCore.Mv
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "Areas/TestArea/Views/Test4/Test17.cshtml"
36+
#line 8 "../Areas/TestArea/Views/Test4/Test17.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "Areas/TestArea/Views/Test4/Test17.cshtml"
44+
#line 9 "../Areas/TestArea/Views/Test4/Test17.cshtml"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/MyAreas_Test4_Test22.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MyAreas_Test4_Test22 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "MyAreas/Test4/Test22.cshtml"
27+
#line 6 "../MyAreas/Test4/Test22.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class MyAreas_Test4_Test22 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "MyAreas/Test4/Test22.cshtml"
36+
#line 8 "../MyAreas/Test4/Test22.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "MyAreas/Test4/Test22.cshtml"
44+
#line 9 "../MyAreas/Test4/Test22.cshtml"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Pages_Shared_Test21.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Pages_Shared_Test21 : global::Microsoft.AspNetCore.Mvc.Razor.RazorP
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "Pages/Shared/Test21.cshtml"
27+
#line 6 "../Pages/Shared/Test21.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class Pages_Shared_Test21 : global::Microsoft.AspNetCore.Mvc.Razor.RazorP
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "Pages/Shared/Test21.cshtml"
36+
#line 8 "../Pages/Shared/Test21.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "Pages/Shared/Test21.cshtml"
44+
#line 9 "../Pages/Shared/Test21.cshtml"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Template.g

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ using test;
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "$PATHSLASH"
27+
#line 6 "../$PATHSLASH"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ using test;
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "$PATHSLASH"
36+
#line 8 "../$PATHSLASH"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "$PATHSLASH"
44+
#line 9 "../$PATHSLASH"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Views_Custom2_Test16.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Views_Custom2_Test16 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "Views/Custom2/Test16.cshtml"
27+
#line 6 "../Views/Custom2/Test16.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class Views_Custom2_Test16 : global::Microsoft.AspNetCore.Mvc.Razor.Razor
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "Views/Custom2/Test16.cshtml"
36+
#line 8 "../Views/Custom2/Test16.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "Views/Custom2/Test16.cshtml"
44+
#line 9 "../Views/Custom2/Test16.cshtml"
4545
}
4646

4747
#line default

csharp/ql/test/query-tests/Security Features/CWE-079/XSSRazorPages/Generated/Views_Custom_Test3_Test15.cshtml.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Views_Custom_Test3_Test15 : global::Microsoft.AspNetCore.Mvc.Razor.
2424
#pragma warning disable 1998
2525
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2626
{
27-
#line 6 "Views/Custom/Test3/Test15.cshtml"
27+
#line 6 "../Views/Custom/Test3/Test15.cshtml"
2828
if (Model != null)
2929
{
3030

@@ -33,15 +33,15 @@ public class Views_Custom_Test3_Test15 : global::Microsoft.AspNetCore.Mvc.Razor.
3333
#nullable disable
3434
WriteLiteral(" <h3>Hello \"");
3535
#nullable restore
36-
#line 8 "Views/Custom/Test3/Test15.cshtml"
36+
#line 8 "../Views/Custom/Test3/Test15.cshtml"
3737
Write(Html.Raw(Model.Name));
3838

3939
#line default
4040
#line hidden
4141
#nullable disable
4242
WriteLiteral("\"</h3>\n");
4343
#nullable restore
44-
#line 9 "Views/Custom/Test3/Test15.cshtml"
44+
#line 9 "../Views/Custom/Test3/Test15.cshtml"
4545
}
4646

4747
#line default

0 commit comments

Comments
 (0)