Commit Graph

129 Commits

Author SHA1 Message Date
Thomas Waldmann 54a85bf56d
format_timedelta: use 3 decimal digits (ms)
maybe this fixes the frequently failing test.
also, giving ms makes more sense than 10ms granularity.
2024-04-04 12:45:28 +02:00
Thomas Waldmann c704e5ea9e
new warnings infrastructure to support modern exit codes
- implement updating exit code based on severity, including modern codes
- extend print_warning with kwargs wc (warning code) and wt (warning type)
- update a global warnings_list with warning_info elements
- create a class hierarchy below BorgWarning class similar to Error class
- diff: change harmless warnings about speed to rc == 0
- delete --force --force: change harmless warnings to rc == 0

Also:

- have BackupRaceConditionError as a more precise subclass of BackupError
2024-02-14 01:26:12 +01:00
Thomas Waldmann 12e224613e
test_is_slow_msgpack: skip test on expected slow msgpack environments 2023-09-14 14:47:30 +02:00
Thomas Waldmann 836b517451
get_*_dir tests: use create=False
In these tests, we only compare paths, but we do not
need to create these paths for that. By not trying to
create them, we can avoid permission issues, e.g. under
fakeroot.
2023-08-27 03:23:50 +02:00
bigtedde a09c8ad940 reverted import grouping 2023-07-29 09:40:24 -07:00
bigtedde ffebd8b044 parametrize helper.py 2023-07-28 12:30:27 -07:00
bigtedde 67334c4e83 removed BaseTestCase from helpers.py 2023-07-26 14:50:52 -07:00
Thomas Waldmann 5013121bd8
fix E501 2023-07-26 01:24:20 +02:00
Thomas Waldmann 35ac39b751
fix F401 2023-07-26 01:23:37 +02:00
TW 4dfce1cca4
Merge pull request #7719 from bket/fix_test
Fix failing test on OpenBSD
2023-07-08 02:20:07 +02:00
Björn Ketelaars 1074089d37 Fix failing test on OpenBSD
A borgbackup-2.0.0b6 test fails on OpenBSD with the message below.

```
=================================== FAILURES ===================================
_____________________________ test_get_runtime_dir _____________________________

path = '/run/user/55/borg', mode = 511, pretty_deadly = True

    def ensure_dir(path, mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO, pretty_deadly=True):
        """
        Ensures that the dir exists with the right permissions.
        1) Make sure the directory exists in a race-free operation
        2) If mode is not None and the directory has been created, give the right
        permissions to the leaf directory. The current umask value is masked out first.
        3) If pretty_deadly is True, catch exceptions, reraise them with a pretty
        message.
        Returns if the directory has been created and has the right permissions,
        An exception otherwise. If a deadly exception happened it is reraised.
        """
        try:
>           os.makedirs(path, mode=mode, exist_ok=True)

build/lib.openbsd-7.3-amd64-cpython-310/borg/helpers/fs.py:37:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
```

If `$XDG_RUNTIME_DIR` is not set `platformdirs.user_runtime_dir()`
returns one of 3 different paths
(https://github.com/platformdirs/platformdirs/pull/201). Proposed fix is
to check if `get_runtime_dir()` returns one of these paths.
2023-07-07 15:38:13 +02:00
Felix Schwarz ae0b3d2fff replace "datetime.utcfromtimestamp" with custom helper to avoid deprecation warnings when using Python 3.12 2023-07-06 21:46:19 +02:00
Peter Gerber 438cf2e7ef
Sanitize paths during archive creation/extraction/...
Paths are not always sanitized when creating an archive and,
more importantly, never when extracting one. The following example
shows how this can be used to attempt to write a file outside the
extraction directory:

$ echo abcdef | borg create -r ~/borg/a --stdin-name x/../../../../../etc/shadow archive-1 -
$ borg list -r ~/borg/a archive-1
-rw-rw---- root   root          7 Sun, 2022-10-23 19:14:27  x/../../../../../etc/shadow
$ mkdir borg/target
$ cd borg/target
$ borg extract -r ~/borg/a archive-1
x/../../../../../etc/shadow: makedirs: [Errno 13] Permission denied: '/home/user/borg/target/x/../../../../../etc'

Note that Borg tries to extract the file to /etc/shadow and the
permission error is a result of the user not having access.

This patch ensures file names are sanitized before archiving.
As for files extracted from the archive, paths are sanitized
by making all paths relative, removing '.' elements, and removing
superfluous slashes (as in '//'). '..' elements, however, are
rejected outright. The reasoning here is that it is easy to start
a path with './' or insert a '//' by accident (e.g. via --stdin-name
or import-tar). '..', however, seem unlikely to be the result
of an accident and could indicate a tampered repository.

With paths being sanitized as they are being read, this "errors"
will be corrected during the `borg transfer` required when upgrading
to Borg 2. Hence, the sanitation, when reading the archive,
can be removed once support for reading v1 repositories is dropped.
V2 repository will not contain non-sanitized paths. Of course,
a check for absolute paths and '..' elements needs to kept in
place to detect tempered archives.

I recommend treating this as a security issue. I see the following
cases where extracting a file outside the extraction path could
constitute a security risk:

a) When extraction is done as a different user than archive
creation. The user that created the archive may be able to
get a file overwritten as a different user.
b) When the archive is created on one host and extracted on
another. The user that created the archive may be able to
get a file overwritten on another host.
c) When an archive is created and extracted after a OS reinstall.
When a host is suspected compromised, it is common to reinstall
(or set up a new machine), extract the backups and then evaluate
their integrity. A user that manipulates the archive before such
a reinstall may be able to get a file overwritten outside the
extraction path and may evade integrity checks.

Notably absent is the creation and extraction on the same host as
the same user. In such case, an adversary must be assumed to be able
to replace any file directly.

This also (partially) fixes #7099.
2023-06-07 23:23:53 +02:00
Thomas Waldmann ffc59dd071
implement unix domain (ipc) socket support
server (listening) side:
borg serve --socket  # default location
borg serve --socket=/path/to/socket

client side:
borg -r socket:///path/to/repo create ...
borg --socket=/path/to/socket -r socket:///path/to/repo ...

served connections:
- for ssh: proto: one connection
- for socket: proto: many connections (one after the other)

The socket has user and group permissions (770).

skip socket tests on win32, they hang infinitely, until
github CI terminates them after 60 minutes.

socket tests: use unique socket name

don't use the standard / default socket name, otherwise tests
running in parallel would interfere with each other by using
the same socket / the same borg serve process.

write a .pid file, clean up .pid and .sock file at exit

add stderr print for accepted/finished socket connection
2023-06-06 21:12:54 +02:00
Thomas Waldmann 7878a34fd4
add get_runtime_dir / BORG_RUNTIME_DIR, tests, docs 2023-06-06 11:58:48 +02:00
Thomas Waldmann c3a4568870
channel progress output via logging system
- simplify progress output (no \r, no terminal size related tweaks)
- emit progress output via the logging system (so it does not use stderr
  of borg serve)
- progress code always logs a json string, the json has all needed
  to either do json log output or plain text log output.
- use formatters to generate plain or json output from that.
- clean up setup_logging
- use a StderrHandler that always uses the **current** sys.stderr
- tweak TestPassphrase to not accidentally trigger just because of seeing 12 in output
2023-05-29 22:40:47 +02:00
Thomas Waldmann 0be545dc45
remove ProgressIndicatorEndless (not used) 2023-05-29 22:40:45 +02:00
Thomas Waldmann b8d49a0274
put security infos into data dir, fixes #5760 2023-05-18 12:48:45 +02:00
Thomas Waldmann 6d38530ff1
fix ChunkerParams validator and tests 2023-04-12 01:15:46 +02:00
Thomas Waldmann 3dd14f4855
ProgressIndicatorPercent: fix space computation for wide chars, fixes #3027
needs to use swidth() in case there are wide chars (like CJK)
in the left part of the msg (e.g. an archive name).
2023-04-02 21:57:02 +02:00
nain-F49FF806 dfef2b9092
fix config/cache dir compatibility issues, fixes #7445 (#7448)
fix config dir compatibility issue, fixes #7445

- add tests
- make sure the result of get_cache_dir matches pre and post #7300 where desired
- harmonize implementation of config_dir_compat and cache_dir_compat tests

Co-authored-by: nain <126972030+F49FF806@users.noreply.github.com>
2023-03-30 00:24:42 +02:00
Thomas Waldmann 516c070c7c
fix tests for platformdirs 3.x.x 2023-02-07 21:14:16 +01:00
Thomas Waldmann d9875a4d7e
improve test_get_security_dir 2023-02-04 18:23:35 +01:00
Thomas Waldmann 078bb7ee02
improve test_get_keys_dir 2023-02-04 18:19:15 +01:00
Thomas Waldmann 6ab424598d
improve test_get_cache_dir 2023-02-04 18:17:43 +01:00
Thomas Waldmann 4bacd0b722
improve test_get_config_dir 2023-02-04 18:15:57 +01:00
Thomas Waldmann bb73e8682f
fix win2 typo 2023-02-04 18:11:28 +01:00
Thomas Waldmann 499e5133b5
test_config_dir_compat: don't test on win32
there is no old borg < 2.0 there anyway.
2023-02-04 01:18:16 +01:00
Thomas Waldmann bde0f11e1e
get_security_dir: legacy=False default 2023-02-04 00:35:04 +01:00
Thomas Waldmann 1d4810af23
get_keys_dir: legacy=False default 2023-02-04 00:35:03 +01:00
Thomas Waldmann b06dd1c66c
get_config_dir: legacy=False default 2023-02-04 00:35:01 +01:00
Thomas Waldmann ebcda6f0a5
get_cache_dir: legacy=False default 2023-02-04 00:34:57 +01:00
Thomas Waldmann 424be763b2
get_base_dir: legacy=False default 2023-02-04 00:34:54 +01:00
Thomas Waldmann 81595a9ca0
conftest.py: use BORG_BASE_DIR to redirect borg testing .config/.cache into a temp dir
XDG_*_HOME is not honoured on macOS and on Windows if we use platformdirs.
2023-02-04 00:34:52 +01:00
Thomas Waldmann 437b5fa859
do not fail on macOS due to different dirs from platformdirs 2023-02-04 00:34:42 +01:00
Thomas Waldmann 8379ecefaa
deal with BORG_BASE_DIR 2023-02-04 00:34:26 +01:00
Thomas Waldmann d3d909ad31
add compatibility test: legacy vs. non-legacy platformdirs 2023-02-03 18:22:51 +01:00
Rayyan Ansari f2452aef2a
helpers: use platformdirs on win32 2023-02-03 17:46:49 +01:00
Thomas Waldmann 0e577452c2
tests: fix prune_split tests by giving the timestamps in local timezone
Guess this fixes issues like #5535.

I tested this with TZ=:
- UTC
- UTC+12
- UTC-12
- UTC+14
- UTC-14
2023-01-19 00:52:27 +01:00
Thomas Waldmann 32d430a1b0
implement text_to_json / binary_to_json, see #6151
binary bytes:
- json_key = <key>_b64
- json_value == base64(value)

text (potentially with surrogate escapes):
- json_key1 = <key>
- json_value1 = value_text (s-e replaced by ?)
- json_key2 = <key>_b64
- json_value2 = base64(value_binary)

json_key2/_value2 is only present if value_text required
replacement of surrogate escapes (and thus does not represent
the original value, but just an approximation).
value_binary then gives the original bytes value (e.g. a
non-utf8 bytes sequence).
2023-01-16 17:45:27 +01:00
Thomas Waldmann 38152c8e5d
safe_unlink tests: use os.link to support win32 also
also:
- add note about mklinkto
- skip tests based on "not are_hardlinks_supported()" if needed
2023-01-13 20:59:30 +01:00
Thomas Waldmann a475227e18
use archivename_validator everywhere
also: simplify, reuse code from text_validator.
2022-12-15 22:54:46 +01:00
Thomas Waldmann 99c6afbc61
add generic text and comment validator 2022-12-12 18:01:07 +01:00
Thomas Waldmann fe2b2bc007
archive names: validate more strictly, fixes #2290
we want to be able to use an archive name as a directory name,
e.g. for the FUSE fs built by borg mount.

thus we can not allow "/" in an archive name on linux.

on windows, the rules are more restrictive, disallowing
quite some more characters (':<>"|*?' plus some more).
we do not have FUSE fs / borg mount on windows yet, but
we better avoid any issues.
we can not avoid ":" though, as our {now} placeholder
generates ISO-8601 timestamps, including ":" chars.

also, we do not want to have leading/trailing blanks in
archive names, neither surrogate-escapes.

control chars are disallowed also, including chr(0).
we have python str here, thus chr(0) is not expected in there
(is not used to terminate a string, like it is in C).
2022-12-12 17:05:01 +01:00
Thomas Waldmann f088682bc9
ignore testsuite test for slow msgpack on cygwin
we don't want to have a failing test just because a
not compiled msgpack was used to run the tests.
2022-11-22 18:16:12 +01:00
Thomas Waldmann d3a2d831b7 refactor replace_placeholders, fixes #6966
fix replacing placeholders in archive name, --comment and --glob-archives values (even if overridden by other options like
--timestamp).

add test.
2022-09-09 23:28:34 +02:00
Thomas Waldmann 66e74e0471 archiver modules: rename to *_cmd[s]
this was already required for list_cmd (due to "list"
being a builtin), now renamed all else also.
2022-08-13 22:59:48 +02:00
Thomas Waldmann 22804f05f9 move prune related code to borg.archiver.prune 2022-08-13 20:59:18 +02:00
Thomas Waldmann 47c2672a64 ssh:// URLs: remove support for /~otheruser/, see #6855
If you used this, just replace it by:

ssh://user@host:port/home/otheruser/
2022-07-15 16:02:45 +02:00
Thomas Waldmann 7957af562d blacken all the code
https://black.readthedocs.io/
2022-07-06 16:34:38 +02:00