Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.Dapr" Version="9.9.0" />
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.NodeJS.Extensions" Version="9.9.0" />
<PackageVersion Include="Aspire.Hosting.Azure.Search" Version="13.4.6" />
<PackageVersion Include="MessagePack" Version="3.1.7" /> <!-- Transitive dependency of Aspire pinned to newer version due to vulnerability in 2.5.192 -->
<PackageVersion Include="MessagePack" Version="3.1.9" /> <!-- Transitive dependency of Aspire pinned to newer version due to vulnerability in 2.5.192 -->
<PackageVersion Include="AWSSDK.BedrockAgent" Version="4.0.100.8" />
<PackageVersion Include="AWSSDK.BedrockAgentRuntime" Version="4.0.100.7" />
<PackageVersion Include="AWSSDK.BedrockRuntime" Version="4.0.101.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public async Task<SessionsPythonCodeExecutionResult> ExecuteCodeAsync(
/// <summary>
/// Uploads a file to the `/mnt/data` directory of the current session.
/// </summary>
/// <param name="remoteFileName">The name of the remote file, relative to `/mnt/data`.</param>
/// <param name="remoteFileName">The name of the remote file, relative to `/mnt/data`. Must not be rooted or contain `.` or `..` path segments.</param>
/// <param name="localFilePath">The path to the file on the local machine.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The metadata of the uploaded file.</returns>
Expand All @@ -120,13 +120,14 @@ public async Task<SessionsPythonCodeExecutionResult> ExecuteCodeAsync(
/// <exception cref="InvalidOperationException">Thrown when file operations are disabled or the path is not allowed.</exception>
[KernelFunction, Description("Uploads a file to the `/mnt/data` directory of the current session.")]
public async Task<SessionsRemoteFileMetadata> UploadFileAsync(
[Description("The name of the remote file, relative to `/mnt/data`.")] string remoteFileName,
[Description("The name of the remote file, relative to `/mnt/data`. Must not be rooted or contain `.` or `..` path segments.")] string remoteFileName,
[Description("The path to the file on the local machine.")] string localFilePath,
CancellationToken cancellationToken = default)
{
Verify.NotNullOrWhiteSpace(remoteFileName, nameof(remoteFileName));
Verify.NotNullOrWhiteSpace(localFilePath, nameof(localFilePath));

ValidateRemoteFileName(remoteFileName);
var validatedLocalPath = this.ValidateLocalPathForUpload(localFilePath);

this._logger.LogInformation("Uploading file: {LocalFilePath} to {RemoteFileName}", validatedLocalPath, remoteFileName);
Expand Down Expand Up @@ -292,6 +293,21 @@ private async Task<HttpResponseMessage> SendAsync(HttpClient httpClient, HttpMet
return await httpClient.SendWithSuccessCheckAsync(request, cancellationToken).ConfigureAwait(false);
}

private static void ValidateRemoteFileName(string remoteFileName)
{
var hasDrivePrefix = remoteFileName.Length >= 2
&& remoteFileName[1] == ':'
&& char.IsLetter(remoteFileName[0]);

if (remoteFileName[0] is '/' or '\\'
|| hasDrivePrefix
|| remoteFileName.Split('/', '\\').Any(segment => segment is "." or ".."))
{
throw new InvalidOperationException(
"The remote file name must be relative and must not contain '.' or '..' path segments.");
}
}

/// <summary>
/// Validates that the local file path is within allowed upload directories.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,61 @@ public async Task ItShouldUploadFileAsync()
Assert.Equal(this._messageHandlerStub.FirstMultipartContent, requestPayload);
}

[Theory]
[InlineData(".")]
[InlineData("..")]
[InlineData("../test-file.txt")]
[InlineData("../../test-file.txt")]
[InlineData("./test-file.txt")]
[InlineData("nested/./test-file.txt")]
[InlineData("nested/../test-file.txt")]
[InlineData("nested/..")]
[InlineData("/mnt/data/../../test-file.txt")]
[InlineData(@"nested\.\test-file.txt")]
[InlineData(@"nested\..\test-file.txt")]
[InlineData(@"\test-file.txt")]
[InlineData(@"\\server\share\test-file.txt")]
[InlineData(@"C:\test-file.txt")]
[InlineData("C:test-file.txt")]
[InlineData("C:/test-file.txt")]
[InlineData("/test-file.txt")]
public async Task ItShouldRejectUnsafeUploadRemoteFileNamesAsync(string remoteFileName)
{
// Arrange
var plugin = new SessionsPythonPlugin(this._settingsWithFileOperationsEnabled, this._httpClientFactory);

// Act
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => plugin.UploadFileAsync(remoteFileName, FileTestDataFilePath));

// Assert
Assert.Contains("must not contain '.' or '..' path segments", exception.Message);
Assert.Null(this._messageHandlerStub.RequestUri);
}

[Theory]
[InlineData("nested/dir/test-file.txt")]
[InlineData(@"nested\dir\test-file.txt")]
[InlineData(".hidden/test-file.txt")]
[InlineData("nested/a..b/test-file.txt")]
public async Task ItShouldAllowSafeUploadRemoteFileNamesAsync(string remoteFileName)
{
// Arrange
var responseContent = await File.ReadAllTextAsync(UpdaloadFileTestDataFilePath);
this._messageHandlerStub.ResponseToReturn = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(responseContent),
};

var plugin = new SessionsPythonPlugin(this._settingsWithFileOperationsEnabled, this._httpClientFactory);

// Act
await plugin.UploadFileAsync(remoteFileName, FileTestDataFilePath);

// Assert
Assert.NotNull(this._messageHandlerStub.RequestUri);
}

[Fact]
public async Task ItShouldDownloadFileWithoutSavingInDiskAsync()
{
Expand Down
Loading