Fix out-of-bounds safety in BlobToFileNameResolver#29672
Open
Ronitsabhaya75 wants to merge 1 commit into
Open
Conversation
| Thanks for your contribution! The pull request validation has started. Please revisit this comment for updated status. |
Contributor
|
Thank you for your contribution @Ronitsabhaya75! We will review the pull request and get back to you soon. |
Contributor
There was a problem hiding this comment.
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); |
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>
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)); | ||
| } |
Contributor
|
Commenter does not have sufficient privileges for PR 29672 in repo Azure/azure-powershell |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This pull request resolves two safety-critical TODO items in the filename conflict resolution logic of the Azure Storage module (
BlobToFileNameResolver.cs):maxFileNameLengthis non-positive (<= 0). If so, the code now throws a clearArgumentOutOfRangeExceptioninstead of proceeding with invalid dimensions.trimLength. If the required trimming size exceeds the actual length of the filename base (which previously caused out-of-boundsRemove()index exceptions), the start index is now safely clamped to0to clear the base filename and return the postfix and extension safely.No changes were made to cmdlet interfaces or public API signatures.
Mandatory Checklist
Please choose the target release of Azure PowerShell. (⚠️ Target release is a different concept from API readiness. Please click below links for details.)
Check this box to confirm: I have read the Submitting Changes section of
CONTRIBUTING.mdand reviewed the following information:ChangeLog.mdfile(s) appropriatelysrc/{{SERVICE}}/{{SERVICE}}/ChangeLog.md.## Upcoming Releaseheader in the past tense.ChangeLog.mdif no new release is required, such as fixing test case only.@Renish-patel thank you for testing and giving me help to write