Skip to content

Text is the fallback for anything unrecognised, so unreadable files render as nonsense #728

Description

@andiwand

DecodedFile::decode falls back to FileType::text_file for anything it does not
recognise, and translate_html is true for it. So a caller that asks the core what a
file is, and trusts the answer, ends up showing a page of nonsense instead of saying
the file cannot be opened.

Measured with 6.10.1 (the Apple xcframework), passing each file through decode,
html::translate and the http server:

file file_type as_text_file().charset() translate GET page
64 random bytes, named .odt text_file null ok 200, 27 KB
a sqlite database text_file null ok 200, 27 KB
an empty file text_file null ok 200, 27 KB
a real text file text_file UTF-8 ok 200, 27 KB

Nothing throws and nothing in the answer distinguishes the first three from the last,
except the charset.

Both readers work around it the same way, by asking for the charset themselves and
treating a null as unsupported — OpenDocument.droid in CoreLoader.kt:

if (file.isTextFile && file.asTextFile().charset() == null) {
    throw OdrException.UnsupportedFileType("no charset could be detected: $inputPath")
}

OpenDocument.ios has just copied that check. It would be better made once, here.

Proposal: do not report a file as text when no charset can be detected. Either
decode fails and list_file_types leaves text_file out, or open/translate
throws for it. Whichever fits the design — the point is that the core's own answer
should say the file is not readable, rather than every caller re-deriving it from the
charset.

One note against the charset alone as the rule: an empty file also has no charset, and
"empty text file" may be worth treating as text rather than as unreadable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions