Skip to content

Commit

Permalink
disallow unknown special tags
Browse files Browse the repository at this point in the history
only known special tags is @prot for now.
  • Loading branch information
ThomasWaldmann committed Oct 9, 2024
1 parent 6435336 commit 20f1ca4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/borg/archiver/tag_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ._common import with_repository, define_archive_filters_group
from ..archive import Archive
from ..constants import * # NOQA
from ..helpers import bin_to_hex, archivename_validator, tag_validator
from ..helpers import bin_to_hex, archivename_validator, tag_validator, Error
from ..manifest import Manifest

from ..logger import create_logger
Expand All @@ -25,6 +25,16 @@ def tags_set(tags):
else:
archive_infos = manifest.archives.list_considering(args)

def check_special(tags):
if tags:
special = {tag for tag in tags_set(tags) if tag.startswith("@")}
if not special.issubset(SPECIAL_TAGS):
raise Error("unknown special tags given.")

check_special(args.set_tags)
check_special(args.add_tags)
check_special(args.remove_tags)

for archive_info in archive_infos:
archive = Archive(manifest, archive_info.id, cache=cache)
if args.set_tags:
Expand Down
4 changes: 4 additions & 0 deletions src/borg/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@
# tar related
SCHILY_XATTR = "SCHILY.xattr." # xattr key prefix in tar PAX headers

# special tags
# @PROT protects archives against accidential deletion or modification by delete, prune or recreate.
SPECIAL_TAGS = frozenset(["@PROT"])

# return codes returned by borg command
EXIT_SUCCESS = 0 # everything done, no problems
EXIT_WARNING = 1 # reached normal end of operation, but there were issues (generic warning)
Expand Down
16 changes: 16 additions & 0 deletions src/borg/testsuite/archiver/tag_cmd_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from ...constants import * # NOQA
from . import cmd, generate_archiver_tests, RK_ENCRYPTION
from ...helpers import Error

pytest_generate_tests = lambda metafunc: generate_archiver_tests(metafunc, kinds="local") # NOQA

Expand Down Expand Up @@ -45,3 +48,16 @@ def test_tag_set_noclobber_special(archivers, request):
# it is possible though to use --set if the existing special tags are also given:
output = cmd(archiver, "tag", "-a", "archive", "--set", "noclobber", "--set", "@PROT")
assert "tags: @PROT,noclobber." in output


def test_tag_set_only_known_special(archivers, request):
archiver = request.getfixturevalue(archivers)
cmd(archiver, "repo-create", RK_ENCRYPTION)
cmd(archiver, "create", "archive", archiver.input_path)
# user can't set / add / remove unknown special tags
with pytest.raises(Error):
cmd(archiver, "tag", "-a", "archive", "--set", "@UNKNOWN")
with pytest.raises(Error):
cmd(archiver, "tag", "-a", "archive", "--add", "@UNKNOWN")
with pytest.raises(Error):
cmd(archiver, "tag", "-a", "archive", "--remove", "@UNKNOWN")

0 comments on commit 20f1ca4

Please sign in to comment.