Skip to content

Commit

Permalink
Merge pull request #261 from gkreitz/check_bad_symlinks
Browse files Browse the repository at this point in the history
Check that all symlinks point to something existing within the problem package
  • Loading branch information
pehrsoderman authored Jun 6, 2024
2 parents 41eb0f5 + 7e8b7e3 commit 9760867
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions problemtools/verifyproblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,8 @@ def check(self, args: argparse.Namespace) -> tuple[int, int]:
if not re.match('^[a-z0-9]+$', self.shortname):
self.error(f"Invalid shortname '{self.shortname}' (must be [a-z0-9]+)")

self._check_symlinks()

run.limit.check_limit_capabilities(self)

for part in args.parts:
Expand All @@ -1861,6 +1863,30 @@ def check(self, args: argparse.Namespace) -> tuple[int, int]:
pass
return ProblemAspect.errors, ProblemAspect.warnings

def _check_symlinks(self):
"""Check that all symlinks point to something existing within the problem package"""
probdir = os.path.realpath(self.probdir)
for root, dirs, files in os.walk(probdir):
for file in dirs + files:
filename = os.path.join(root, file)
if os.path.islink(filename):
target = os.path.realpath(filename)
# relfile is the filename of the symlink, relative to the problem root (only used for nicer error messages)
relfile = os.path.relpath(filename, self.probdir)
# reltarget is what the symlink points to (absolute, or relative to where the symlink is)
reltarget = os.readlink(filename)
if not os.path.exists(target):
self.error(
f"Symlink {relfile} links to {reltarget} which does not exist"
)
if os.path.commonpath([probdir, target]) != probdir:
self.error(
f"Symlink {relfile} links to {reltarget} which is outside of problem package"
)
if os.path.isabs(reltarget):
self.error(
f"Symlink {relfile} links to {reltarget} which is an absolute path. Symlinks must be relative."
)

def re_argument(s: str) -> Pattern[str]:
try:
Expand Down

0 comments on commit 9760867

Please sign in to comment.