Repository navigation
ftguess: report a path it cannot read instead of crashing on it - #901
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
python -m oletools.ftguess /nope.docdoes not survive a missing path -- the exceptionobject that
xglobyields in place of the file content is handed straight to the typeguesser:
The same tools in this repository already handle that contract:
olevba.py,olemeta.pyandolemap.pycheckisinstance(data, Exception)before touching thecontent (see also #900, which adds the same guard to
olemeta.py/olemap.py/oletimes.py),and
olevbaprints exactly one line:Change
and the caller counts the failures so a scan that could not read its input does not
report success:
process_file()keeps its(container, filename, data)signature, so any externalcaller keeps working; it now returns a bool instead of
None.ftguess /nope.docFileNotFoundErrortracebackFile : /nope.doc+ERROR: Given path '/nope.doc' does not exist!, exit 1ftguess real.docERRORline, exit 1Verified on the checked-out tree: patched
ftguess /nope.docprints the two lines andexits 1,
ftguess tests/test-data/msodde/harmless-clean.docstill reportsFile Type : MS Word 97 Documentand exits 0.