1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 17:55:08 +00:00
Commit Graph

191 Commits

Author SHA1 Message Date
Tw93
9db5488397 security: validate raw path in moveToTrash before filepath.Abs resolves traversal
filepath.Abs resolves ".." components, so the existing validatePath call
on the resolved path could never catch traversal attempts. Move validation
before Abs to reject raw input with "..", keeping the post-Abs check as
defense-in-depth.
2026-03-14 10:39:33 +08:00
Tw93
169be1e1f2 fix(timeout): inherit helper state and pass checks 2026-03-14 08:32:11 +08:00
Tw93
0d2f217f28 security: add regression tests for validatePath with special chars
- Add TestValidatePath covering Chinese, emoji, and special characters
- Add TestValidatePathWithChineseAndSpecialChars for filesystem tests
- Fix validatePath to detect .. components without rejecting valid paths

Ensures paths with $, ;, :, emoji, Chinese chars are not rejected
while still blocking path traversal attempts.
2026-03-14 08:26:45 +08:00
Tw93
951e395ab7 security: fix CodeQL command injection and path traversal alerts
- Add validatePath() helper to check path safety before external commands
- Validate paths in delete.go (moveToTrash), scanner.go (mdfind, du),
  and main.go (open command)
- Remove overly restrictive character whitelist that rejected valid
  macOS paths (Chinese, emoji, $, ;, etc.)
- Unify path validation logic across all three files

Fixes CodeQL alerts:
- Command injection in osascript (delete.go)
- Command injection in mdfind/du (scanner.go)
- Path traversal in open command (main.go)
2026-03-14 08:24:08 +08:00
Tw93
f6acfa774c feat(disk): enhance APFS disk usage reporting with Finder integration 2026-03-14 07:48:16 +08:00
Tw93
cb2eb1097a fix(analyze): add darwin build tag to all platform-specific files
All files in cmd/analyze/ are macOS-only but several were missing the
//go:build darwin constraint. On Linux (e.g. CodeQL CI), Go compiled
these files without the types defined in main.go (which had the tag),
causing undefined symbol errors for dirEntry, fileEntry, scanResult,
cacheEntry, historyEntry and model.

- Add //go:build darwin to heap.go, cache.go, scanner.go, cleanable.go,
  delete.go, format.go, constants.go and all *_test.go files

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-14 07:46:32 +08:00
Dylan Joss
7350547458 feat: add JSON output tests and README docs for analyze and status (#556)
* feat: add JSON output tests and README docs for analyze and status

Add 7 BATS tests covering `--json` output for `mo analyze` and `mo status`:
- schema structure
- field types
- pipe auto-detection.

Also document the `--json` flag in a new "Machine-Readable Output" README section, including the auto-detection behavior when piped.

* chore: use waitgroup go in status collector

---------

Co-authored-by: Tw93 <hitw93@gmail.com>
2026-03-13 11:40:41 +08:00
Tw93
a34cdee809 chore: auto format code 2026-03-09 16:24:43 +00:00
tw93
8c53923ce8 fix(status): improve disk card display refs #551 2026-03-08 23:46:46 +08:00
tw93
89a9ae0ce2 fix(analyze): count top-level files in json output 2026-03-07 10:10:41 +08:00
Oleksandr Redko
603df79a01 refactor: fix modernize lint issues (#539) 2026-03-05 20:46:12 +08:00
tw93
8e4b8a5e0d Improve update checks and cleanup UX, add timeout regressions 2026-03-05 12:00:07 +08:00
tw93
ff69504f89 fix: harden CI test stability and status collector resilience 2026-03-04 16:49:20 +08:00
Noah Qin
c88691c2c8 feat: add --json flag to analyze command for non-TTY environments (#533)
* feat: add --json flag to analyze command

* feat: implement JSON output mode for analyze

* refactor: rename jsonOutput flag to jsonMode to avoid conflict
2026-03-04 16:22:09 +08:00
Noah Qin
fbaf5e7c91 fix(status): enable network data in JSON mode (#532)
Network rate calculation requires two samples to compute the delta.
In JSON mode, the collector was only called once, causing the network
field to always return nil.

This change adds a second collection call with a 1-second interval,
allowing the network rates to be calculated properly.
2026-03-04 02:59:01 +08:00
Noah Qin
2a4eaf007b feat(status): add --json flag for programmatic access (#529)
Add --json command-line flag to mo status that outputs system metrics
in JSON format without requiring a TTY environment.

This enables:
- Integration with GUI applications (e.g., native macOS apps)
- Use in automation scripts and monitoring systems
- Piping output to tools like jq for data extraction
- Recording metrics for analysis

Implementation:
- Add JSON struct tags to all metric types
- Add --json flag using Go's flag package
- Implement runJSONMode() for one-time metric collection
- Refactor main() to support both TUI and JSON modes
- Maintain 100% backward compatibility (default TUI unchanged)

Testing:
- All 454 existing tests pass
- JSON output validated with jq and python json.tool
- Pipeline and redirection work correctly
- No breaking changes to existing functionality
2026-03-03 16:05:55 +08:00
tw93
1be71edc9d fix: use Base-10 sizes and mdls logical size to match macOS Finder
- Switch bytes_to_human (shell) and humanizeBytes (Go) from Base-2
  (1024) to Base-10 (1000) to match Apple's storage calculation
  standard since Snow Leopard
- Add proper decimal rounding instead of truncation
- Use mdls kMDItemLogicalSize for .app bundles to avoid APFS clone
  file undercounting by du

Fixes #511
2026-02-28 10:02:34 +08:00
tw93
bbe3d3f284 fix(status): keep compact header single-line on tight widths 2026-02-27 10:07:44 +08:00
tw93
f4118dc883 fix(status): hide os and uptime in compact header 2026-02-27 10:02:47 +08:00
tw93
15f698c606 fix(status): hide swap size text when card is narrow 2026-02-27 10:02:06 +08:00
tw93
8436377922 test(status): add narrow-width rendering coverage 2026-02-27 09:53:24 +08:00
tw93
369d3b8840 fix(status): wrap header and card output on narrow terminals 2026-02-27 09:53:21 +08:00
tw93
9056ce5b38 fix(status): adapt single-column width calculation 2026-02-27 09:53:18 +08:00
tw93
7648bd9d12 fix(status): prevent duplicate mole rendering in error state
- renderHeader now returns empty string as second return value when
  error message is present, avoiding duplicate mole in main.go View
- Add unit tests to prevent regression

Fixes P2 issue: view.go (line 184) + main.go (line 160)
2026-02-21 20:21:45 +08:00
tw93
9de661b5df fix(status): prefer internal disks over external in disk listing
When multiple disks are connected, the status command was sorting
only by size, causing external disks to appear first when they are
larger than the internal disk. This resulted in showing incorrect
free space (external disk size) instead of the internal disk.

The sort now prioritizes internal disks before sorting by size,
ensuring the internal disk always appears first.

Fixes #466
2026-02-16 19:08:25 +08:00
tw93
8e8059b0aa fix(status): resolve layout issue when stretching terminal window (#467)
When the terminal is stretched wide, the header info line may wrap to
multiple lines but the mole position was calculated independently based
on terminal width, causing vertical misalignment.

Separate header and mole rendering so mole always appears on dedicated
lines below the header regardless of terminal width.
2026-02-16 19:07:42 +08:00
tw93
878dfa1bde refactor(analyze): remove unused volume mount helper 2026-02-10 16:33:10 +08:00
tw93
1a559f8563 optimize analyze scanner concurrency and channel send semantics 2026-02-10 16:23:24 +08:00
tw93
b605dde450 refactor: simplify analyze volumes behavior 2026-02-10 15:32:13 +08:00
Andrei Murariu
37bdc6edf7 feat: Exclude External Disks from Analyze (#428)
Created optional flag `--exclude-volumes` for `mo analyze` to consent the users to analyze the filesystem without taking in consideration attached Volumes.
Normal behaviour (with volumes included) is left untouched.
2026-02-10 15:28:33 +08:00
tw93
e65b73eb64 Merge branch 'main' into dev 2026-02-10 14:27:26 +08:00
Oleksandr Redko
599b39a3fb refactor: use builtin max instead of maxInt (#430) 2026-02-10 14:27:00 +08:00
tw93
e6829b9a5d fix: improve cache freshness fallback and proxy detection 2026-02-07 17:48:55 +08:00
tw93
95b3818da8 fix(analyze): fix scan deadlock with non-blocking fallback and add regression test (#419) 2026-02-07 11:01:00 +08:00
tw93
a0d5b476d3 fix(analyze): skip virtualization/container mounts to prevent NFS hangs
- Add OrbStack, Colima, Parallels, VMware Fusion, VirtualBox, Rancher Desktop to skip list
- Prevent infinite NFS timeout loops when scanning VM/container mount points
- Fixes #416
2026-02-05 11:40:00 +08:00
tw93
0fcf777230 refactor: simplify channel send logic with trySend function 2026-01-31 20:05:21 +08:00
tw93
e81be16031 perf: optimize scanner timer usage and app protection matching
- Replace time.After() with reusable timer to reduce GC pressure
- Use pre-compiled regex for app bundle matching (O(1) vs O(N))
- Fix Bash 3.2 compatibility (remove local -n usage)
2026-01-30 15:37:13 +08:00
tw93
89dcb0c3b5 fix: Use du -P for accurate size calculation and add timeouts to channel sends to prevent blocking. 2026-01-30 15:06:30 +08:00
tw93
87f605e9a8 test: add tests for cmd/status and conclude merge 2026-01-30 10:59:09 +08:00
Dylan Joss
b8e15b1c16 test: add tests for parsing and filtering functions (#385)
Add unit tests for parsing functions in `cmd/status/view_test.go`:
- `parseInt`: parses integers from strings with padding/decimals
- `parseRefreshRate`: parses display refresh rates from system output
- `isNoiseInterface`: filters noise network interfaces
- `parsePMSet`: parses macOS pmset battery output

Coverage for `cmd/status` improved from 8.4% to 12.6%.
2026-01-29 11:40:00 +08:00
tw93
64f79a59d8 feat: optimize log system and add mo log command
- Add get_timestamp() helper and optimize log rotation
- Create mo log viewer with search/filter capabilities
- Improve test coverage to 18.4% with better assertions
- Add security fixes for grep injection prevention
2026-01-28 19:46:01 +08:00
Dylan Joss
f815a5f28c test: add tests for byte formatters and disk helpers (#382)
Add unit tests for additional utility functions in `cmd/status/view_test.go`:
- `humanBytes`: byte formatting with decimals and units
- `humanBytesCompact`: compact byte formatting
- `splitDisks`: separates internal/external disks
- `diskLabel`: generates numbered disk labels

Coverage for cmd/status improved from 6.9% to 8.4%.
2026-01-28 19:12:04 +08:00
tw93
ab4433d9c0 Merge branch 'main' of https://github.com/tw93/Mole 2026-01-28 11:43:41 +08:00
tw93
61a3238f19 fix(scan): prevent hang on cyclic symlinks (#378, #379) 2026-01-28 11:43:39 +08:00
Tw93
6036b4606c chore: auto format code 2026-01-28 03:26:51 +00:00
Dylan Joss
a9c5794ec4 test: add tests for utility functions and heap implementations (#380)
Add unit tests for utility functions in cmd/status/view_test.go:
- formatRate: MB/s rate formatting with adaptive precision
- shorten: string truncation with ellipsis
- humanBytesShort: byte formatting with binary units

Add unit tests for heap implementations in cmd/analyze/heap_test.go:
- entryHeap: min-heap for dirEntry (basic ops, empty, single element)
- largeFileHeap: min-heap for fileEntry (basic ops, top N pattern)
2026-01-28 11:25:08 +08:00
tw93
86c9005381 Merge branch 'dwjoss/cleanable-tests' of https://github.com/dwjoss/Mole; branch 'main' of https://github.com/tw93/Mole 2026-01-27 10:30:43 +08:00
Dylan Joss
162ba949ee test: expand cleanable directory detection tests
Enhance test coverage for `isCleanableDir` and add new tests for
`isHandledByMoClean` in `cmd/analyze/analyze_test.go`.

`TestIsHandledByMoClean` (new, 12 test cases):
- Paths `mo clean` handles (caches, logs, saved state, trash, reports)
- Paths `mo clean` does NOT handle (project dirs, home, random)
- Case sensitivity verification

`TestIsCleanableDir` (expanded from 3 to 24 test cases):
- Project dependencies (`node_modules`, `venv`, `pycache`, `build`, etc.)
- Xcode/iOS directories (DerivedData, Pods)
- Build outputs (dist, target, .next, coverage)
- Paths excluded because `mo clean` handles them
- Edge cases (empty string, root path, just basename)
2026-01-26 14:05:02 -08:00
tw93
e966838c82 style: standardize punctuation across codebase
- Replace parentheses with commas for supplementary info
- Use commas instead of em-dashes for separators
- Update bullet points from - to * in some contexts
- Improve version extraction regex with fallback logic
2026-01-26 14:36:06 +08:00
Tw93
83166f2fe8 fix: avoid orphan service false positives and warm large file threshold 2026-01-23 20:16:06 +08:00