Skip to content

fix(scanner/imports): use code-side alias for named ES imports#14

Merged
boorad merged 6 commits into
mainfrom
fix/aliased-named-imports
Apr 28, 2026
Merged

fix(scanner/imports): use code-side alias for named ES imports#14
boorad merged 6 commits into
mainfrom
fix/aliased-named-imports

Conversation

@boorad
Copy link
Copy Markdown
Contributor

@boorad boorad commented Apr 28, 2026

Summary

  • Bug fix (closes Aliased named imports use original name instead of code-side alias #8): Capture the actual code-side binding for aliased ES imports (import { authorize as auth } from "../policy") so the regex in is_enforcement_point matches against the alias actually used in code, not the original name.
  • Bug fix (rules/java): Fix access-decision-voter rule to match generic and fully-qualified Spring Security interfaces (e.g. AccessDecisionVoter<Object>, org.springframework.security.access.PermissionEvaluator). The existing fixture for the generic case had been failing on main.
  • Test cleanups from prior code review: Consolidate the four java_security_interface_impl_* tests into one table-driven test, and pin can* / hasAny* predicates with explicit fixtures in custom-authz-call.

Changes

  • src/scanner/imports.rsTS_NAMED_IMPORT_QUERY now uses an alternation [(import_specifier alias: ...) (import_specifier !alias name: ...)], capturing the alias when present and the original name otherwise. New tests cover aliased-only imports, mixed aliased+plain in one statement, and enforcement-point regex matching via alias. Existing aliased-import test also asserts the original non-policy name is excluded.
  • src/scanner/matcher.rs — Four java_security_interface_impl_* tests collapsed into a single table-driven java_security_interface_impl_cases with per-case context preserved in assertion messages. (CodeRabbit nitpick, fix(rules/java): repair invalid query in security-interface-impl #11)
  • rules/java/custom-authz-call.toml — Added positive fixtures for can* predicates (canEditRole, canManagePermission, canAssignAdminRole for keyword-in-middle), and explicit negative fixtures for hasAnyRole / hasAnyAuthority framework APIs. (CodeRabbit nitpick, feat(rules/java): heuristic detection of custom authz service calls #13)
  • rules/java/access-decision-voter.toml — Added generic_type and scoped_type_identifier query branches mirroring security-interface-impl.toml. Pinned the fully-qualified case with a new fixture.

Test plan

  • cargo check
  • cargo clippy --all-targets -- -D warnings
  • cargo fmt --check
  • cargo test — 105 / 105 pass
  • cargo run -- rules test — 122 / 122 pass (the 1 pre-existing failure on main is now also fixed)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed TypeScript aliased import handling to correctly capture and track actual identifiers used in code.
  • Tests

    • Expanded Java security rule test coverage for interface implementations and authorization predicates.
    • Improved detection accuracy for Spring Security and custom authorization patterns.

boorad added 6 commits April 28, 2026 13:30
`import { authorize as auth } from "../policy"` previously captured
`authorize` (the original name) instead of `auth` (the binding actually
used in code). The word-boundary regex in `is_enforcement_point` then
failed to match calls like `auth(...)`, misclassifying enforcement
points as inline auth findings.

Update `TS_NAMED_IMPORT_QUERY` to alternate on the `import_specifier`:
capture `alias` when present, otherwise capture `name` (using the
`!alias` negative-field operator). Add tests for purely-aliased,
mixed, and enforcement-point-via-alias cases.

Closes #8
Collapse the four `java_security_interface_impl_*` tests (plain,
fully-qualified, generic, no-false-positive) into a single
table-driven test. Failure messages include the case name so
per-case context is preserved.

Addresses CodeRabbit nitpick on #11.
The rule's inclusive `is|has|can` matcher and exclusive `hasAny*`
not_match weren't directly fixture-tested. Add positive fixtures
for `canEditRole` and `canManagePermission`, and explicit negative
fixtures for `hasAnyRole` and `hasAnyAuthority` so neither side of
the predicate can silently regress.

Addresses CodeRabbit nitpick on #13.
…l name

`detects_aliased_named_policy_import` asserted that the alias from a
non-policy path (`Router as R`) is not captured, but didn't explicitly
assert that the original name (`Router`) is also excluded. Add that
assertion so both halves of the path-filtering contract on aliased
imports are pinned, and split the inline comment into its two intents
(policy-path alias capture vs. non-policy path filtering).
…call

The rule's non-greedy inclusive regex
`^(is|has|can)\w*?(Admin|...|Role|...)` is exercised for `is*` with
keyword-in-middle (via `isAdminForAccount`) but not for `can*`. Add a
`canAssignAdminRole` fixture so the `can`-prefixed keyword-in-middle
case can't silently regress.
The `access-decision-voter` rule only matched
`(type_list (type_identifier))`, so generic Spring Security interfaces
like `AccessDecisionVoter<Object>` (which parse as `generic_type`) and
fully-qualified names like `org.springframework.security.access.
PermissionEvaluator` (which parse as `scoped_type_identifier`) were
missed. The existing `AccessDecisionVoter<Object>` fixture in the
rule's own test block had been failing on main as a result.

Add `generic_type` and `scoped_type_identifier` query branches,
mirroring `security-interface-impl.toml`, and pin the
fully-qualified case with a new fixture.
@boorad boorad self-assigned this Apr 28, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e911785f-38f3-4465-a4a2-32db20491e44

📥 Commits

Reviewing files that changed from the base of the PR and between 59125ce and 7919ba3.

📒 Files selected for processing (4)
  • rules/java/access-decision-voter.toml
  • rules/java/custom-authz-call.toml
  • src/scanner/imports.rs
  • src/scanner/matcher.rs

📝 Walkthrough

Walkthrough

The PR expands Java security rule coverage for interface matching (supporting generic and fully-qualified types), fixes TypeScript aliased import capture in the scanner, enhances authorization predicate test cases with Spring API exclusions, and refactors Java interface tests into a table-driven structure.

Changes

Cohort / File(s) Summary
Java Security Rules
rules/java/access-decision-voter.toml, rules/java/custom-authz-call.toml
Expanded Tree-sitter queries and test coverage: access-decision-voter.toml now matches parameterized generic interfaces and fully-qualified scoped_type_identifier implementations; custom-authz-call.toml adds positive fixtures for can* predicates and negative fixtures for Spring framework APIs (hasAnyRole, hasAnyAuthority) to prevent false positives.
Scanner Import & Test Refactoring
src/scanner/imports.rs, src/scanner/matcher.rs
Fixed named-import alias capture in TypeScript queries to use alias names instead of original identifiers; added unit tests for aliased/mixed imports and enforcement-point detection. Consolidated four separate Java interface implementation tests into a single table-driven test with expect_match assertions.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Poem

🐰 A rabbit hops through queries bright,
Aliases fixed—they're captured right!
Generic types and scopes unfold,
Security rules, both new and bold!
Tests consolidated, neat and tight, 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(scanner/imports): use code-side alias for named ES imports' directly and specifically describes the primary bug fix in the PR, which is capturing the alias for named ES imports in enforcement-point detection.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@amazon-q-developer amazon-q-developer Bot left a comment

Choose a reason for hiding this comment

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

This PR successfully fixes the aliased ES import bug (#8) and adds important missing functionality for Java rules. The changes are well-tested with comprehensive fixtures covering edge cases. All modifications are working correctly and ready to merge.

Key improvements:

  • Aliased named imports now correctly capture the code-side binding (e.g., import { authorize as auth } captures "auth")
  • Java access-decision-voter rule now matches generic types (AccessDecisionVoter<Object>) and fully-qualified interfaces
  • Enhanced test coverage with additional fixtures for can* predicates and framework API exclusions
  • Test refactoring consolidates four separate tests into one table-driven test

The implementation correctly handles the alternation pattern in Tree-sitter queries and all tests pass (105/105 unit tests, 122/122 rule tests).


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

@boorad boorad merged commit 1d97922 into main Apr 28, 2026
3 checks passed
@boorad boorad deleted the fix/aliased-named-imports branch April 28, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aliased named imports use original name instead of code-side alias

1 participant