Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue where symlink loop detection considered all symlinks to be loops #185

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wordfence/scanning/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .matching import Matcher, RegexMatcher
from .filtering import FileFilter, filter_any
from ..util import timing
from ..util.io import StreamReader
from ..util.io import StreamReader, is_same_file
from ..util.pcre import PcreOptions, PCRE_DEFAULT_OPTIONS, PcreJitStack
from ..util.units import scale_byte_unit
from ..intel.signatures import SignatureSet
Expand Down Expand Up @@ -140,7 +140,7 @@ def __init__(
def _is_loop(self, path: str, parents: Optional[List[str]] = None) -> bool:
realpath = os.path.realpath(path)
try:
if os.path.samefile(path, realpath):
if is_same_file(path, realpath):
log.warning(
f'Symlink pointing to itself detected at {path}'
)
Expand Down
25 changes: 24 additions & 1 deletion wordfence/util/io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fcntl
import os
from typing import Optional, IO, TextIO, Generator
from enum import IntEnum
from enum import Enum, IntEnum


class IoException(Exception):
Expand Down Expand Up @@ -112,3 +112,26 @@ def ensure_file_is_writable(
return path
else:
return ensure_directory_is_writable(os.path.dirname(path))


class PathType(Enum):
FILE = 'file',
DIRECTORY = 'directory',
LINK = 'link'


def get_path_type(path: str) -> PathType:
if os.path.islink(path):
return PathType.LINK
elif os.path.isdir(path):
return PathType.DIRECTORY
else:
return PathType.FILE


def is_same_file(path: str, other: str) -> bool:
type = get_path_type(path)
other_type = get_path_type(other)
if type is not other_type:
return False
return os.path.samefile(path, other)