Skip to content

ftguess: report a path it cannot read instead of crashing on it - #901

Open
qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:fix/ftguess-missing-path-message
Open

qianeric-backup wants to merge 1 commit into
decalage2:masterfrom
qianeric-backup:fix/ftguess-missing-path-message

Conversation

@qianeric-backup

Copy link
Copy Markdown

python -m oletools.ftguess /nope.doc does not survive a missing path -- the exception
object that xglob yields in place of the file content is handed straight to the type
guesser:

$ ftguess /nope.doc
File       : /nope.doc
Traceback (most recent call last):
  File ".../oletools/ftguess.py", line 918, in process_file
    ftg = ftype_guess(filepath=filename, data=data)
  ...
  File ".../oletools/ftguess.py", line 155, in guess_ftype
    raise FileNotFoundError('File not found')
FileNotFoundError: File not found

The same tools in this repository already handle that contract: olevba.py,
olemeta.py and olemap.py check isinstance(data, Exception) before touching the
content (see also #900, which adds the same guard to olemeta.py/olemap.py/oletimes.py),
and olevba prints exactly one line:

$ olevba /nope.doc
ERROR: Given path '/nope.doc' does not exist!

Change

def process_file(container, filename, data):
    print('File       : %s' % filename)
    # xglob reports a file it could not read by yielding the exception instance
    # instead of the file content, so it must not be handed over to the guesser
    if isinstance(data, Exception):
        if isinstance(data, PathNotFoundException):
            print('ERROR: Given path %r does not exist!' % filename, file=sys.stderr)
        else:
            print('ERROR: Exception opening/reading %r: %s' % (filename, data),
                  file=sys.stderr)
        return False
    ftg = ftype_guess(filepath=filename, data=data)
    ...
    return True

and the caller counts the failures so a scan that could not read its input does not
report success:

    error_count = 0
    for container, filename, data in xglob.iter_files(...):
        if container and filename.endswith('/'):
            continue
        if not process_file(container, filename, data):
            error_count += 1

    if error_count:
        sys.exit(1)

process_file() keeps its (container, filename, data) signature, so any external
caller keeps working; it now returns a bool instead of None.

invocation before after
ftguess /nope.doc FileNotFoundError traceback File : /nope.doc + ERROR: Given path '/nope.doc' does not exist!, exit 1
ftguess real.doc one report block unchanged, exit 0
mixed good + missing traceback, no report for the rest report for the good file, one ERROR line, exit 1

Verified on the checked-out tree: patched ftguess /nope.doc prints the two lines and
exits 1, ftguess tests/test-data/msodde/harmless-clean.doc still reports
File Type : MS Word 97 Document and exits 0.

`python -m oletools.ftguess /nope.doc` does not survive a missing path -- the exception
object that `xglob` yields in place of the file content is handed straight to the type
guesser:

```
$ ftguess /nope.doc
File       : /nope.doc
Traceback (most recent call last):
  File ".../oletools/ftguess.py", line 918, in process_file
    ftg = ftype_guess(filepath=filename, data=data)
  ...
  File ".../oletools/ftguess.py", line 155, in guess_ftype
    raise FileNotFoundError('File not found')
FileNotFoundError: File not found
```

The same tools in this repository already handle that contract: `olevba.py`,
`olemeta.py` and `olemap.py` check `isinstance(data, Exception)` before touching the
content (see also decalage2#900, which adds the same guard to `olemeta.py`/`olemap.py`/`oletimes.py`),
and `olevba` prints exactly one line:

```
$ olevba /nope.doc
ERROR: Given path '/nope.doc' does not exist!
```

## Change

```python
def process_file(container, filename, data):
    print('File       : %s' % filename)
    # xglob reports a file it could not read by yielding the exception instance
    # instead of the file content, so it must not be handed over to the guesser
    if isinstance(data, Exception):
        if isinstance(data, PathNotFoundException):
            print('ERROR: Given path %r does not exist!' % filename, file=sys.stderr)
        else:
            print('ERROR: Exception opening/reading %r: %s' % (filename, data),
                  file=sys.stderr)
        return False
    ftg = ftype_guess(filepath=filename, data=data)
    ...
    return True
```

and the caller counts the failures so a scan that could not read its input does not
report success:

```python
    error_count = 0
    for container, filename, data in xglob.iter_files(...):
        if container and filename.endswith('/'):
            continue
        if not process_file(container, filename, data):
            error_count += 1

    if error_count:
        sys.exit(1)
```

`process_file()` keeps its `(container, filename, data)` signature, so any external
caller keeps working; it now returns a bool instead of `None`.

| invocation | before | after |
|---|---|---|
| `ftguess /nope.doc` | `FileNotFoundError` traceback | `File       : /nope.doc` + `ERROR: Given path '/nope.doc' does not exist!`, exit 1 |
| `ftguess real.doc` | one report block | unchanged, exit 0 |
| mixed good + missing | traceback, no report for the rest | report for the good file, one `ERROR` line, exit 1 |

Verified on the checked-out tree: patched `ftguess /nope.doc` prints the two lines and
exits 1, `ftguess tests/test-data/msodde/harmless-clean.doc` still reports
`File Type  : MS Word 97 Document` and exits 0.
@decalage2
decalage2 self-requested a review November 25, 2026 19:53
@decalage2 decalage2 self-assigned this Nov 25, 2026
@decalage2 decalage2 added this to the oletools 0.60 milestone Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants