Skip to content

Autofix: Code scanning alert #3#5

Open
Notoriousjayy wants to merge 1 commit into
mainfrom
autofix/code-scanning-3
Open

Autofix: Code scanning alert #3#5
Notoriousjayy wants to merge 1 commit into
mainfrom
autofix/code-scanning-3

Conversation

@Notoriousjayy
Copy link
Copy Markdown
Owner

Automated autofix campaign run at 2026-01-18T23:40:42Z.

Alert: https://github.com/Notoriousjayy/ghas-code-scanning-toolkit/security/code-scanning/3

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread scripts/apply_policy.py
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")

cfg = json.loads(Path(args.config).read_text(encoding="utf-8"))
config_path = Path(args.config).expanduser().resolve()

Check failure

Code scanning / CodeQL

Uncontrolled data used in path expression High

This path depends on a
user-provided value
.

Copilot Autofix

AI 4 months ago

General approach: validate or constrain the user-supplied config path before using it to access the file system. Typical strategies are: restrict it to a known base directory, ensure it’s not absolute, and/or enforce a naming pattern or extension; then use resolve() and verify the final path is within the allowed base.

Best fit here with minimal functional change: keep accepting a --config path, but (1) interpret it relative to the current working directory when it’s not absolute, (2) normalize it, and (3) optionally enforce that it ends with .json (since the code expects JSON). We can also log a clear error if the path is suspicious or invalid. We already use Path and .resolve(), so we don’t need new imports or libraries.

Concrete changes in scripts/apply_policy.py:

  • Around line 69, replace the direct config_path = Path(args.config).expanduser().resolve() with a small block that:
    • Builds an initial Path from args.config.
    • Optionally enforces a simple extension check (e.g., .json).
    • Resolves the path.
  • Keep the existing is_file() check and subsequent read_text logic the same, so functionality is preserved except for the added validation.

No new helper functions or external imports are required; everything can be done in place using pathlib.Path.

Suggested changeset 1
scripts/apply_policy.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/scripts/apply_policy.py b/scripts/apply_policy.py
--- a/scripts/apply_policy.py
+++ b/scripts/apply_policy.py
@@ -66,7 +66,13 @@
 
     logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
 
-    config_path = Path(args.config).expanduser().resolve()
+    raw_config_path = Path(args.config)
+    # Basic validation for the configuration file path
+    if raw_config_path.suffix and raw_config_path.suffix.lower() != ".json":
+        log.error("Configuration file must have a .json extension: %s", raw_config_path)
+        return 1
+
+    config_path = raw_config_path.expanduser().resolve()
     if not config_path.is_file():
         log.error("Configuration file does not exist or is not a file: %s", config_path)
         return 1
EOF
@@ -66,7 +66,13 @@

logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")

config_path = Path(args.config).expanduser().resolve()
raw_config_path = Path(args.config)
# Basic validation for the configuration file path
if raw_config_path.suffix and raw_config_path.suffix.lower() != ".json":
log.error("Configuration file must have a .json extension: %s", raw_config_path)
return 1

config_path = raw_config_path.expanduser().resolve()
if not config_path.is_file():
log.error("Configuration file does not exist or is not a file: %s", config_path)
return 1
Copilot is powered by AI and may make mistakes. Always verify output.
@Notoriousjayy Notoriousjayy enabled auto-merge (squash) January 19, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants