A restricted mode compiles only policy files made of keywords and literals, checked before they run¶
Context and Problem Statement¶
A .bylaw.ps1 file is a PowerShell script, and Build-Bylaw runs it with the caller's permissions. Each file has its own runspace, but that isn't a security sandbox (ADR-0014), and the README warns that compiling a policy file runs it.
Security teams adopting policy as code are likely to object for two reasons:
- A pull request that changes a policy file runs code in the pipeline that builds it. That pipeline may also hold the credentials that deploy policy.
- Reviewers have to read PowerShell. A loop, helper function or dot-sourced file can hide what a policy does in a way that policy JSON can't.
Most policy files don't need anything but Bylaw's keywords. A survey of the repository's files on 2026-11-30 found:
- All 193 files that
ConvertTo-Bylawwrote for the Azure Landing Zones Library (ADR-0042) use only these: keyword calls; string, number and boolean literals; hashtables and arrays;[ordered]; keyword calls in parentheses, such as(Param EffectDisks); and variables assigned a literal hashtable, which are initiative members (252 assignments). - The 11 golden-test files and the 1 sandbox-verification file use the same constructs.
- 4 of the 24 example files use more on purpose: dot-sourcing,
foreach,if, string interpolation, method calls andImport-PowerShellDataFile. They areexamples/powershell/allowed-skus,public-network-accessandrequire-tag, andexamples/storage/storage-require-baseline.
How can a pipeline compile policy files without running arbitrary code from them?
Decision Drivers¶
- The pipeline decides whether a file may use full PowerShell, not the file or the pull request being built.
- A file is checked before any of it runs, so nothing in a rejected file executes.
- Converted files and plain policies compile in restricted mode unchanged.
- Authors who want PowerShell's loops, helpers and data files keep them.
- Errors point at the line that broke the rule, like Bylaw's other diagnostics.
Considered Options¶
- A check of the file's syntax tree against an allowlist, before it runs
- Constrained Language Mode in the compile runspace
- PowerShell's restricted language, as used by
datasections - Hiding every command except Bylaw's keywords in the runspace
- A data format instead of PowerShell
- Guidance only: review the compiled JSON, and build without credentials
Decision Outcome¶
Chosen option: "A check of the file's syntax tree against an allowlist, before it runs", together with the guidance. On 2026-11-30, PowerShell's own mechanisms were tried and failed (see the options below), and a data format would be a second language. The guidance is still worth giving, because restricted mode doesn't sandbox Bylaw itself.
What restricted mode allows¶
The file is parsed, and every node in its syntax tree must be one of the following:
- Commands: Bylaw's keywords, called by their bare name, such as
FieldorMust. Calls with&, dot-sourcing, module-qualified names, splatting and redirection are not allowed. - Statements: a pipeline of keyword commands, such as
Field minimumTlsVersion | Must -Be 'TLS1_2', or an assignment$name = <value>. The variable must be a plain name, not an automatic, preference, scoped orenv:variable, and the value must be allowed. - Values:
- numbers and single-quoted strings;
- double-quoted strings with no
$or subexpression; $true,$falseand$null;- variables assigned earlier in the file;
- hashtables with constant keys, and
[ordered]on a hashtable literal; - arrays, with
,or@(); - a keyword call in parentheses, such as
(Param EffectDisks)or(Current). - Script blocks: literals passed to a keyword. Their contents are checked by the same rules.
- Comments.
Everything else is an error, including:
- function, class, enum and
paramdefinitions, andusingstatements; if,foreach,while,switch,tryandtrap;- member access and method calls;
- casts and type literals other than
[ordered]; - operators;
- expandable strings that interpolate.
PowerShell runs code through commands, script blocks, property and method access, type conversion and string interpolation. The allowlist lets through only Bylaw's keywords and script blocks whose contents are checked too.
The check runs in the compile runspace. Bylaw runs the checked syntax tree, through Ast.GetScriptBlock(), instead of reading the file again, so the file can't change between the check and the run. A tried script block made this way kept the file name and line number in its error records.
Who chooses¶
Build-Bylaw -Restrictedturns it on."restricted": trueinbylaw.json(ADR-0012) also turns it on for a project.- Nothing in a file or in
bylaw.jsoncan turn it off when the caller asked for it. A pull request can only weaken it by changing the pipeline definition, which security teams already protect. ConvertTo-Bylaw's proof (ADR-0039) always compiles in restricted mode. The files it writes must pass, which also enforces what ADR-0014's exception for reused runspaces assumes.- During the preview it is opt-in, so the README's examples that use PowerShell keep working.
Errors¶
A new front-end error, the next free BYL1xxx code, is "not allowed in restricted mode". It names the construct and points at its line. All of them are reported before anything in the file runs. The file isn't run, and the build fails.
Guidance in the README¶
A "Bylaw in a pipeline" section recommends:
- Build in a stage with no Azure credentials, and deploy in a separate stage that only reads the build's output.
- Commit the compiled JSON next to the source, and review its diff. Bylaw's output is deterministic (ADR-0009), so CI can rebuild and fail when the committed JSON differs.
- Use
-Restrictedon pull requests, and in any stage that builds files the author of the pipeline doesn't control.
Consequences¶
- Good, because a security team can require that building a policy file runs nothing but Bylaw's keywords, and enforce it from the pipeline.
- Good, because a restricted file reads as a declaration, and every value it compiles is visible in it.
- Good, because the check is cheap. Parsing and walking the 193 converted Azure Landing Zones files, 65,811 nodes, took 0.7 seconds in one process.
- Bad, because the allowlist is a second set of rules to maintain. Each new keyword, and each new form of value a keyword accepts, has to be allowed.
- Bad, because restricted files can't use loops, helpers or data files. Authors who need them can build those files without
-Restrictedin a stage they trust, or generate plain files from a script. - Bad, because it isn't an operating system sandbox. Bylaw itself still runs with the caller's permissions, so a bug in a keyword, or in PowerShell's parameter binding, could still be exploited. The guidance limits what such a bug could reach.
Confirmation¶
- Every converted Azure Landing Zones file, every golden-test file, and the 20 example files that use only keywords build with
-Restricted, with the same output as without it. - For each disallowed construct, a test checks that the file is rejected with the new error at the right line. It also checks that nothing in the file ran, using a statement with a visible side effect.
- A test checks that the allowlist holds exactly Bylaw's keywords, and that no command in the default runspace, such as an alias, has a keyword's name.
- A test checks that no keyword evaluates a string as code: no
Invoke-Expression,[scriptblock]::Create,ExpandStringorNewScriptBlockundersrc/Private/Keywords.
Pros and Cons of the Options¶
A check of the file's syntax tree against an allowlist, before it runs¶
- Good, because it is decided before anything runs, and reports every problem with its line.
- Good, because it works the same on every platform and PowerShell 7.2 or later, with no Windows-only policy.
- Bad, because Bylaw owns the rules and must keep them complete.
Constrained Language Mode in the compile runspace¶
- Good, because PowerShell enforces it, and it is what Windows Defender Application Control (WDAC) uses.
- Bad, because Bylaw doesn't run in it. Tried on 2026-11-30 with
InitialSessionState.LanguageMode = ConstrainedLanguage, building one example gave 123 errors, starting with "Cannot create type. Only core types are supported in this language mode", because Bylaw uses PowerShell classes (ADR-0006). Only a module trusted by a WDAC policy runs in full language there, which needs Windows and a signed module.
PowerShell's restricted language, as used by data sections¶
- Good, because PowerShell checks it, with
ScriptBlock.CheckRestrictedLanguageand an allowed command list. - Bad, because it doesn't allow script block literals. Tried on 2026-11-30,
Policy 'x' { Field a | Must -Be 'b' }was rejected: "Script block literals are not allowed in restricted language mode or a Data section". Every Bylaw file needs them.
Hiding every command except Bylaw's keywords in the runspace¶
This is how Just Enough Administration (JEA) limits a session.
- Good, because the runspace stays in full language, so Bylaw's classes work.
- Bad, because it limits only what the caller invokes directly, not what a script does. Tried on 2026-11-30 with every default command set to private: a direct call to
Get-ChildItemfailed, but a script run in the same runspace listedC:\withGet-ChildItemand called[System.IO.File]::Exists.
A data format instead of PowerShell¶
Examples are .psd1, YAML or JSON.
- Good, because nothing in the file can run.
- Bad, because it is a second language beside the Pester-style one (ADR-0002). Nested conditions,
Requireblocks and pipelines such asField x | Must -Bedon't map well onto data.
Guidance only: review the compiled JSON, and build without credentials¶
- Good, because it needs no code, and it protects what is deployed even if the build is compromised.
- Bad, because it doesn't stop a pull request from running code in CI. Reviewers still have to trust the build that made the JSON they review.
Later¶
- Making restricted mode the default at 1.0, with
-AllowScriptfor full PowerShell, through the language version (ADR-0012). Build-Bylaw -Check, which compares a fresh build with the committed output and fails on a difference, so the guidance needs no script.- Signing the module, so it can be trusted by WDAC on locked-down machines.