Skip to content

Fold the duplicated per-browser promotion loop into a helper - #352

Open
dchaudhari7177 wants to merge 1 commit into
RyanDFIR:mainfrom
dchaudhari7177:refactor/335-promotion-loop
Open

Fold the duplicated per-browser promotion loop into a helper#352
dchaudhari7177 wants to merge 1 commit into
RyanDFIR:mainfrom
dchaudhari7177:refactor/335-promotion-loop

Conversation

@dchaudhari7177

Copy link
Copy Markdown
Contributor

Closes #335.

absorb_browser_analysis() takes a browser_analysis and folds the common collections plus the promotion loop, and both branches of run() become one line each.

The collections are folded by attribute name rather than by branching on the browser, as the issue asks:

BROWSER_COLLECTIONS = (
    "parsed_artifacts", "parsed_storage",
    "parsed_extension_data", "parsed_sync_data",
    "version", "preferences",
)

Chrome's parsed_extension_data and parsed_sync_data therefore need no special case, and Firefox growing one later needs no edit here.

On the installed_extensions decision

The issue asks whether that block stays outside the helper. It stays outside, and the reason turned out to be stronger than "it is different": moving it in is not behaviour-preserving.

I moved it in first and diffed the output. It produced one extra record:

{"source_long": "Chrome Extensions", "profile": "tests/fixtures\firefox",
 "ext_id": "uBlock0@raymondhill.net", "name": "uBlock Origin", ...,
 "data_type": "chrome:extension:installed"}

So Firefox does produce an installed_extensions block. It is parsed and then dropped, for exactly the same reason session_structure used to be — the promotion lives only in the Chrome branch. But promoting it as-is sends a Firefox profile's add-ons through the Chrome shape, and they come out labelled Chrome Extensions with data_type chrome:extension:installed, which is wrong for a Firefox profile.

That is a real finding and probably its own issue, but it is a behaviour change, and this issue is about removing duplication. Rather than smuggle it in, the block stays in the Chrome branch with a comment, and the helper's docstring records what is waiting there. Happy to open a separate issue for the Firefox side, or to fold it in here with the labelling fixed if you would rather have it in one go.

Two small notes on faithfulness

  • The session_structure check stays hasattr, not a truthiness test. getattr(..., None) is not None would quietly change what an empty-but-present structure does, and this is the promotion that had already gone missing once.
  • The promotion loop iterates browser_analysis.__dict__.items() instead of indexing __dict__[item] three times. Same values, same order, same broad except — I did not narrow it, since a promotion failure is deliberately non-fatal here.

Verification

python -m pytest tests/ -q --ignore=tests/corpus     248 passed, 2 skipped, 80 subtests passed
python -m pytest tests/test_corpus_e2e.py -q         3 passed, 2 skipped, 22 subtests passed

Output equivalence, which is the thing that actually matters for a refactor. Same fixtures, JSONL, before and after:

cmp before.jsonl after.jsonl   ->  identical, 377 records

The 120-line diff is 69 insertions / 51 deletions, most of which is the helper's docstring explaining the installed_extensions decision.

The Chrome and Firefox branches of run() carried near-verbatim copies of
the same parsed_artifacts/parsed_storage/version/preferences folding, the
same session_structure promotion, and the same {presentation, data}
promotion loop.

The drift that predicts had already happened: session_structure promotion
existed only in the Chrome branch, so when Firefox started producing one
it rendered nothing until the same seven lines were copied across.

absorb_browser_analysis() folds the collections by attribute name rather
than by branching on the browser, so Chrome's parsed_extension_data and
parsed_sync_data need no special case and a browser that grows a
collection later needs no edit here.

installed_extensions stays in the Chrome branch. It is the one thing in
the two branches that is a genuine difference rather than duplication,
and moving it is not behaviour-preserving: Firefox does produce an
installed_extensions block, it is simply never promoted, and promoting it
emits a Firefox profile's add-ons as "Chrome Extensions" with data_type
chrome:extension:installed. That is worth fixing, but it is a different
change from removing this duplication.

Output is byte-identical on the test fixtures, 377 records before and
after.

Closes RyanDFIR#335
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.

Factor out the duplicated per-browser promotion loop in AnalysisSession

1 participant