Skip to content

Fix out-of-bounds safety in BlobToFileNameResolver#29672

Open
Ronitsabhaya75 wants to merge 1 commit into
Azure:mainfrom
Ronitsabhaya75:Blob-File
Open

Fix out-of-bounds safety in BlobToFileNameResolver#29672
Ronitsabhaya75 wants to merge 1 commit into
Azure:mainfrom
Ronitsabhaya75:Blob-File

Conversation

@Ronitsabhaya75
Copy link
Copy Markdown

@Ronitsabhaya75 Ronitsabhaya75 commented May 22, 2026

Description

This pull request resolves two safety-critical TODO items in the filename conflict resolution logic of the Azure Storage module (BlobToFileNameResolver.cs):

  1. MaxFileNameLength Guard Clause: Added validation to check if maxFileNameLength is non-positive (<= 0). If so, the code now throws a clear ArgumentOutOfRangeException instead of proceeding with invalid dimensions.
  2. Bounds Protection during Truncation: Implemented defensive boundaries when calculating trimLength. If the required trimming size exceeds the actual length of the filename base (which previously caused out-of-bounds Remove() index exceptions), the start index is now safely clamped to 0 to clear the base filename and return the postfix and extension safely.

No changes were made to cmdlet interfaces or public API signatures.

Mandatory Checklist

  • SHOULD update ChangeLog.md file(s) appropriately
    • Update src/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.
      • A snippet outlining the change(s) made in the PR should be written under the ## Upcoming Release header in the past tense.
    • Should not change ChangeLog.md if no new release is required, such as fixing test case only.
  • SHOULD regenerate markdown help files if there is cmdlet API change. Instruction
  • SHOULD have proper test coverage for changes in pull request.
  • SHOULD NOT adjust version of module manually in pull request

@Renish-patel thank you for testing and giving me help to write

Copilot AI review requested due to automatic review settings May 22, 2026 14:49
@azure-client-tools-bot-prd
Copy link
Copy Markdown

Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status.

@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

Thank you for your contribution @Ronitsabhaya75! We will review the pull request and get back to you soon.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Improves filename conflict resolution robustness by adding validation for maximum filename length and guarding against negative substring removal indices.

Changes:

  • Added validation to reject non-positive max filename length values.
  • Prevented string.Remove(...) from throwing when calculated trim length exceeds the base filename length.

int maxFileNameLength = this.getMaxFileNameLength();
if (maxFileNameLength <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxFileNameLength), "Max file name length must be greater than zero.");
Comment on lines 299 to 313
string postfixString = string.Format(" ({0})", count);

// TODO - trimLength could be be larger than pathAndFilename.Length, what do we do in this case?
int trimLength = (fileName.Length + postfixString.Length + extension.Length) - maxFileNameLength;

if (trimLength > 0)
{
fileName = fileName.Remove(fileName.Length - trimLength);
int startIndex = fileName.Length - trimLength;
if (startIndex < 0)
{
startIndex = 0;
}
fileName = fileName.Remove(startIndex);
}

return string.Format("{0}{1}{2}", fileName, postfixString, extension);
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment on lines +301 to +306
// If the postfix and extension alone exceed the limit, truncate the
// final result to maxFileNameLength so the contract is still honored.
if (postfixString.Length + extension.Length >= maxFileNameLength)
{
string result = string.Format("{0}{1}", postfixString, extension);
return result.Substring(0, Math.Min(result.Length, maxFileNameLength));
Comment on lines +313 to +318
int startIndex = fileName.Length - trimLength;
if (startIndex < 0)
{
startIndex = 0;
}
fileName = fileName.Remove(startIndex);
Co-authored-by: Renish Patel <renishpatel2482001@gmail.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment on lines +306 to +314
if (postfixString.Length + extension.Length >= maxFileNameLength)
{
throw new InvalidOperationException(
string.Format(
CultureInfo.InvariantCulture,
"Max file name length '{0}' is too small to generate a unique conflict-resolved file name for extension '{1}'.",
maxFileNameLength,
extension));
}
@azure-pipelines
Copy link
Copy Markdown
Contributor

Commenter does not have sufficient privileges for PR 29672 in repo Azure/azure-powershell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants