Skip to content

Commit

Permalink
Add warning for dotfiles in info plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkrzyskow committed May 1, 2024
1 parent 5cb3117 commit 0e8b959
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
37 changes: 36 additions & 1 deletion material/plugins/info/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,16 @@ def on_config(self, config):

# Create self-contained example from project
files: list[str] = []
dotpaths: list[str] = []
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
for abs_root, dirnames, filenames in os.walk(os.getcwd()):
# Set and print progress indicator
indicator = f"Processing: {abs_root}"
print(indicator, end="\r", flush=True)

# Track warning to decide if the indicator should be cleared
warned = False

# Prune the folders in-place to prevent their processing
for name in list(dirnames):
# Resolve the absolute directory path
Expand All @@ -228,6 +232,12 @@ def on_config(self, config):
# Exclude the directory and all subdirectories
if self._is_excluded(path):
dirnames.remove(name)
continue

# Warn about .dotdirectories
if _warn_on_dotpath(path):
warned = True
dotpaths.append(path)

# Write files to the in-memory archive
for name in filenames:
Expand All @@ -238,12 +248,18 @@ def on_config(self, config):
if self._is_excluded(path):
continue

# Warn about .dotfiles
if _warn_on_dotpath(path):
warned = True
dotpaths.append(path)

# Resolve the relative path to create a matching structure
path = os.path.relpath(path, os.path.curdir)
f.write(path, os.path.join(example, path))

# Clear the line for the next indicator
print(" " * len(indicator), end="\r", flush=True)
if not warned:
print(" " * len(indicator), end="\r", flush=True)

# Add information on installed packages
f.writestr(
Expand Down Expand Up @@ -309,6 +325,16 @@ def on_config(self, config):
if buffer.nbytes > 1000000:
log.warning("Archive exceeds recommended maximum size of 1 MB")

# Print warning when file contains hidden .dotpaths
if dotpaths:
print("List of potentially sensitive files:")
for path in dotpaths:
print(f" - {path}")
log.warning(
"Archive contains potentially sensitive dotfiles.\nPlease "
"review and share only necessary data to reproduce the issue."
)

# Aaaaaand done
sys.exit(1)

Expand Down Expand Up @@ -488,6 +514,15 @@ def _get_project_config(project_config_file: str):

return config

# Check if the path is a .dotpath, and issue a warning when that is the case.
# The function also returns a boolean to track results outside it.
def _warn_on_dotpath(path: str) -> bool:
if path.rsplit(os.sep, 1)[-1].startswith("."):
log.warning(f"The following .dotpath will be included: {path}")
return True
return False


# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
Expand Down
37 changes: 36 additions & 1 deletion src/plugins/info/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,16 @@ def on_config(self, config):

# Create self-contained example from project
files: list[str] = []
dotpaths: list[str] = []
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
for abs_root, dirnames, filenames in os.walk(os.getcwd()):
# Set and print progress indicator
indicator = f"Processing: {abs_root}"
print(indicator, end="\r", flush=True)

# Track warning to decide if the indicator should be cleared
warned = False

# Prune the folders in-place to prevent their processing
for name in list(dirnames):
# Resolve the absolute directory path
Expand All @@ -228,6 +232,12 @@ def on_config(self, config):
# Exclude the directory and all subdirectories
if self._is_excluded(path):
dirnames.remove(name)
continue

# Warn about .dotdirectories
if _warn_on_dotpath(path):
warned = True
dotpaths.append(path)

# Write files to the in-memory archive
for name in filenames:
Expand All @@ -238,12 +248,18 @@ def on_config(self, config):
if self._is_excluded(path):
continue

# Warn about .dotfiles
if _warn_on_dotpath(path):
warned = True
dotpaths.append(path)

# Resolve the relative path to create a matching structure
path = os.path.relpath(path, os.path.curdir)
f.write(path, os.path.join(example, path))

# Clear the line for the next indicator
print(" " * len(indicator), end="\r", flush=True)
if not warned:
print(" " * len(indicator), end="\r", flush=True)

# Add information on installed packages
f.writestr(
Expand Down Expand Up @@ -309,6 +325,16 @@ def on_config(self, config):
if buffer.nbytes > 1000000:
log.warning("Archive exceeds recommended maximum size of 1 MB")

# Print warning when file contains hidden .dotpaths
if dotpaths:
print("List of potentially sensitive files:")
for path in dotpaths:
print(f" - {path}")
log.warning(
"Archive contains potentially sensitive dotfiles.\nPlease "
"review and share only necessary data to reproduce the issue."
)

# Aaaaaand done
sys.exit(1)

Expand Down Expand Up @@ -488,6 +514,15 @@ def _get_project_config(project_config_file: str):

return config

# Check if the path is a .dotpath, and issue a warning when that is the case.
# The function also returns a boolean to track results outside it.
def _warn_on_dotpath(path: str) -> bool:
if path.rsplit(os.sep, 1)[-1].startswith("."):
log.warning(f"The following .dotpath will be included: {path}")
return True
return False


# -----------------------------------------------------------------------------
# Data
# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 0e8b959

Please sign in to comment.