Skip to content

Apply SMART v2 scope filters to chained and _has searches - #6087

Closed
adithipo wants to merge 5 commits into
mainfrom
personal/adithip/icm-incident-investigation
Closed

adithipo wants to merge 5 commits into
mainfrom
personal/adithip/icm-incident-investigation

Conversation

@adithipo

@adithipo adithipo commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Makes SMART v2 scope search-parameter filters (e.g. Condition.rs?category=problem-list-item) apply consistently to chained and _has searches, aligning them with direct search and _include/_revinclude.

Changes in SearchOptionsFactory:

  • Permitted: the scope's filters are applied within the chained / _has portion of the query. When multiple filtered scopes cover the target type and each filters on the same single parameter, their values are combined as one comma-separated (OR) value, e.g. category=a,b.
  • Refused (403): multiple filtered scopes that cannot be combined (different or multiple parameters, or comparator values such as ge2020), modified or chained scope parameters (e.g. :not), chains resolving to multiple target types, nested chains, unsupported scope parameters, and _id/_lastUpdated/_type or vector filters.
  • Unchanged: requests where no filtered scopes apply.

Supporting change in the SQL ChainFlatteningRewriter: predicates on a chain link that have no search-parameter table (e.g. _id) are now evaluated on the target resource, so a user's _id predicate can be combined with an appended scope filter.

Applies to SQL and Cosmos, all FHIR versions. No schema changes.

Related issues

Addresses AB#214773.

Testing

  • Unit tests in SearchOptionsFactorySmartScopeTests cover filter application in chains and _has, same-parameter scope merging, the refused (403) cases above, and unchanged behavior for unfiltered scopes.
  • A unit test in ChainFlatteningRewriterTests covers a chain link combining _id with a search-parameter predicate.
  • Both pass locally (dotnet test --project <R4 Core / SqlServer unit test project> --filter-class ...).
  • Integration tests in SmartSearchTests cover filtered scopes applied to forward chains (by name and by _id) and _has against SQL and Cosmos; these run in CI.

FHIR Team Checklist

  • Update the title of the PR to be succinct and less than 65 characters
  • Add a milestone to the PR for the sprint that it is merged (i.e. add S47)
  • Tag the PR with the type of update: Bug, Build, Dependencies, Enhancement, New-Feature or Documentation
  • Tag the PR with Open source, Azure API for FHIR (CosmosDB or common code) or Azure Healthcare APIs (SQL or common code) to specify where this change is intended to be released.
  • Tag the PR with Schema Version backward compatible or Schema Version backward incompatible or Schema Version unchanged if this adds or updates Sql script which is/is not backward compatible with the code.
  • When changing or adding behavior, if your code modifies the system design or changes design assumptions, please create and include an ADR.
  • CI is green before merge Build Status
  • Review squash-merge requirements

Semver Change (docs)

Patch

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@adithipo
adithipo requested a review from a team as a code owner November 23, 2026 19:03
Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:03
@adithipo adithipo added Bug Bug bug bug. Open source This change is only relevant to the OSS code or release. Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Schema Version unchanged labels Nov 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical scope-validation bypass can disclose Condition matches excluded by the scope, while predicate composition also causes SQL and comparator-parsing failures.

3 open issues
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file
What changed in this PR

This PR extends SMART v2 scope filtering to chained and _has searches in the shared Core layer, aiming to align authorization with direct and include searches without schema changes.

Changes:

  • Injects scope filters into chain predicates and merges compatible same-parameter scopes.
  • Rejects filter combinations that cannot be enforced, with a new localized error message.
  • Adds unit tests for filter application, scope merging, rejection cases, and unchanged behavior.
File Description
src/​Microsoft.Health.Fhir.Shared.Core/​Features/​Search/​SearchOptionsFactory.cs Applies and validates SMART scope filters within chains.
src/​Microsoft.Health.Fhir.Shared.Core.UnitTests/​Features/​Search/​SearchOptionsFactorySmartScopeTests.cs Adds chain-filtering tests and expression helpers.
src/​Microsoft.Health.Fhir.Core/​Resources.resx Adds the unsupported chain-filter error message.
src/​Microsoft.Health.Fhir.Core/​Resources.Designer.cs Exposes the new localized resource string.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:33
@adithipo adithipo added the No-ADR ADR not needed label Nov 23, 2026
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Mocked-parser tests with invalid R4 chains leave SQL/Cosmos scope enforcement unverified, while single scopes with multiple filters bypass the documented restriction and new refusals return HTTP 403 instead of the documented 400.

2 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Enforce the filter limit within each scope

src/​Microsoft.Health.Fhir.Shared.Core/​Features/​Search/​SearchOptionsFactory.cs:1389

A single scope containing multiple filters bypasses the single-parameter restriction here. For example, Condition.rs?category=problem-list-item&code=x is returned unchanged and both predicates are injected into _has, although the PR explicitly lists multiple filters within one scope as refused. Check each scope's parameter count before this early return so the documented restriction also applies when there is only one applicable scope.

3 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 23, 2026 19:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

SQL predicate rewriting loses the Patient chain level and drops valid authorized matches, while multi-filter scopes bypass the required 400 rejection.

2 open issues
2 previously missed issues

In code that hasn't changed since last review

High severity Preserve chain level when rewriting long string predicates

src/​Microsoft.Health.Fhir.Shared.Core/​Features/​Search/​SearchOptionsFactory.cs:1358

With a Patient.rs?name=<257-character value> scope, the injected filter causes SQL searches such as Observation?subject:Patient._id=p1 to lose valid authorized matches. StringOverflowRewriter changes the name predicate, and the inherited SqlExpressionRewriter.VisitTable resets its ChainLevel from 1 to 0, so SQL intersects Patient name rows with the root Observation keys rather than the Patient target. Preserve the existing chain level when rewriting table predicates and add a SQL pipeline regression test for this case.

Medium severity Reject multiple parameters before the single-scope shortcut

src/​Microsoft.Health.Fhir.Shared.Core/​Features/​Search/​SearchOptionsFactory.cs:1389

The single-scope branch below returns every parameter without checking their count. Consequently, a scope such as Condition.rs?category=problem-list-item&code=x is injected into _has rather than rejected, contradicting the PR's explicit 400 contract for multiple filters within one scope. Check that each applicable scope contains exactly one parameter before taking the single-scope shortcut, and cover this refusal case.

1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because an unpublished moderate finding indicates that a scope with multiple filters can bypass the advertised 403 refusal.

No open issues 🎉

2 resolved since last review
1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 20:42
@adithipo adithipo closed this Nov 23, 2026
@adithipo
adithipo deleted the personal/adithip/icm-incident-investigation branch November 23, 2026 20:45
@adithipo

Copy link
Copy Markdown
Contributor Author

Superseded by #6089 (branch renamed).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Authorization-sensitive changes span SQL and Cosmos, but runtime provider validation was unavailable and added tests cover only R4/R4B, requiring maintainer review of broader provider and cross-version CI evidence.

No open issues 🎉

1 file not reviewed
  • src/Microsoft.Health.Fhir.Core/Resources.Designer.cs: Generated file

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure API for FHIR Label denotes that the issue or PR is relevant to the Azure API for FHIR Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug. No-ADR ADR not needed No-PaaS-breaking-change Open source This change is only relevant to the OSS code or release. Schema Version unchanged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants