Title: ConstrainedLanguage mode should enforce language restrictions when invoking instance ScriptMethods · Issue #28434 · PowerShell/PowerShell · GitHub
Open Graph Title: ConstrainedLanguage mode should enforce language restrictions when invoking instance ScriptMethods · Issue #28434 · PowerShell/PowerShell
X Title: ConstrainedLanguage mode should enforce language restrictions when invoking instance ScriptMethods · Issue #28434 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/28434
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ConstrainedLanguage mode should enforce language restrictions when invoking instance ScriptMethods","articleBody":"### Prerequisites\n\n- [x] Write a descriptive title.\n- [x] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\n- [x] Search the existing issues.\n- [x] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\n- [x] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\n\n### Steps to reproduce\n\nOn macOS 26.7.1 arm64, using PowerShell 7.7.0 (Core):\n\n1. Start a PowerShell session in FullLanguage mode.\n2. Create a System.Text.StringBuilder instance.\n3. Attach an instance ScriptMethod named RunPayload using Add-Member.\n4. Set the session language mode to ConstrainedLanguage.\n5. Attempt a direct .NET method invocation.\n6. Invoke the previously attached instance ScriptMethod.\n\nThe following script reproduces the behavior:\n```powershell\n$stringBuilder = [System.Text.StringBuilder]::new()\n$stringBuilder | Add-Member -MemberType ScriptMethod -Name RunPayload -Value {\n [System.IO.File]::WriteAllText(\n '/tmp/powershell-language-mode-test.txt',\n 'Executed'\n )\n}\n$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'\n# This invocation is blocked.\ntry {\n [System.IO.File]::WriteAllText(\n '/tmp/powershell-direct-test.txt',\n 'Direct'\n )\n}\ncatch {\n Write-Host \"Direct invocation blocked: $($_.FullyQualifiedErrorId)\"\n}\n# This invocation succeeds.\ntry {\n $stringBuilder.RunPayload()\n Write-Host 'Instance ScriptMethod executed successfully.'\n}\ncatch {\n Write-Host \"Instance ScriptMethod blocked: $($_.FullyQualifiedErrorId)\"\n}\nTest-Path '/tmp/powershell-language-mode-test.txt'\n```\nThe script creates the instance ScriptMethod before changing the session language mode, allowing the behavior to be reproduced consistently.\n\n### Expected behavior\n\nConstrainedLanguage mode should enforce its method invocation restrictions consistently, including when invoking instance ScriptMethods attached to existing objects.\n\nIn particular, invoking an instance ScriptMethod from a ConstrainedLanguage session should not implicitly retain a more permissive execution context established when the method was created.\n\nThe runtime should enforce the caller’s language-mode restrictions when invoking the method, or otherwise ensure that instance ScriptMethods cannot be used to execute operations prohibited by the current language mode.\n\nThis would provide more consistent enforcement across different method invocation paths.\n\n### Actual behavior\n\nAn instance ScriptMethod created in FullLanguage mode can still execute successfully after the calling session transitions to ConstrainedLanguage mode.\n\nThe ScriptMethod retains the execution context in which it was created. Consequently, its body can execute .NET operations that would otherwise be prohibited in the current ConstrainedLanguage session.\n\nFor example, a direct invocation of [System.IO.File]::WriteAllText() is blocked, while the same operation succeeds when executed inside the previously attached instance ScriptMethod.\n\n### Error details\n\n```console\nMethodInvocationNotSupportedInConstrainedLanguage\n```\n\nThis error is produced by the direct .NET method invocation. The instance ScriptMethod invocation succeeds and does not produce an error.\n\n### Environment data\n\n```powershell\nPowerShell 7.7.0, macOS 26.7.1, arm64.\n```\n\n### Visuals\n\n_No response_","author":{"url":"https://github.com/dsuzener","@type":"Person","name":"dsuzener"},"datePublished":"2026-11-17T06:44:43Z","url":"https://github.com/PowerShell/PowerShell/issues/28434"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:59e6a827-b38a-4b2a-9321-d8cbbf4c0c26 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8C8E:3B7815:17E449:1FBF13:6AFD1099 |
| html-safe-nonce | 7753e6974a56073d3525bcf0b52d387609c53e777e0ebb93964b1a216ec76d76 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QzhFOjNCNzgxNToxN0U0NDk6MUZCRjEzOjZBRkQxMDk5IiwidmlzaXRvcl9pZCI6IjQ4NjMyNDIyNDE4NjQxNzU3NjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 45e4e9eca1f6d85e9458989b568267f14b6d75e5fed2976b20dcc67c38ed9e0e |
| hovercard-subject-tag | issue:6439072884 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/28434/issue_layout |
| twitter:image | https://opengraph.githubassets.com/301a5e4e23f29f05d004e79c29c3193b768412352f3daeb9a75f26df2241fcf0/PowerShell/PowerShell/issues/28434 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/301a5e4e23f29f05d004e79c29c3193b768412352f3daeb9a75f26df2241fcf0/PowerShell/PowerShell/issues/28434 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dsuzener |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7c34e25df4e46e972c8f0225e63410231bdeb9ea5634597c712796bbcdb2e627 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8fa571ebaa95e4e6bf872197c0e84b7dc48e6785 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width