Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove homebrew app casks #24593

Merged
merged 7 commits into from
Dec 24, 2024
Merged

Remove homebrew app casks #24593

merged 7 commits into from
Dec 24, 2024

Conversation

mostlikelee
Copy link
Contributor

#22944

Removing homebrew casks that install app bundles from the macos software detail query as they are already reported in the apps table. apps table provides bundle_identifer which is used in vulnerability (CPE) matching to grab the correct vendor.

  • Changes file added for user-visible changes in changes/, orbit/changes/ or ee/fleetd-chrome/changes.
    See Changes files for more information.
  • Added/updated tests
  • Manual QA for all new/changed functionality

@mostlikelee mostlikelee marked this pull request as ready for review December 10, 2024 16:25
path AS installed_path
FROM homebrew_packages
WHERE type = 'cask'
AND NOT EXISTS (SELECT 1 FROM file WHERE file.path LIKE CONCAT(homebrew_packages.path, '/%%%%', '/%%.app%%') LIMIT 1);
Copy link
Member

Choose a reason for hiding this comment

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

Double checking: LIKE with double %% (recursive search) on the file table can only be used at the end.

Copy link
Member

Choose a reason for hiding this comment

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

Double wildcards can NEVER be used mid-string (infix)

https://blog.1password.com/the-file-table-osquerys-secret-weapon/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

interesting...i'll have to figure out why this is currently working

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the WHERE clause to be safe, but it's interesting that this works in osqueryi:

SELECT path FROM file WHERE file.path LIKE CONCAT('/opt/homebrew/Caskroom/firefox', '/%%%%', '/%%.app%%') LIMIT 1;

Copy link
Member

Choose a reason for hiding this comment

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

IIRC a query that uses %% not in the end "works" (returns results) but is it actually recursive?

path AS installed_path
FROM homebrew_packages
WHERE type = 'cask'
AND NOT EXISTS (SELECT 1 FROM file WHERE file.path LIKE CONCAT(homebrew_packages.path, '/%%%%', '/%%.app%%') LIMIT 1);
Copy link
Member

Choose a reason for hiding this comment

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

Am probably missing something, why not do CONCAT(homebrew_packages.path, '/%%%%/%%.app%%').

Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

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

Left some questions.

@lucasmrod lucasmrod self-assigned this Dec 10, 2024
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.80%. Comparing base (669900c) to head (4e5cc48).
Report is 227 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24593      +/-   ##
==========================================
+ Coverage   63.51%   63.80%   +0.28%     
==========================================
  Files        1592     1605      +13     
  Lines      151140   152819    +1679     
  Branches     3885     3885              
==========================================
+ Hits        95994    97500    +1506     
- Misses      47492    47538      +46     
- Partials     7654     7781     +127     
Flag Coverage Δ
backend 64.60% <ø> (+0.30%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mostlikelee mostlikelee marked this pull request as draft December 10, 2024 18:01
@noahtalerman noahtalerman removed their request for review December 13, 2024 14:07
@mostlikelee mostlikelee marked this pull request as ready for review December 17, 2024 13:18
path AS installed_path
FROM homebrew_packages
WHERE type = 'cask'
AND NOT EXISTS (SELECT 1 FROM file WHERE file.path LIKE CONCAT(homebrew_packages.path, '/%%') AND file.path LIKE '/%.app%' LIMIT 1);
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this file.path LIKE '/%.app%' just matching something like /Google Chrome.app? (on the root path / only)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe file.path is looking for a string match, which is why this is working:

osquery> SELECT path FROM file WHERE file.path LIKE CONCAT('/opt/homebrew/Caskroom/firefox', '/%%') AND file.path LIKE '/%.app%' LIMIT 1;
+-----------------------------------------------------+
| path                                                |
+-----------------------------------------------------+
| /opt/homebrew/Caskroom/firefox/126.0.1/Firefox.app/ |
+-----------------------------------------------------+
osquery> SELECT path FROM file WHERE file.path LIKE CONCAT('/opt/homebrew/Caskroom/ngrok', '/%%') AND file.path LIKE '/%.app%' LIMIT 1;

I think either should work, it seems clearer to use %.app% so i'll update

rachaelshaw
rachaelshaw previously approved these changes Dec 17, 2024
Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

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

LGTM.

Left one nit comment and the following question to discuss:
On the following sample file query:

SELECT * FROM file WHERE file.path LIKE '/usr/local/Caskroom/jd-gui/%%' AND file.path LIKE '/%.app';

The first LIKE is used to walk the directory and the second LIKE uses string matching?

server/service/osquery_utils/queries.go Show resolved Hide resolved
path AS installed_path
FROM homebrew_packages
WHERE type = 'cask'
AND NOT EXISTS (SELECT 1 FROM file WHERE file.path LIKE CONCAT(homebrew_packages.path, '/%%%%') AND file.path LIKE '%%.app%%' LIMIT 1);
Copy link
Member

Choose a reason for hiding this comment

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

After some testing I found out what's happening behind the scenes:

SELECT 1 FROM file 
WHERE file.path LIKE CONCAT(homebrew_packages.path, '/%%')
AND file.path LIKE '%.app' LIMIT 1
  1. osquery will first walk the first directory in the LIKE: CONCAT(homebrew_packages.path, '/%%') and generate the list of paths.
  2. osquery will then walk the other directory in the LIKE: '%.app', which given it's not an absolute path will try to look for .apps in the current working directory (which for osqueryd usually run as root will be /) and generate the list of paths, which on macOS will be an empty list.

So, only (1) will generate paths to process.

After (1) and (2), the sqlite engine will perform its own filtering with LIKE (string matching) over the returned paths in (1).

So, basically LIKE is used first by osquery code to generate the paths AND then by the sqlite engine which will do string matching as usual.

The trick here is that we can use this because we are expecting / to contain no apps and no large number of files or directories (I was not able to create files or .apps in /).

Copy link
Member

Choose a reason for hiding this comment

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

Rather than relying on "no large number of files or directories" to make this query efficient, should we instead use filename LIKE '%.app'?

Copy link
Member

@lucasmrod lucasmrod left a comment

Choose a reason for hiding this comment

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

This reverts commit c9746df.
Copy link
Member

@iansltx iansltx left a comment

Choose a reason for hiding this comment

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

Approving for docs

@iansltx
Copy link
Member

iansltx commented Dec 23, 2024

...oh wait, this is host vitals rather than APIs so @marko-lisica has to approve this

@mostlikelee mostlikelee merged commit f6f35be into main Dec 24, 2024
27 checks passed
@mostlikelee mostlikelee deleted the 22944-hombrew-casks branch December 24, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

7 participants