Skip to content

fix(octop): add is_dir + OSError guard for agently_cli attachment folder iterdir - #2666

Closed
TrueFurina wants to merge 1 commit into
TencentCloud:mainfrom
TrueFurina:r77-agently-iterdir-guard
Closed

TrueFurina wants to merge 1 commit into
TencentCloud:mainfrom
TrueFurina:r77-agently-iterdir-guard

Conversation

@TrueFurina

Copy link
Copy Markdown

Summary

Add is_dir() gate + ry/except OSError around older.iterdir() in �gently_cli.py attachment handler. One file, one commit.

Problem

_finalize_attachment() does list(folder.iterdir()) with zero protection. The function has an outer except Exception: that re-raises and cleans up, but the unhandled exception type (FileNotFoundError, NotADirectoryError, PermissionError) provides no semantic hint about what went wrong — and on Windows, PermissionError could originate from ACL restrictions or antivirus locks even after older was successfully created moments earlier.

The older path is a temp directory unpacked from a user-supplied attachment archive. Failure modes:

  1. Archive extraction fails → older doesn't exist → FileNotFoundError.
  2. Archive extraction produces a single-file non-directory output → NotADirectoryError.
  3. Windows ACL/antivirus lock blocks read → PermissionError.

Fix

Add explicit if not folder.is_dir(): gate plus ry/except OSError around iterdir(), both raising the existing ValueError(tr('connector.agently.invalid_attachment')). This lets the outer except Exception: propagate a semantically meaningful error (and run shutil.rmtree(folder) cleanup) regardless of which filesystem error occurred.

Pattern

This fell through the R71-R72 iterdir sweep because the existing outer except Exception: masked the problem from grep-based searches. The _purge_expired_confirmations() sibling in the same file already uses is_dir() gate before older.iterdir() — this makes the two consistent.

…der iterdir

The attachment-file handler in _finalize_attachment() calls list(folder.iterdir()) with zero protection - neither an is_dir() gate nor OSError handling. The folder is a temp unpack directory created from a user-supplied archive; if extraction fails (corrupt archive, partial write, concurrent cleanup), folder may not exist or not be a directory, causing FileNotFoundError or NotADirectoryError. On Windows, ACL restrictions or antivirus locks could also raise PermissionError during iterdir. Add both an is_dir gate and a try/except OSError, raising the existing ValueError on failure so the outer except Exception branch runs its cleanup (shutil.rmtree(folder)).
@jubaoliang jubaoliang closed this Oct 31, 2026
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.

2 participants