← Back to Pull requests

test(iast): resolve Filelock import targets through public API #22052

Closed
unknown closed this pull request without merging on Nov 24, 2026 12:56 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 04:24 UTC
Author

Description

Related to https://datadoghq.atlassian.net/browse/APPSEC-71951

The package-compatibility tests import filelock._api for both Filelock 3.14 and the latest release. Filelock 4.3 moved BaseFileLock to filelock._core._lock and removed _api, so both the plain and the IAST-patched import checks fail on the latest version before exercising the package.

The test runner now accepts a module:attribute selector and validates the submodule that defines the attribute, and Filelock is selected through its public BaseFileLock export:

  • The selector is resolved with pkgutil.resolve_name. Plain module names import exactly as before.

  • The attribute must be a class or function, and its __module__ must be a submodule of the selected package.

  • When the attribute has Python code, at least one of its functions or methods must be in that module's file. The runner collects the code files of the function, or of the methods of every class in the MRO that shares that __module__. It unwraps decorators and property getters, ignores generated code such as a dataclass __init__, and only counts functions whose __qualname__ is in the owning class's body, so helpers that dataclasses attach from dataclasses.py on Python 3.13+ don't count. A class with no such methods, such as an exception subclass, must have its class statement in that module's source (checked with inspect.getsource). Classes from compiled extensions are rejected with an explicit message, since IAST can't patch them.

  • This rejects re-export modules. For example, Filelock 4.3 sets __module__ = "filelock.asyncio" on its async classes, but their code is in filelock/_async_lock/, so filelock:AsyncFileLock is rejected. It also rejects classes re-exported from another package. Instances, constants and other non-class, non-function attributes get a ValueError naming the selector.

  • The runner prints which module a selector resolved to, so a failure shows which file was checked. The pinned and latest versions now resolve to different files.
  • The Filelock endpoint now takes its lock inside a per-request temporary directory, the same pattern pkg_iniconfig.py uses for xdist. It previously created foobar in the current directory, and Filelock 3.14 doesn't delete it on Unix, so each run left an empty file at the repository root. The endpoint docstring now names the pinned version, 3.14.0.

    PackageForTesting documents the module:attribute form of import_module_to_validate.

    The local template environment also installs urllib3, which tests/appsec/app.py imports at the top level. CI already gets it through requests.

    Customer impact

    None, test-only change.

    Testing

  • appsec_iast_packages -k filelock, in a freshly installed local environment with Filelock 4.3 as latest: passes on Python 3.12, 3.13 and 3.15 and leaves no file in the repository root. On main the same selection fails with No module named 'filelock._api'. The endpoint check runs against the pinned 3.14 only; the latest version gets the import checks.

  • Selector checks with Filelock 4.3.1 and 3.14.0: filelock:BaseFileLock resolves to filelock._core._lock and filelock._api respectively. filelock:AsyncFileLock (4.3), filelock:__version__ and collections:OrderedDict are rejected with a ValueError. json:JSONDecoder resolves to json.decoder.

  • Selector checks against a small fixture package: a class whose first method uses @contextmanager, a dataclass with only fields, a function wrapped by a functools.wraps decorator from another file, and a property-only class all resolve to their defining module. A method-less exception class resolves too. A module-level instance, and classes re-exported with an overridden __module__ (with or without methods), are rejected. A slots dataclass resolves too, and a class from a compiled extension (multidict 7.2.1's MultiDict) is rejected as compiled. Checked on Python 3.12, 3.13 and 3.15, since inspect.getsource locates classes differently from 3.13 on and dataclasses gain extra methods there.

  • scripts/lint checks passes.

  • CI here may not exercise Filelock 4.3: the job resolves "latest" through CI's configured package index, which may not offer it yet. Branches that still import filelock._api also pass there, so the local runs above are what cover 4.3.
  • Risks

    Low. Only selectors containing a colon take the new path, and Filelock is the only package that uses one.

    Additional Notes

    Discovered while validating https://github.com/DataDog/dd-trace-py/pull/22051 and kept separate, since the Filelock module move is independent of that change. No release note is needed for this test-only change.

    Test Management IDs for the Filelock entries this fixes:

    DD_46CYMF DD_TD4WM4 DD_ZM3P5R DD_E823VQ DD_DCJ9DQ DD_T91B6L DD_C0WL6V DD_3WVAQ1 DD_VDK12I DD_6VF95K

    Diff: 608 lines (+456 / -152) across 7 files Reviewer: avara1986 Review duration: 512m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.50) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 608 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.384
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #22052"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.1% (MEDIUM) · Higher = larger blast radius
    50.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.7% · Safe to merge
    21.7%