Skip to content

Add -PipeName Support to Web Cmdlets#26024

Open
mdaneri wants to merge 25 commits intoPowerShell:masterfrom
mdaneri:master
Open

Add -PipeName Support to Web Cmdlets#26024
mdaneri wants to merge 25 commits intoPowerShell:masterfrom
mdaneri:master

Conversation

@mdaneri
Copy link

@mdaneri mdaneri commented Sep 9, 2025

Add -PipeName Support to Web Cmdlets

PR Summary

Fix #24475

Add Named Pipe (-PipeName) transport support to Invoke-WebRequest and Invoke-RestMethod, enabling
HTTP request/response exchange over a local Windows named pipe (parity with existing -UnixSocket
support). Includes test infrastructure (PipeName test module) and Pester tests validating basic GET
via both cmdlets.

PR Context

Some hosting / sandbox / constrained Windows scenarios (service isolation, container-like dev flows,
locked-down loopback policies) benefit from intra-host communication without TCP. PowerShell already
supports Unix domain sockets cross‑platform; this extends similar functionality on Windows via named
pipes for web cmdlets. The implementation reuses the existing request pipeline with a custom stream
bound to the named pipe. Tests confirm successful 200 responses and content handling over the pipe.

Summary of Changes

  • Add -PipeName parameter to web cmdlets (Invoke-WebRequest, Invoke-RestMethod).
  • Implement named pipe connection handling paralleling existing Unix socket logic.
  • Introduce PipeName test helper module: test/tools/Modules/PipeName/ (PipeName.psm1, PipeName.psd1).
  • No behavioral change when -PipeName is not specified.

Design / Considerations

  • Mirrors -UnixSocket design for consistency.
  • Scope limited to simple request/response (no HTTP/2 changes required).
  • Reuses existing response parsing; no new public types.
  • Security: server controls pipe ACLs; client only connects by supplied name.
  • Negligible perf impact (code path only active when parameter bound).

Validation

  • Local build succeeded (Start-PSBuild).
  • Full existing tests unaffected (insertion only, no edits to other logic).
  • Verified parameter binding integration with existing parameter sets.

Tests Added

Describe "Web cmdlets PipeName tests" (Tags: CI):

  • Invoke-WebRequest -PipeName returns 200 and expected body.
  • Invoke-RestMethod -PipeName returns expected body.

Potential Follow-Ups

  • Negative tests (invalid pipe name, timeout, premature close).
  • Multiple sequential request test to validate re-connection behavior.
  • Documentation updates: parameter docs, examples. PR12345
  • Release notes entry.
  • Implement mutual exclusivity between PipeName and UnixSocket (ParameterSet or check and throw ?)

Release Notes (Suggested)

Add -PipeName support to Invoke-WebRequest and Invoke-RestMethod on Windows, enabling
HTTP-over-named-pipe scenarios similar to existing -UnixSocket support.

PR Checklist

Introduces a PipeName property to web cmdlets, enabling HTTP-like communication over named pipes. Updates WebRequestSession to handle PipeName connections, adds tests for PipeName functionality, and provides a test module for named pipe server operations.
Refactored PipeName test setup in WebCmdlets.Tests.ps1 to use local variables instead of script-scoped variables and simplified test tags. Added a README.md for the PipeName module with usage instructions.
@mdaneri mdaneri requested review from a team and jshigetomi as code owners September 9, 2025 14:58
@mdaneri
Copy link
Author

mdaneri commented Sep 9, 2025

@microsoft-github-policy-service agree

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Sep 10, 2025
mdaneri and others added 7 commits September 9, 2025 22:33
Added null checks for UnixSocket and PipeName before connecting. Updated socket and pipe connection methods to use cancellation tokens and properly calculate timeout values for named pipes.

Reinsert WebSession.PipeName = PipeName; that was removed by mistake
Replaces the ConnectToUnixSocketAsync method and rollback to the previous implementation because it's not part of the PR .
Introduces a PipeName property to web cmdlets, enabling HTTP-like communication over named pipes. Updates WebRequestSession to handle PipeName connections, adds tests for PipeName functionality, and provides a test module for named pipe server operations.
Refactored PipeName test setup in WebCmdlets.Tests.ps1 to use local variables instead of script-scoped variables and simplified test tags. Added a README.md for the PipeName module with usage instructions.
Added null checks for UnixSocket and PipeName before connecting. Updated socket and pipe connection methods to use cancellation tokens and properly calculate timeout values for named pipes.

Reinsert WebSession.PipeName = PipeName; that was removed by mistake
Replaces the ConnectToUnixSocketAsync method and rollback to the previous implementation because it's not part of the PR .
@mdaneri
Copy link
Author

mdaneri commented Sep 10, 2025

Hi @iSazonov,
I think it's ready for review,
I cleanup the code, ported the missing code that I forgot to port from my repo and removed anything that was not essential to the PR (unix socket cancellation token)

Thanks

@daxian-dbw daxian-dbw added WG-Cmdlets general cmdlet issues WG-NeedsReview Needs a review by the labeled Working Group labels Sep 10, 2025
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty]
public virtual string PipeName { get; set; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: We should think about removing this virtual.

Copy link
Author

Choose a reason for hiding this comment

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

I used virtual because all other properties are virtual. But I agree with you it's useless

Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be wrapped in an IFDEF since it is Windows only. You should not see this parameter on non-Windows platforms. Same wrapper should be applied to all code paths that touch the pipename value.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We have no standard pattern for this but I believe it is better for UX to get specific message like "The parameter is supported only on Windows. On Unix you can use UnixSocket parameter." instead of generic one "A parameter cannot be found ..."

Removed redundant null check and used null-forgiving operator on _pipeName in NamedPipeClientStream constructor to address potential nullability warnings.
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Sep 23, 2025
@SteveL-MSFT SteveL-MSFT removed the WG-NeedsReview Needs a review by the labeled Working Group label Oct 15, 2025
@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Oct 15, 2025
@SteveL-MSFT
Copy link
Member

@PowerShell/wg-powershell-cmdlets had already approved the original issue being resolved by this PR, no further review needed by WG unless it deviates from the proposal in the isuse

Copy link
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

This PR adds Named Pipe transport support to PowerShell's web cmdlets (Invoke-WebRequest and Invoke-RestMethod) on Windows, providing functionality similar to the existing -UnixSocket parameter. This enables HTTP communication over Windows named pipes for scenarios requiring local IPC without TCP networking.

Key Changes

  • Added -PipeName parameter to web cmdlets with corresponding connection handling logic
  • Created test infrastructure (PipeName module) to validate named pipe HTTP communication
  • Implemented ConnectToNamedPipeAsync method mirroring the Unix socket pattern

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
WebRequestPSCmdlet.Common.cs Adds -PipeName parameter definition and session preparation logic
WebRequestSession.cs Implements named pipe connection callback and session state management
WebCmdlets.Tests.ps1 Adds CI tests validating both web cmdlets with named pipe transport
PipeName.psm1 Provides test helper functions for managing named pipe HTTP server
PipeName.psd1 Module manifest for the PipeName test module
README.md Documentation for using the PipeName test module

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Oct 29, 2025
@microsoft-github-policy-service
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

mdaneri and others added 11 commits November 5, 2025 10:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Cmdlet/WebRequestSession.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…Cmdlet/WebRequestSession.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Reformatted class and function definitions to use consistent brace placement and spacing. Improved code readability by updating formatting in loops and function parameters, and added a suppression attribute to Start-PipeServer. No functional changes were made.
Removed unnecessary blank line and adjusted indentation for delegate assignment and method call arguments to improve code readability in WebRequestSession.cs.
Corrected delegate and method call formatting in WebRequestSession.cs for improved code readability and consistency.
Unified and improved connection handling for Unix Domain Sockets and Named Pipes in WebRequestSession. Added dedicated async methods for each connection type, clarified precedence, and updated parameter names for clarity.
This address the CodeFactor issue Complex Method(20)
Clarified code by adding comments to distinguish Unix Domain Socket and Named Pipe connection configuration in WebRequestSession. Also reformatted the certificate validation callback for improved readability.
Added blank lines after timeout calculation and socket connection steps to improve code readability in WebRequestSession.cs.

// Create the pipe client stream and connect
var stream = new System.IO.Pipes.NamedPipeClientStream(".", _pipeName!, System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.Asynchronous);
await stream.ConnectAsync(timeoutMs, cancellationToken).ConfigureAwait(false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need the await? Can we just return ValueTask without async in the method header?

@SteveL-MSFT SteveL-MSFT moved this to In-Progress-PullRequests in Cmdlets Working Group Feb 4, 2026
@SteveL-MSFT SteveL-MSFT added the WG-Reviewed A Working Group has reviewed this and made a recommendation label Feb 4, 2026
@SteveL-MSFT SteveL-MSFT moved this from In-Progress-PullRequests to Reviewed in Cmdlets Working Group Feb 4, 2026
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 Review - Needed The PR is being reviewed WG-Cmdlets general cmdlet issues WG-Reviewed A Working Group has reviewed this and made a recommendation

Projects

Status: Reviewed

Development

Successfully merging this pull request may close these issues.

Support Invoke-WebRequest over Windows named pipes

5 participants