Skip to content

fix: globalVariable upsertData illogic causes false negative in detection#64

Merged
jietian-sts merged 1 commit intoantgroup:mainfrom
Center-Sun:fix/upsert-globalvar-illogic
Aug 13, 2025
Merged

fix: globalVariable upsertData illogic causes false negative in detection#64
jietian-sts merged 1 commit intoantgroup:mainfrom
Center-Sun:fix/upsert-globalvar-illogic

Conversation

@Center-Sun
Copy link
Contributor

@Center-Sun Center-Sun commented Aug 8, 2025

Thank you for your contribution to CloudRec!

What About:

  • Server (java)
  • Collector (go)
  • Rule (opa)

Description:

This PR fixes a JSON double-encoding issue in the RuleScanContext where global variable data was being incorrectly serialized, causing data corruption. And cause detection failure for rules that rely on global variables during scanByRule and scanByGroup operations

Issue Details

  • globalVariable.getData() returns a JSON string representation of the data
  • opaRepository.upsertData() internally serializes the input data to JSON again
  • This resulted in double JSON encoding, where the final stored data contained escaped quotes and malformed JSON structure
  • Example of the issue:
    • Original data: {"key": "value"}
    • After getData(): "{\"key\": \"value\"}"
    • After upsertData(): "\"{\\\"key\\\": \\\"value\\\"}\"" (double-escaped)

Summary by Sourcery

Bug Fixes:

  • Parse the JSON string from globalVariable.getData() before passing it to opaRepository.upsertData to avoid double encoding and ensure correct rule detection

@gemini-code-assist
Copy link
Contributor

Important

Installation incomplete: to start using Gemini Code Assist, please ask the organization owner(s) to visit the Gemini Code Assist Admin Console and sign the Terms of Services.

@sourcery-ai
Copy link

sourcery-ai bot commented Aug 8, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR resolves a JSON double-encoding issue by parsing the global variable data string before calling opaRepository.upsertData, ensuring valid JSON objects are stored and eliminating malformed data and false detection negatives.

Sequence diagram for global variable upsertData JSON handling

sequenceDiagram
    participant RuleScanContext
    participant RuleAgg
    participant GlobalVariable
    participant OpaRepository
    RuleScanContext->>RuleAgg: getGlobalVariables()
    loop for each GlobalVariable
        RuleScanContext->>GlobalVariable: getPath()
        RuleScanContext->>GlobalVariable: getData()
        RuleScanContext->>RuleScanContext: JSON.parse(globalVariable.getData())
        RuleScanContext->>OpaRepository: upsertData(path, parsedData)
    end
Loading

Class diagram for RuleScanContext and OpaRepository interaction

classDiagram
    class RuleScanContext {
        - opaRepository: OpaRepository
        load(ruleAgg: RuleAgg)
    }
    class OpaRepository {
        upsertData(path: String, data: Object)
        createOrUpdatePolicy(regoPath: String, regoPolicy: String)
    }
    class GlobalVariable {
        getPath(): String
        getData(): String
    }
    class RuleAgg {
        getRegoPath(): String
        getRegoPolicy(): String
        getGlobalVariables(): List~GlobalVariable~
    }
    RuleScanContext --> OpaRepository
    RuleScanContext --> RuleAgg
    RuleAgg --> GlobalVariable
    GlobalVariable <.. RuleScanContext : uses
Loading

File-Level Changes

Change Details Files
Prevent double JSON encoding of global variable data by parsing before upsert
  • Changed opaRepository.upsertData to receive a parsed object instead of a raw JSON string
app/application/src/main/java/com/alipay/application/service/rule/job/RuleScanContext.java

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @Center-Sun - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@j3ttt j3ttt requested a review from jietian-sts August 11, 2025 03:38
@j3ttt
Copy link
Collaborator

j3ttt commented Aug 13, 2025

lgtm

@jietian-sts jietian-sts self-requested a review August 13, 2025 08:09
@jietian-sts jietian-sts merged commit 2f1e76d into antgroup:main Aug 13, 2025
1 check passed
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.

3 participants