Skip to content

Conversation

@yotsuda
Copy link
Contributor

@yotsuda yotsuda commented Nov 15, 2025

Summary

This PR fixes issue #25482 where Invoke-RestMethod and Invoke-WebRequest fail when uploading read-only files via the -Form parameter.

Changes

Code Fix

  • File: src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
  • Change: Added FileAccess.Read parameter to FileStream constructor in GetMultipartFileContent() method
  • Impact: Allows read-only files to be uploaded successfully

Test Coverage

  • File: test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
  • Added Tests:
    • Invoke-WebRequest -Form supports read-only file values
    • Invoke-RestMethod -Form supports read-only file values
  • Test Coverage: Both cmdlets are tested with read-only files, including proper cleanup

Root Cause

The GetMultipartFileContent() method was opening files with FileMode.Open without specifying FileAccess, which defaults to FileAccess.ReadWrite. This caused an "Access denied" error when attempting to upload read-only files.

Fix

Changed the FileStream initialization from:

new FileStream(file.FullName, FileMode.Open)

To:

new FileStream(file.FullName, FileMode.Open, FileAccess.Read)

Since the file is only being read for upload, FileAccess.Read is the correct access mode and allows read-only files to be processed.

Related Issue

Fixes #25482

Fixes PowerShell#25482

Changed FileStream to use FileAccess.Read when uploading files via -Form parameter, allowing read-only files to be uploaded successfully.
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 15, 2025
@yotsuda yotsuda marked this pull request as ready for review November 15, 2025 09:48
@@ -1,4 +1,4 @@
# Copyright (c) Microsoft Corporation.
# Copyright (c) Microsoft Corporation.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder what is changed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I apologize for the inconvenience. A UTF-8 BOM was accidentally added to the file during editing. I've fixed this in commit 8fe4fed.

@iSazonov iSazonov self-assigned this Nov 17, 2025
@iSazonov iSazonov merged commit d486365 into PowerShell:master Nov 18, 2025
44 of 46 checks passed
@yotsuda yotsuda deleted the fix/invoke-restmethod-readonly-file branch November 18, 2025 13:09
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-RestMethod [...] -Form $formData where a formData field points to a read only file fails

2 participants