Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions server/service/osquery_utils/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"strconv"
"strings"
"time"
"unicode"

"github.com/fleetdm/fleet/v4/pkg/str"
"github.com/fleetdm/fleet/v4/server/config"
Expand Down Expand Up @@ -1760,10 +1761,20 @@
`\program files (x86)\`: {},
}
knownPaths := make(map[string]struct{})
// >>> OPENFRAME(program-files-scan-duplicates): also match by folder name — upstream fleetdm/fleet#59098

Check notice on line 1764 in server/service/osquery_utils/queries.go

View check run for this annotation

Flamingo / Flamingo Code Review

OPENFRAME sentinel in queries.go omits the required openframe/docs/<doc>.md reference

OPENFRAME sentinel in queries.go omits the required openframe/docs/<doc>.md reference FLEETMDM-001 requires the opening sentinel in a shared upstream file to be of the form `// >>> OPENFRAME(<slug>): <why> — openframe/docs/<doc>.md`. The block opened at line 1764 of server/service/osquery_utils/queries.go (an upstream fleetdm/fleet file) carries a slug and a rationale but points at an upstream issue (`upstream fleetdm/fleet#59098`) instead of a fork doc under `openframe/docs/`. The same omission applies to the later blocks in this diff (lines ~1770, ~1797, ~1816, ~1892), which carry only `// >>> OPENFRAME(program-files-scan-duplicates)` with no why and no doc path. **What it risks:** Upstream sync tooling and future maintainers cannot find the fork rationale document for this divergence, so the logic may be dropped or silently re-conflicted on the next fleetdm/fleet merge. **How to fix:** Add the `openframe/docs/<doc>.md` reference to the opening sentinel (and create/point to that doc), keeping the upstream issue link as additional context.
Comment thread
aliaska-varieva marked this conversation as resolved.
Resolved
// Many programs entries have no install_location, so their executables are also matched by
// folder name: "C:\Program Files\7-Zip\7z.exe" belongs to "7-Zip 26.06 (x64 edition)".
knownNames := make(map[string]struct{})
// <<< OPENFRAME(program-files-scan-duplicates)
for _, row := range mainSoftwareResults {
if row["source"] != "programs" {
continue
}
// >>> OPENFRAME(program-files-scan-duplicates)
if name := programFolderName(row["name"]); name != "" {
knownNames[name] = struct{}{}
}
// <<< OPENFRAME(program-files-scan-duplicates)
p := normalizeWindowsDir(row["installed_path"])
if p == "" {
continue
Expand All @@ -1783,6 +1794,12 @@
continue
}

// >>> OPENFRAME(program-files-scan-duplicates)
if skipProgramFilesScanExe(exePath) {
continue
}
// <<< OPENFRAME(program-files-scan-duplicates)

// Extract and normalize the directory containing the exe.
exeDir := normalizeWindowsDir(windowsDirname(exePath))

Expand All @@ -1796,6 +1813,16 @@
break
}
}
// >>> OPENFRAME(program-files-scan-duplicates)
if !duplicate {
for _, dir := range programFilesScanDirs(exePath) {
if _, ok := knownNames[programFolderName(dir)]; ok {
duplicate = true
break
}
}
}
Comment thread
aliaska-varieva marked this conversation as resolved.
Resolved
// <<< OPENFRAME(program-files-scan-duplicates)
if duplicate {
continue
}
Expand Down Expand Up @@ -1862,6 +1889,99 @@
return row["file_version"]
}

// >>> OPENFRAME(program-files-scan-duplicates)
// programFilesWindowsComponentDirs are folders Windows itself installs under Program Files. Their
// executables are parts of the OS, not applications.
var programFilesWindowsComponentDirs = map[string]struct{}{
Comment thread
aliaska-varieva marked this conversation as resolved.
Resolved
"internet explorer": {},
"windows defender": {},
"windows defender advanced threat protection": {},
"windows mail": {},
"windows media player": {},
"windows multimedia platform": {},
"windows nt": {},
"windows photo viewer": {},
"windows portable devices": {},
"windows security": {},
"windows sidebar": {},
"windowspowershell": {},
}

// programFilesCommonFilesWindowsDirs are the Windows folders under Program Files\Common Files. The rest of
// Common Files holds third-party components, which are still reported.
var programFilesCommonFilesWindowsDirs = map[string]struct{}{
"microsoft shared": {},
"services": {},
"speechengines": {},
"system": {},
}

// skipProgramFilesScanExe reports whether a scanned executable is never an application of its own:
// uninstallers, and the Windows component folders except MsMpEng.exe, which stands for Windows Defender.
func skipProgramFilesScanExe(exePath string) bool {
path := strings.ToLower(exePath)
filename := path[strings.LastIndex(path, `\`)+1:]
if strings.HasPrefix(filename, "unins") {
return true
}
if filename == "msmpeng.exe" {
return false
}
dirs := programFilesScanDirs(path)
if len(dirs) == 0 {
return false
}
if dirs[0] == "common files" {
if len(dirs) < 2 {
return false
}
_, ok := programFilesCommonFilesWindowsDirs[dirs[1]]
return ok
}
_, ok := programFilesWindowsComponentDirs[dirs[0]]
return ok
}

// programFilesScanDirs returns the folders between Program Files and the executable, outermost first.
func programFilesScanDirs(exePath string) []string {
_, rel, ok := strings.Cut(strings.ToLower(exePath), `\program files\`)
if !ok {
return nil
}
parts := strings.Split(rel, `\`)
return parts[:len(parts)-1]
}

Check notice on line 1953 in server/service/osquery_utils/queries.go

View check run for this annotation

Flamingo / Flamingo Code Review

programFilesScanDirs ignores Program Files (x86), so the new skip/folder-name dedup never applies to 32-bit installs

programFilesScanDirs ignores Program Files (x86), so the new skip/folder-name dedup never applies to 32-bit installs programFilesScanDirs only cuts on the literal `\program files\`. Paths under `C:\Program Files (x86)\...` do not contain that substring (they contain `\program files (x86)\`), so the function returns nil for every 32-bit install. Both new behaviours added in this diff depend on it: skipProgramFilesScanExe (queries.go:1930) returns false for all x86 paths, and the knownNames folder-name dedup loop (queries.go:1818) iterates over an empty slice. The surrounding upstream code explicitly treats both roots as equivalent (skipInstallPaths lists `\program files\` and `\program files (x86)\` at lines 1760-1761), so the asymmetry is unintended: `C:\Program Files (x86)\Internet Explorer\iexplore.exe` and x86 uninstaller-adjacent Windows component exes are still reported, and x86 apps without install_location still produce duplicate inventory rows. **What it risks:** Hosts with 32-bit applications keep getting duplicated software entries and spurious Windows-component entries in inventory, the exact problem this change is meant to fix. **How to fix:** Try both Program Files roots when cutting the path prefix.
Comment thread
aliaska-varieva marked this conversation as resolved.
Resolved

// programFolderName reduces a program or folder name to the form both share: lowercase letters and
// digits, without trailing parenthesized parts or versions ("7-Zip 26.06 (x64 edition)" and "7-Zip"
// give "7zip"). Only dotted versions and years count as versions, so "Autodesk 3ds Max 2026" keeps
// "3ds" and "Microsoft 365 Apps" keeps "365".
func programFolderName(name string) string {
name = strings.TrimSpace(name)
for {
trimmed := strings.TrimSpace(programNameTrailingParenthesized.ReplaceAllString(name, ""))
trimmed = strings.TrimSpace(programNameTrailingVersion.ReplaceAllString(trimmed, ""))
if trimmed == name {
break
}
name = trimmed
}
var b strings.Builder
for _, r := range strings.ToLower(name) {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
b.WriteRune(r)
}
}
return b.String()
}

var (
programNameTrailingParenthesized = regexp.MustCompile(`\s*\([^()]*\)$`)
programNameTrailingVersion = regexp.MustCompile(`(?i)[\s._-]+v?(\d+(\.\d+)+|(19|20)\d{2})$`)
)

// <<< OPENFRAME(program-files-scan-duplicates)

// windowsDirname returns the directory portion of a Windows path (everything before the last backslash).
func windowsDirname(path string) string {
if idx := strings.LastIndex(path, `\`); idx >= 0 {
Expand Down
104 changes: 104 additions & 0 deletions server/service/osquery_utils/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3930,6 +3930,110 @@ func TestWindowsProgramFilesScan(t *testing.T) {
},
},
},
{
name: "exe matched by folder name when the program has no install_location",
mainResults: []map[string]string{
{"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""},
{"name": "Node.js", "source": "programs", "installed_path": ""},
{"name": "Git", "source": "programs", "installed_path": ""},
},
fileScanResults: []map[string]string{
{"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
{"path": `C:\Program Files\7-Zip\7zFM.exe`, "filename": "7zFM.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
{"path": `C:\Program Files\nodejs\node.exe`, "filename": "node.exe", "product_version": "26.13.0", "file_version": "26.13.0.0"},
{"path": `C:\Program Files\Git\cmd\git.exe`, "filename": "git.exe", "product_version": "2.43.0", "file_version": "2.43.0"},
},
expected: []map[string]string{
{"name": "7-Zip 26.06 (x64 edition)", "source": "programs", "installed_path": ""},
{"name": "Node.js", "source": "programs", "installed_path": ""},
{"name": "Git", "source": "programs", "installed_path": ""},
},
},
{
name: "folder name must match the whole program name",
mainResults: []map[string]string{
{"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""},
},
fileScanResults: []map[string]string{
{"path": `C:\Program Files\Adobe\DNG Converter\DNGConverter.exe`, "filename": "DNGConverter.exe", "product_version": "16.1", "file_version": "16.1.0.0"},
},
expected: []map[string]string{
{"name": "Adobe Acrobat (64-bit)", "source": "programs", "installed_path": ""},
{
"name": "DNGConverter.exe", "version": "16.1", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Adobe\DNG Converter`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
},
},
{
name: "only trailing versions, years and parenthesized parts are trimmed from program names",
mainResults: []map[string]string{
{"name": "7-Zip-26.06", "source": "programs", "installed_path": ""},
{"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""},
{"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""},
{"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""},
},
fileScanResults: []map[string]string{
{"path": `C:\Program Files\Acme Suite\viewer.exe`, "filename": "viewer.exe", "product_version": "3.0", "file_version": "3.0.0.0"},
{"path": `C:\Program Files\7-Zip\7z.exe`, "filename": "7z.exe", "product_version": "26.06", "file_version": "26.6.0.0"},
{"path": `C:\Program Files\Autodesk\AutoCAD 2026\acad.exe`, "filename": "acad.exe", "product_version": "25.1", "file_version": "25.1.0.0"},
{"path": `C:\Program Files\Microsoft\Tool\tool.exe`, "filename": "tool.exe", "product_version": "1.0", "file_version": "1.0"},
},
expected: []map[string]string{
{"name": "7-Zip-26.06", "source": "programs", "installed_path": ""},
{"name": "Autodesk 3ds Max 2026", "source": "programs", "installed_path": ""},
{"name": "Microsoft 365 Apps for enterprise - en-us", "source": "programs", "installed_path": ""},
{"name": "Acme (CAD) Suite", "source": "programs", "installed_path": ""},
{
"name": "viewer.exe", "version": "3.0", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Acme Suite`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
{
"name": "acad.exe", "version": "25.1", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Autodesk\AutoCAD 2026`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
{
"name": "tool.exe", "version": "1.0", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Microsoft\Tool`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
},
},
{
name: "Windows component folders and uninstallers are skipped, MsMpEng.exe is kept",
mainResults: []map[string]string{},
fileScanResults: []map[string]string{
{"path": `C:\Program Files\Windows Defender\MsMpEng.exe`, "filename": "MsMpEng.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"},
{"path": `C:\Program Files\Windows Defender\MpCmdRun.exe`, "filename": "MpCmdRun.exe", "product_version": "4.18.25030.2", "file_version": "4.18.25030.2"},
{"path": `C:\Program Files\Internet Explorer\iexplore.exe`, "filename": "iexplore.exe", "product_version": "11.0.26100.1", "file_version": "11.0.26100.1"},
{"path": `C:\Program Files\Windows Media Player\wmplayer.exe`, "filename": "wmplayer.exe", "product_version": "12.0.26100.1", "file_version": "12.0.26100.1"},
{"path": `C:\Program Files\Common Files\microsoft shared\helper.exe`, "filename": "helper.exe", "product_version": "1.0", "file_version": "1.0"},
{"path": `C:\Program Files\Common Files\Acme\updater.exe`, "filename": "updater.exe", "product_version": "3.1", "file_version": "3.1"},
{"path": `C:\Program Files\SomeApp\unins000.exe`, "filename": "unins000.exe", "product_version": "1.0", "file_version": "1.0"},
{"path": `C:\Program Files\SomeApp\Uninstall.exe`, "filename": "Uninstall.exe", "product_version": "1.0", "file_version": "1.0"},
},
expected: []map[string]string{
{
"name": "MsMpEng.exe", "version": "4.18.25030.2", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Windows Defender`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
{
"name": "updater.exe", "version": "3.1", "source": "programs",
"vendor": "", "installed_path": `C:\Program Files\Common Files\Acme`,
"extension_id": "", "extension_for": "", "upgrade_code": "",
"release": "", "arch": "", "bundle_identifier": "", "last_opened_at": "",
},
},
},
{
name: "non-programs entries in main do not affect dedup",
mainResults: []map[string]string{
Expand Down
Loading