Skip to content

Commit 3d6f874

Browse files
authored
Return BadRequest for invalid parameter (#10064)
1 parent 52fc98b commit 3d6f874

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/WebJobs.Script.WebHost/Controllers/ExtensionsController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public async Task<IActionResult> Get()
6767
[Route("admin/host/extensions/{id}")]
6868
public async Task<IActionResult> Delete(string id)
6969
{
70+
// We are using a file name check, but the API expects a NuGet package name/ID
71+
if (!FileUtility.IsValidFileName(id))
72+
{
73+
return BadRequest();
74+
}
75+
7076
if (_extensionBundleManager.IsExtensionBundleConfigured())
7177
{
7278
return BadRequest(Resources.ExtensionBundleBadRequestDelete);
@@ -92,6 +98,11 @@ await _extensionsManager.DeleteExtensions(id)
9298
[Route("admin/host/extensions/jobs/{id}")]
9399
public async Task<IActionResult> GetJobs(string id)
94100
{
101+
if (!IsValidGuid(id))
102+
{
103+
return BadRequest();
104+
}
105+
95106
ExtensionsRestoreJob job = await GetJob(id);
96107

97108
if (job == null)
@@ -258,5 +269,10 @@ private async Task<IEnumerable<ExtensionsRestoreJob>> GetInProgressJobs()
258269

259270
return jobs;
260271
}
272+
273+
private bool IsValidGuid(string value)
274+
{
275+
return Guid.TryParse(value, out _);
276+
}
261277
}
262278
}

src/WebJobs.Script/Extensions/FileUtility.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.IO.Abstractions;
8+
using System.Linq;
89
using System.Reflection;
910
using System.Text;
1011
using System.Threading.Tasks;
@@ -13,6 +14,7 @@ namespace Microsoft.Azure.WebJobs.Script
1314
{
1415
public static class FileUtility
1516
{
17+
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
1618
private static IFileSystem _default = new FileSystem();
1719
private static IFileSystem _instance;
1820

@@ -282,5 +284,30 @@ private static void DoSafeAction(Action action, bool ignoreErrors)
282284
{
283285
}
284286
}
287+
288+
/// <summary>
289+
/// Checks if a file name is valid.
290+
/// </summary>
291+
/// <param name="input">The string to sanitize.</param>
292+
/// <returns>Boolean value determining if file name is valid or not.</returns>
293+
public static bool IsValidFileName(string fileName)
294+
{
295+
if (string.IsNullOrEmpty(fileName))
296+
{
297+
return false;
298+
}
299+
300+
if (fileName.Contains("..") || Path.IsPathRooted(fileName))
301+
{
302+
return false;
303+
}
304+
305+
if (fileName.Contains(Path.DirectorySeparatorChar) || fileName.Contains(Path.AltDirectorySeparatorChar))
306+
{
307+
return false;
308+
}
309+
310+
return fileName.IndexOfAny(InvalidFileNameChars) >= 0 ? false : true;
311+
}
285312
}
286313
}

test/WebJobs.Script.Tests/IO/FileUtilityTests.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
7-
using System.Text;
8-
using System.Threading.Tasks;
4+
using System.IO;
95
using Xunit;
106

117
namespace Microsoft.Azure.WebJobs.Script.Tests.IO
@@ -23,5 +19,22 @@ public void GetRelativePath_ReturnsExpectedPath(string path1, string path2, stri
2319

2420
Assert.Equal(expectedPath, result);
2521
}
22+
23+
[Theory]
24+
[InlineData("file/name", false)]
25+
[InlineData("file\0name", false)]
26+
[InlineData("file\\name", false)]
27+
[InlineData("..\\..\\filename", false)]
28+
[InlineData("../../filename", false)]
29+
[InlineData("..filename", false)]
30+
[InlineData("filename", true)]
31+
[InlineData("file-name", true)]
32+
[InlineData("file_name", true)]
33+
[InlineData("filename123", true)]
34+
public void IsValidFileName_ReturnsBool(string input, bool expectedOutput)
35+
{
36+
var result = FileUtility.IsValidFileName(input);
37+
Assert.Equal(expectedOutput, result);
38+
}
2639
}
2740
}

0 commit comments

Comments
 (0)