Microsoft 365 Security as Code — Part 5: Conditional Access as code, and where policies look right but are not
Section 5.2.2 of the benchmark has 17 Conditional Access controls. Writing them as code was the easy part. This is about the policies that read correctly, show as enabled, and still do not protect what you think they protect.
This is part 5 of a series on managing Microsoft 365 security as code, based on the CIS Microsoft 365 Foundations Benchmark. Each part can be read on its own, and all parts are listed on the series page.

Conditional Access is the part of the benchmark that is most natural to write as code. It is also the most tricky to get right. A policy is a small object with users, applications, conditions and a grant. Terraform manages it well, a pull request shows exactly what changes, and the controls in section 5.2.2 map to policies quite directly.
Writing the policies was the easy part. Checking whether they do what their names say was harder, and it found more problems. This part is about those problems, because I think they matter more than the code itself. Everything below comes from the (sometimes) painful process of testing and fixing forward getting it right.
A policy is not a firewall
One idea explains most of what follows, and a colleague pointed it out to me when he read the draft: Conditional Access does not work like a firewall.
A firewall ends with an implicit deny. If no rule allows the traffic, it is blocked. Conditional Access has no such rule at the end. If no policy applies to a sign-in, the sign-in is allowed. So a policy never closes a door by itself. It only acts on the sign-ins that match its conditions, and everything else passes through.
This changes how you must read a policy. If a policy requires a compliant device and applies only to Windows, then Android, iOS and Linux devices are not blocked. They are simply not in scope, so nothing is checked. If you want only compliant devices to connect, a rule that allows them is not enough. You need a rule that blocks the devices that are not compliant, and you need to check that the rule covers every platform, not only the ones you had in mind. Several of the cases below are this same mistake in a different form.
1. Report-only is not enforced
A policy in report-only mode is evaluated and logged, but it blocks nothing. It is a good way to test a policy before it affects people, and that is what we use it for.
The risk is what happens next. The policy exists, it has the right name, it reads correctly in code, and the control looks done. Control 5.2.2.5 asks for phishing-resistant sign-in for administrators. We have two policies for it, and both are in report-only mode. The test tool accepts their scope and their strength, and fails them only on the state. The Entra ID sign-in logs still show whether the policy was hit or not, which is a good indication for if the policy behaves as expected.
The rule is: every policy that has impact (which is basically all of them) will be monitored for at least 48 hours so we are sure that it behaves as expected. A control is met only when the policy is enabled, and the register shows which policies are still report-only and why.
2. A filter that selects the wrong devices
This one is easy to get wrong, and the code looks like it makes sense. An older policiy used a device filter like this:
devices {
filter {
mode = "include"
rule = "device.isCompliant -eq True"
}
}
The intention is "require a compliant device". But the filter does not say what is required. It says which devices the policy applies to. With include, the policy applies only to devices that are already compliant. A device that is not compliant does not match the filter, so the policy does not apply to it, and the user signs in without a check. The policy only looked at the devices that did not need it.
This is the firewall habit at work: the rule was written as "allow compliant devices", but in Conditional Access the policy must describe the devices you want to act on. The better pattern is to require the managed device in the grant, or to use the filter in exclude mode, as our current unmanaged-device policies do. That policy then applies to every device that is not compliant:
devices {
filter {
mode = "exclude"
rule = "device.isCompliant -eq True"
}
}
3. A policy that targets no application
Control 5.2.2.13 asks for periodic reauthentication for all users. We have a policy for it, it is enabled, and it sets a sign-in frequency of five days for all users except the break-glass accounts.
The application list says ["None"]. "None" is a valid value that matches no application, so the policy never applies to any sign-in. Nothing in the plan or the policy list shows a problem. The accurate statement is that no cloud app is included. It is a no-op that looks like a control.
4. A name that says something the setting does not
In part 1 I wrote about the control for Intune enrolment, where we relaxed "every time" to a freshness of two hours because device builds failed. The setting is two hours, and the decision is recorded as a deviation.
But the policy's display name still says "sign-in frequency to every time", because the name follows the benchmark wording. Anyone who reads the portal will believe the control is met. The name is part of the evidence an auditor reads, so it must say what the code does. Make sure that when you change a control, it is reflected in the name. It can be as as simple as adding a "deviation" suffix.
5. Two risk policies that overlap
Control 5.2.2.7 asks for a response to risky sign-ins, and control 5.2.2.8 asks to block them. The first requires multi-factor authentication for medium and high sign-in risk. The second blocks high risk for members and excludes guests.
For a member with a high-risk sign-in, both policies match, and the block wins. So the multi-factor policy only matters for medium risk, and for guests at high risk. That works, but you have to reason it out, because the policy list does not show it. It also means 5.2.2.8 is only partly met: medium risk gets a multi-factor prompt, not a block. That is a design decision, and it belongs in the register as a deviation.
6. A grouping that covers less than its name
For administrator access, one of our two device policies targets the application group called "Microsoft Admin Portals", together with Azure management. The name suggests all admin centres. In practice the group resolves to four application IDs. The admin centres for Intune, Teams, Purview and the Power Platform are not in it, and neither is Microsoft Graph. A policy that says "admin portals" is not the same as a policy that covers the admin portals you use.
7. The mobile browser falls through
Our managed-device policy for 5.2.2.9 is limited to desktop and mobile apps, and it leaves out Android and iOS. That is deliberate. Browsers are handled by separate policies with restricted sessions, and phones are handled by app protection policies.
When I read these together, I found one case that nothing covers: a person who opens Teams or SharePoint in the browser of a personal phone. The managed-device policy excludes the platform. The app protection policy applies to apps, not browsers. The restricted-session policies apply to one group of users. So that sign-in falls through every device policy. Each policy is correct on its own, and the gap is only visible when you read them together.
The same is true for platforms. A policy written for Windows only leaves Linux, Android and iOS with no check at all. We have a policy that blocks every platform except Windows and macOS, and it is in report-only mode today, so it does not close this gap yet. Eventually this scenario falls into the same category as opening from a browser on a personal device, which is covered off with Defender for Cloud Apps.
What I changed in how I work
- Every policy name must match its settings. I check this in review, in the same step as the control number.
- A control is "met" only when the policy is enabled and targets something. Report-only, disabled and "None" are each recorded as a state in the register, with a reason.
- Use the "What If" functionality in Conditional Access. It gives you insight into the intended behavior of your CA policies.
- New CA policies ALWAYS need to be deployed with reportOnly and observed for at least 48 hours.
- I read policies in groups, by persona and by application, not one at a time. The mobile browser gap and the overlapping risk policies only show up that way.
PIM activation gotcha
Policies that target administrator roles, such as 5.2.2.1, 5.2.2.4 and 5.2.2.5, behave differently for people who hold a role as eligible and activate it later. As Microsoft describes it, role targeting applies after activation.
In part 6 I move to Entra ID hardening, and the controls that have no API.
Get in touch
I help teams that need Microsoft 365 security to be written down as code, reviewed like code, and provable to an auditor, especially in financial services and other regulated environments. If you have Conditional Access policies that you have never read as a group, or you want a second pair of eyes before an audit, you can find more at ascode.nl.
This post is part of the series Microsoft 365 Security as Code, where you can find all the parts.