-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Respect --allow-local-file-access flag in artwork and sourcecode
This change: * Generalize file access checks. * When `--allow-local-file-access` flag is set, only allow access to files within src dir and below. * Always allow access to `templates_dir` for XML entity includes. Fixes GHSA-432c-wxpg-m4q3
- Loading branch information
Showing
6 changed files
with
117 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
# Copyright The IETF Trust 2017, All Rights Reserved | ||
# Copyright The IETF Trust 2017, All Rights Reserved | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals, print_function | ||
|
||
from xml2rfc.util import name, num, date, postal | ||
from xml2rfc.util import name, num, date, postal, file | ||
|
||
__all__ = ['name', 'num', 'date', 'postal', ] | ||
__all__ = [ | ||
"name", | ||
"num", | ||
"date", | ||
"postal", | ||
"file", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright The IETF Trust 2025, All Rights Reserved | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import re | ||
|
||
|
||
class FileAccessError(Exception): | ||
"""File access error""" | ||
|
||
pass | ||
|
||
|
||
def can_access(options, source, path, access_templates=False): | ||
# templates can be always accessed | ||
template_path = os.path.abspath(os.path.join(options.template_dir, path)) | ||
if ( | ||
access_templates | ||
and template_path.startswith(options.template_dir) | ||
and os.path.exists(template_path) | ||
): | ||
return True | ||
|
||
# user allowed access? | ||
if not options.allow_local_file_access: | ||
raise FileAccessError( | ||
f"Can not access local file: {path}. Use --allow-local-file-access enable access." | ||
) | ||
|
||
# does it have shell meta-chars? | ||
shellmeta = re.compile("[><*[`$|;&(#]") | ||
if shellmeta.search(path): | ||
raise FileAccessError(f"Found disallowed shell meta-characters in {path}") | ||
|
||
# file is within the source dir? | ||
dir = os.path.abspath(os.path.dirname(source)) | ||
path = os.path.abspath(os.path.join(dir, path)) | ||
if not path.startswith(dir): | ||
raise FileAccessError( | ||
f"Expected a file located beside or below the .xml source (in {dir}), but found a reference to {path}" | ||
) | ||
|
||
# does it exists? | ||
if not os.path.exists(path): | ||
raise FileAccessError(f"Expected a file at '{path}', but no such file exists") | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters