enh(backup): harden file type and name constraints on restore#3341
Conversation
There was a problem hiding this comment.
Pull request overview
Tightens backup restore behavior in motioneye.config.restore() by restricting extraction to regular files during tarball restore, aiming to reduce risk from non-file tar members.
Changes:
- Filter tar members using
TarInfo.isfile()before applying the allowed filename patterns. - Keep existing pattern-based allowlist for config-related filenames during restore.
4d7449a to
1bb928e
Compare
Once we support Python 3.12+ only, we can use `filter='data'` , which refuses to extract any kind of link, normalizes paths, skips paths pointing outside of the target dir, skips owner/mode, and more more. The `isfile()` check, however prevents extraction of any unintended file types, and the name pattern matching is hereby hardened to really only allow leading `./` segments, and no other combination of these characters, and no `/` anywhere else in the filename. This is needed since `fnmatchcase()` wildcard `*` matches `/` as well. Signed-off-by: MichaIng <micha@dietpi.com>
1bb928e to
47a40f5
Compare
Agent-Logs-Url: https://github.com/motioneye-project/motioneye/sessions/8b459da4-7afa-4856-9bcb-a90554666125 Co-authored-by: MichaIng <28480705+MichaIng@users.noreply.github.com>
Agent-Logs-Url: https://github.com/motioneye-project/motioneye/sessions/8b459da4-7afa-4856-9bcb-a90554666125 Co-authored-by: MichaIng <28480705+MichaIng@users.noreply.github.com>
|
@copilot
Hence the loop added to extract every file individually seems to not actually add anything to security, but adds quite a lot of unnecessary processing. If so, we better keep it as it was and use |
Agent-Logs-Url: https://github.com/motioneye-project/motioneye/sessions/ed04a67e-63f2-4eec-a3d9-19121ccda194 Co-authored-by: MichaIng <28480705+MichaIng@users.noreply.github.com>
Agreed, and adjusted in e02b162. I reverted the per-file extraction loop and switched back to |
Agent-Logs-Url: https://github.com/motioneye-project/motioneye/sessions/00a4e7c8-9dfa-4f02-9b39-e917cebeea6a Co-authored-by: MichaIng <28480705+MichaIng@users.noreply.github.com>
Agent-Logs-Url: https://github.com/motioneye-project/motioneye/sessions/4839fe3b-fc5f-47e0-9706-78656db1c956 Co-authored-by: MichaIng <28480705+MichaIng@users.noreply.github.com>
83481d2 to
4df84a0
Compare
We do not test the actual restored file or tarball member contents, hence skip this entirely to avoid confusion or false implications. Create and check multiple files and tarball members with positional arguments in a single function call, instead of multple calls, a list or dict. Move `assertIsNotNone(data)` from backup tests into the members assert function. The same for checking the restore result is not such a coding benefit, since its assert function does not currently take or need the JSON result. Instead, it is sufficient to check the expected result in a single separate test, and skip it entirely in all others, where it is about the restored files, not about the return value of the restore function. A test with `settings.ENABLE_REBOOT = True` => `{reboot: True}` could make sense though.
Signed-off-by: MichaIng <micha@dietpi.com>
4df84a0 to
437aaeb
Compare
Once we support Python 3.12+ only, we can use
filter='data'instead, or additionally, which refuses to extract any kind of link, normalizes paths, skips owner/mode, and more more. With our hardcoded strict filename pattern and theisfile()check, however, all security concerns are addressed.