Skip to content

Commit 042ba3e

Browse files
authored
[release/10.0.1xx-rc.2] [msbuild] Improve detection of resources that would end up outside the app bundle. (#23920)
Backport of #23919.
1 parent df0d21f commit 042ba3e

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

msbuild/Xamarin.MacDev.Tasks/Tasks/CollectBundleResources.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ public static bool TryCreateItemWithLogicalName<T> (T task, ITaskItem item, [Not
227227
return false;
228228
}
229229

230-
if (logicalName.StartsWith (".." + Path.DirectorySeparatorChar, StringComparison.Ordinal)) {
231-
task.Log.LogError (null, null, null, item.ItemSpec, 0, 0, 0, 0, MSBStrings.E0100, logicalName);
230+
// Always check for both windows and non-windows style path separators, because we can get both on either platform.
231+
if (logicalName.StartsWith ("..\\", StringComparison.Ordinal) || logicalName.StartsWith ("../", StringComparison.Ordinal)) {
232+
task.Log.LogWarning (null, null, null, item.ItemSpec, 0, 0, 0, 0, MSBStrings.E0100, logicalName);
232233
return false;
233234
}
234235

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
7+
using Microsoft.Build.Framework;
8+
using Microsoft.Build.Utilities;
9+
10+
using NUnit.Framework;
11+
12+
using Xamarin.Utils;
13+
14+
namespace Xamarin.MacDev.Tasks {
15+
[TestFixture]
16+
public class CollectBundleResourcesTaskTests : TestBase {
17+
[Test]
18+
public void LogicalNameOutsideAppBundle ()
19+
{
20+
var currentDirectory = Environment.CurrentDirectory;
21+
try {
22+
var tmpdir = Cache.CreateTemporaryDirectory ();
23+
var task = CreateTask<CollectBundleResources> ();
24+
var item = new TaskItem ("image.png");
25+
var projDir = Path.Combine (tmpdir, "B");
26+
Directory.CreateDirectory (projDir);
27+
Environment.CurrentDirectory = projDir;
28+
File.WriteAllText (Path.Combine (projDir, item.ItemSpec), "image!");
29+
item.SetMetadata ("LocalDefiningProjectFullPath", Path.Combine (tmpdir, "A", "SDK.csproj"));
30+
item.SetMetadata ("LocalMSBuildProjectFullPath", Path.Combine (projDir, "Project.csproj"));
31+
task.BundleResources = [item];
32+
var rv = task.Execute ();
33+
34+
Assert.That (Engine.Logger.ErrorEvents.Count, Is.EqualTo (0), "Errors");
35+
36+
Assert.That (Engine.Logger.WarningsEvents.Count, Is.EqualTo (1), "Warnings");
37+
Assert.That (Engine.Logger.WarningsEvents [0].Message, Is.EqualTo ("The path '../B/image.png' would result in a file outside of the app bundle and cannot be used."), "Warning Message");
38+
39+
Assert.That (rv, Is.True, "Execute");
40+
} finally {
41+
Environment.CurrentDirectory = currentDirectory;
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)