fileaudit 0.2.4__tar.gz → 0.2.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. fileaudit-0.2.6/.github/workflows/publish-release.yml +57 -0
  2. fileaudit-0.2.6/.github/workflows/release-notes.yml +44 -0
  3. {fileaudit-0.2.4 → fileaudit-0.2.6}/CHANGELOG.md +24 -1
  4. {fileaudit-0.2.4 → fileaudit-0.2.6}/PKG-INFO +3 -3
  5. {fileaudit-0.2.4 → fileaudit-0.2.6}/README.md +1 -1
  6. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/_toc.yml +2 -0
  7. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/about.md +1 -0
  8. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/architecture/xml.md +24 -0
  9. fileaudit-0.2.6/docs/images/xml_diagram1.png +0 -0
  10. fileaudit-0.2.6/docs/notebooks/safe_validation.ipynb +331 -0
  11. fileaudit-0.2.6/docs/notebooks/xml_validation_demo.ipynb +546 -0
  12. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/python_validations.md +19 -0
  13. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/sponsors.md +43 -42
  14. fileaudit-0.2.6/docs/xml_validations.md +239 -0
  15. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/__about__.py +1 -1
  16. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/csv_check.py +8 -15
  17. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/gz_check.py +9 -26
  18. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/json_check.py +6 -15
  19. fileaudit-0.2.6/src/fileaudit/python_check.py +523 -0
  20. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/targz_check.py +30 -34
  21. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/xml_check.py +3 -7
  22. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/zip_check.py +36 -119
  23. fileaudit-0.2.6/tests/test_csv.py +364 -0
  24. fileaudit-0.2.6/tests/test_csv_validation.py +322 -0
  25. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_gz_validation.py +1 -13
  26. fileaudit-0.2.6/tests/test_gz_validation2.py +322 -0
  27. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_json.py +7 -4
  28. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_json_core.py +15 -11
  29. fileaudit-0.2.6/tests/test_python.py +569 -0
  30. fileaudit-0.2.6/tests/test_python_realfiles.py +34 -0
  31. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_python_timeouts.py +0 -22
  32. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_targz.py +19 -18
  33. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_targz_core.py +6 -12
  34. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_unittest_json.py +28 -20
  35. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_xml.py +13 -14
  36. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_xml_extra.py +3 -2
  37. fileaudit-0.2.6/tests/test_xml_files.py +33 -0
  38. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_zip.py +2 -9
  39. fileaudit-0.2.6/tests/test_zip_blackbox.py +584 -0
  40. fileaudit-0.2.6/tests/validationfiles/python/file_with_warnings.py +732 -0
  41. fileaudit-0.2.6/tests/validationfiles/python/python2_file_willnotwork.py +80 -0
  42. fileaudit-0.2.6/tests/validationfiles/python/sample_with_null.py +0 -0
  43. fileaudit-0.2.4/docs/xml_validations.md +0 -178
  44. fileaudit-0.2.4/htmlcov/.gitignore +0 -2
  45. fileaudit-0.2.4/htmlcov/class_index.html +0 -448
  46. fileaudit-0.2.4/htmlcov/coverage_html_cb_dd2e7eb5.js +0 -735
  47. fileaudit-0.2.4/htmlcov/favicon_32_cb_c827f16f.png +0 -0
  48. fileaudit-0.2.4/htmlcov/function_index.html +0 -1423
  49. fileaudit-0.2.4/htmlcov/index.html +0 -283
  50. fileaudit-0.2.4/htmlcov/keybd_closed_cb_900cfef5.png +0 -0
  51. fileaudit-0.2.4/htmlcov/status.json +0 -1
  52. fileaudit-0.2.4/htmlcov/style_cb_4667309f.css +0 -391
  53. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf___about___py.html +0 -104
  54. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf___init___py.html +0 -99
  55. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_cli_py.html +0 -319
  56. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_csv_check_py.html +0 -786
  57. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_gz_check_py.html +0 -687
  58. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_json_check_py.html +0 -486
  59. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_python_check_py.html +0 -579
  60. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_tar_check_py.html +0 -465
  61. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_targz_check_py.html +0 -472
  62. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_xml_check_py.html +0 -671
  63. fileaudit-0.2.4/htmlcov/z_65b8f32785d686bf_zip_check_py.html +0 -660
  64. fileaudit-0.2.4/src/fileaudit/python_check.py +0 -457
  65. fileaudit-0.2.4/tests/test_csv.py +0 -840
  66. fileaudit-0.2.4/tests/test_python.py +0 -791
  67. fileaudit-0.2.4/tests/test_xml_files.py +0 -75
  68. {fileaudit-0.2.4 → fileaudit-0.2.6}/.github/workflows/docbuild.yml +0 -0
  69. {fileaudit-0.2.4 → fileaudit-0.2.6}/.github/workflows/sastvalidation.yml +0 -0
  70. {fileaudit-0.2.4 → fileaudit-0.2.6}/.github/workflows/testsuite.yml +0 -0
  71. {fileaudit-0.2.4 → fileaudit-0.2.6}/.gitignore +0 -0
  72. {fileaudit-0.2.4 → fileaudit-0.2.6}/CONTRIBUTE.md +0 -0
  73. {fileaudit-0.2.4 → fileaudit-0.2.6}/LICENSE +0 -0
  74. {fileaudit-0.2.4 → fileaudit-0.2.6}/SECURITY.md +0 -0
  75. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/CONTRIBUTE.md +0 -0
  76. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/SECURITY.md +0 -0
  77. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/_config.yml +0 -0
  78. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/_static/nocxstyle.css +0 -0
  79. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/ai_policy.md +0 -0
  80. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/architecture/architecture_overview.md +0 -0
  81. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/changelog.md +0 -0
  82. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/csv_validations.md +0 -0
  83. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/examples.md +0 -0
  84. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/general_use.md +0 -0
  85. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/gz_validations.md +0 -0
  86. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/help.md +0 -0
  87. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/OO.png +0 -0
  88. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/ROI_logo.png +0 -0
  89. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/YourLogoHere.png +0 -0
  90. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/ai_use.png +0 -0
  91. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/architecture_overview.png +0 -0
  92. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/ca_logo.png +0 -0
  93. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/codeauditlogo.png +0 -0
  94. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/fileaudit_deps.png +0 -0
  95. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/fileaudit_logo.png +0 -0
  96. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/filescan_screenshot_16012026.png +0 -0
  97. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/modulescan_screenshot_16012026.png +0 -0
  98. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/nocxbanner.png +0 -0
  99. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/overview_linkaudit.png +0 -0
  100. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/images/overview_screenshot_16012026.png +0 -0
  101. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/installation.md +0 -0
  102. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/intro.md +0 -0
  103. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/json_validations.md +0 -0
  104. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/license.md +0 -0
  105. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/notebooks/json_check_demo.ipynb +0 -0
  106. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/notebooks/validate_csv_examples.ipynb +0 -0
  107. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.cli.rst +0 -0
  108. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.csv_check.rst +0 -0
  109. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.gz_check.rst +0 -0
  110. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.json_check.rst +0 -0
  111. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.python_check.rst +0 -0
  112. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.rst +0 -0
  113. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.tar_check.rst +0 -0
  114. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.targz_check.rst +0 -0
  115. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.xml_check.rst +0 -0
  116. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/fileaudit.zip_check.rst +0 -0
  117. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/reference/modules.rst +0 -0
  118. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/tar_validations.md +0 -0
  119. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/targz_validations.md +0 -0
  120. {fileaudit-0.2.4 → fileaudit-0.2.6}/docs/zip_validations.md +0 -0
  121. {fileaudit-0.2.4 → fileaudit-0.2.6}/fileaudit.png +0 -0
  122. {fileaudit-0.2.4 → fileaudit-0.2.6}/pyproject.toml +0 -0
  123. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/__init__.py +0 -0
  124. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/cli.py +0 -0
  125. {fileaudit-0.2.4 → fileaudit-0.2.6}/src/fileaudit/tar_check.py +0 -0
  126. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_cli.py +0 -0
  127. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_csv_extra.py +0 -0
  128. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_gz.py +0 -0
  129. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_gz_extra.py +0 -0
  130. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_tar.py +0 -0
  131. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/test_validate_tar.py +0 -0
  132. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/csv/sample.csv +0 -0
  133. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/gz/samplefile.txt.gz +0 -0
  134. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/json/codeaudit_scan.json +0 -0
  135. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/json/nojsonfile.json +0 -0
  136. /fileaudit-0.2.4/tests/validationfiles/python/sample_with_null.py → /fileaudit-0.2.6/tests/validationfiles/python/incorrect_python_file.py +0 -0
  137. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/cyclic.xml +0 -0
  138. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/expansion.xml +0 -0
  139. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/external.xml +0 -0
  140. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/nlnetfeed.atom +0 -0
  141. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/nocx_rss.xml +0 -0
  142. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/xmlbomb.xml +0 -0
  143. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/xmlbomb2.xml +0 -0
  144. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/xml/xmlbomb3.xml +0 -0
  145. {fileaudit-0.2.4 → fileaudit-0.2.6}/tests/validationfiles/zip/sample.zip +0 -0
@@ -0,0 +1,57 @@
1
+ name: Publish Python 🐍 distribution 📦 to PyPI
2
+
3
+ # only publish to PyPI on tag pushes
4
+ on:
5
+ push:
6
+ tags:
7
+ - '*' # or 'v*' if you use version tags
8
+
9
+
10
+ jobs:
11
+ build:
12
+ name: Build distribution 📦
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v6
17
+ with:
18
+ persist-credentials: false
19
+ - name: Set up Python
20
+ uses: actions/setup-python@v6
21
+ with:
22
+ python-version: "3.x"
23
+ - name: Install pypa/build
24
+ run: >-
25
+ python3 -m
26
+ pip install
27
+ build
28
+ --user
29
+ - name: Build a wheel and a source distribution
30
+ run: python3 -m build
31
+ - name: Store the distribution packages
32
+ uses: actions/upload-artifact@v5
33
+ with:
34
+ name: python-package-distributions
35
+ path: dist/
36
+
37
+ publish-to-pypi:
38
+ name: >-
39
+ Publish Python 🐍 distribution 📦 to PyPI
40
+
41
+ needs:
42
+ - build
43
+ runs-on: ubuntu-latest
44
+ environment:
45
+ name: pypi
46
+ url: https://pypi.org/p/fileaudit/
47
+ permissions:
48
+ id-token: write # IMPORTANT: mandatory for trusted publishing
49
+
50
+ steps:
51
+ - name: Download all the dists
52
+ uses: actions/download-artifact@v6
53
+ with:
54
+ name: python-package-distributions
55
+ path: dist/
56
+ - name: Publish distribution 📦 to PyPI
57
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,44 @@
1
+ name: Release Notes
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - '[0-9]*' # 1.0.0, 2.3.1, etc.
7
+
8
+ permissions:
9
+ contents: write
10
+
11
+ jobs:
12
+ release:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ with:
17
+ fetch-depth: 0
18
+
19
+ - name: Create GitHub Release
20
+ env:
21
+ GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22
+ run: |
23
+ # Get the previous tag (or empty if this is the first)
24
+ PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || true)
25
+
26
+ if [ -n "$PREV_TAG" ]; then
27
+ NOTES=$(git log ${PREV_TAG}..HEAD --pretty=format:'* %s (%h)')
28
+ COMPARE="https://github.com/${{ github.repository }}/compare/${PREV_TAG}...${GITHUB_REF_NAME}"
29
+ else
30
+ NOTES=$(git log --pretty=format:'* %s (%h)')
31
+ COMPARE="https://github.com/${{ github.repository }}/commits/${GITHUB_REF_NAME}"
32
+ fi
33
+
34
+ {
35
+ echo "## What's Changed"
36
+ echo ""
37
+ echo "$NOTES"
38
+ echo ""
39
+ echo "**Full Changelog**: $COMPARE"
40
+ } > notes.md
41
+
42
+ gh release create "$GITHUB_REF_NAME" \
43
+ --title "Release $GITHUB_REF_NAME" \
44
+ --notes-file notes.md
@@ -1,6 +1,29 @@
1
1
  # Change Log
2
2
 
3
- All notable changes to this project will be documented in this file.
3
+ All notable changes to this project will be documented in this file.
4
+ See also Github [Release notes](https://github.com/nocomplexity/fileaudit/tags) for full details.
5
+
6
+ ## Version 0.2.6
7
+
8
+ **Added**
9
+ * Activated GitHub provenance action to generate a verifiable build record upon releasing a new version on [PyPI](https://pypi.org/project/fileaudit/).
10
+ * Added an XML example notebook.
11
+
12
+ **Documentation**
13
+ * Updated project documentation.
14
+
15
+
16
+ ## Version 0.2.5
17
+
18
+ **Changed**
19
+
20
+ - CLI / Direct Call Exception Handling: Improved exception handling across all functions when called directly or via CLI. Exceptions are now propagated directly to the caller function rather than printed to stdout/stderr.
21
+
22
+ **Fixed**
23
+
24
+ - python_check Module: Removed print statements when `validate_python` encounters an exception, leaving exception handling entirely to the calling application. `validate_python` now returns True on success and raises an exception on failure.
25
+
26
+
4
27
 
5
28
  ## Version 0.2.4
6
29
 
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.5
2
2
  Name: fileaudit
3
- Version: 0.2.4
3
+ Version: 0.2.6
4
4
  Summary: Python file security validator.
5
5
  Project-URL: Homepage, https://github.com/nocomplexity/fileaudit
6
6
  Project-URL: Documentation, https://github.com/nocomplexity/fileaudit#readme
@@ -38,7 +38,7 @@ Description-Content-Type: text/markdown
38
38
  [![Documentation](https://img.shields.io/badge/Python%20File%20Audit%20Manual-Available-blue)](https://nocomplexity.github.io/fileaudit/)
39
39
  [![License](https://img.shields.io/badge/License-MPL--2.0-FFD700)](https://github.com/nocomplexity/fileaudit/blob/main/docs/license.md)
40
40
 
41
- [![fileaudit_logo](fileaudit.png)](https://fileaudit.nocomplexity.com/)
41
+ [![fileaudit_logo](https://raw.githubusercontent.com/nocomplexity/fileaudit/refs/heads/main/fileaudit.png)](https://fileaudit.nocomplexity.com/)
42
42
 
43
43
  File Audit – Simplify Python Security by adding one line!
44
44
  **Build secure Python applications by default. Validate files before you use them.**
@@ -7,7 +7,7 @@
7
7
  [![Documentation](https://img.shields.io/badge/Python%20File%20Audit%20Manual-Available-blue)](https://nocomplexity.github.io/fileaudit/)
8
8
  [![License](https://img.shields.io/badge/License-MPL--2.0-FFD700)](https://github.com/nocomplexity/fileaudit/blob/main/docs/license.md)
9
9
 
10
- [![fileaudit_logo](fileaudit.png)](https://fileaudit.nocomplexity.com/)
10
+ [![fileaudit_logo](https://raw.githubusercontent.com/nocomplexity/fileaudit/refs/heads/main/fileaudit.png)](https://fileaudit.nocomplexity.com/)
11
11
 
12
12
  File Audit – Simplify Python Security by adding one line!
13
13
  **Build secure Python applications by default. Validate files before you use them.**
@@ -27,6 +27,8 @@ parts:
27
27
  sections:
28
28
  - file: notebooks/validate_csv_examples
29
29
  - file: notebooks/json_check_demo
30
+ - file: notebooks/xml_validation_demo
31
+ - file: notebooks/safe_validation
30
32
 
31
33
 
32
34
 
@@ -13,6 +13,7 @@ A FreeBSD user since version 4 for its simplicity and rock-solid security, I sti
13
13
 
14
14
  Currently, I lead initiatives at NoComplexity.com, an innovative IT company focused on straightforward solutions. For more insights and updates, you can find me on X (despite my privacy and other concerns with BigTech...) at [@maikelmardjan](https://X.com/maikelmardjan).
15
15
 
16
+ (publications)=
16
17
  ## Other NO|Complexity Publications
17
18
 
18
19
  ### Cyber Security
@@ -2,6 +2,30 @@
2
2
 
3
3
  This section outlines specific implementation details for the `validation_xml` functionality.
4
4
 
5
+ ## High Level Flow
6
+
7
+ ![xml1](../images/xml_diagram1.png)
8
+
9
+
10
+ Explanation of the Main Stages:
11
+
12
+ | Stage | What happens | Security goal |
13
+ | --- | --- | --- |
14
+ | 1. Entry point | validate_xml decides whether it is being used as a direct call, a decorator, or a factory. | Flexible API without sacrificing safety. |
15
+ | 2. URL / Path validation | _validate_url_scheme rejects anything that is not a local path or a pure https:// URL. Protocol-relative URLs (//...) and other schemes are blocked. | Prevents SSRF and scheme-downgrade attacks. |
16
+ | 3. Size check (early) | Local files: stat(). Remote files: optional HEAD + hard limit on GET. | Stops huge files before they are fully loaded. |
17
+ | 4. Safe reading + GZip | Content is read with a hard size limit. GZip is decompressed with the same size limit on the decompressed data. | Protects against GZip bombs. |
18
+ | 5. DOCTYPE rejection | Simple regex rejects any <!DOCTYPE. | Completely eliminates XXE and classic entity-expansion attacks. |
19
+ | 6. Structural validation | XMLSecurityValidator parses the XML and walks the tree iteratively (no recursion). It enforces depth, element count, attribute count, name length and text length. | Protects against deep nesting, attribute bombs, large text nodes, etc. |
20
+ | 7. Error handling | Every failure raises FileValidationError with a clear message. | Consistent, easy-to-catch security errors. |
21
+
22
+ Key Design Decisions:
23
+ - No raw XML strings – only file paths / HTTPS URLs are accepted.
24
+ - HTTPS-only + redirect protection – HTTPSOnlyRedirectHandler blocks any redirect to a non-HTTPS location.
25
+ - Iterative tree walk – avoids Python recursion limits even on very deep (but still limited) documents.
26
+ - Early rejection – size and scheme checks happen before expensive parsing.
27
+ - Decorator & factory support – the same validation pipeline is reused whether you call the function directly or decorate another function.
28
+
5
29
 
6
30
  In the table below outlines how functionality implemented in `validate_xml` and its underlying functions (`_validate_xml_file`, `XMLSecurityValidator`, and `HTTPSOnlyRedirectHandler`) are created:
7
31
 
@@ -0,0 +1,331 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "04bdc232",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Safe validation for `validate_tar_gz` \n",
9
+ "\n",
10
+ "When using a direct validation function, such as `validate_tar_gz`, any exceptions raised during validation are passed to the calling function. This means the caller is responsible for handling validation errors.\n",
11
+ "\n",
12
+ "However, there are situations where you may prefer a simple `True` or `False` result instead of handling exceptions yourself. You might also want to catch the exception and integrate it into your own logging or error-reporting functionality.\n",
13
+ "\n",
14
+ "After all, the reason why validation failed is often important information that should not be lost.\n",
15
+ "\n",
16
+ "Below is an example of how to implement a `safe_validate_tar_gz` wrapper around `validate_tar_gz`. It accepts the same validation parameters, but catches exceptions and returns `False` when validation fails.\n",
17
+ "\n",
18
+ "This notebook demonstrates direct TAR.GZ validation and a safe wrapper that returns `True` or `False` while logging validation failures.\n",
19
+ "\n",
20
+ "The notebook creates its own temporary archive, smaller than 100 KB. No external archive is required."
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "markdown",
25
+ "id": "09ec0a33",
26
+ "metadata": {},
27
+ "source": [
28
+ "## 1. Install Python File Audit\n",
29
+ "\n",
30
+ "```bash\n",
31
+ "pip install fileaudit\n",
32
+ "```"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "markdown",
37
+ "id": "0db90c7c",
38
+ "metadata": {},
39
+ "source": [
40
+ "## 2. Imports and logging"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "execution_count": 1,
46
+ "id": "ccf48b4b",
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "import logging\n",
51
+ "import tarfile\n",
52
+ "import tempfile\n",
53
+ "from pathlib import Path\n",
54
+ "\n",
55
+ "from fileaudit.targz_check import validate_tar_gz, TarValidationError\n",
56
+ "\n",
57
+ "logging.basicConfig(\n",
58
+ " level=logging.INFO,\n",
59
+ " format='%(levelname)s: %(message)s',\n",
60
+ ")\n",
61
+ "logger = logging.getLogger('tar_gz_demo')"
62
+ ]
63
+ },
64
+ {
65
+ "cell_type": "markdown",
66
+ "id": "431eaf16",
67
+ "metadata": {},
68
+ "source": [
69
+ "## 3. Define `safe_validate_tar_gz`\n",
70
+ "\n",
71
+ "The direct API returns `True` when validation succeeds and raises an exception when validation fails. This wrapper catches validation exceptions, logs the reason, and returns `False`."
72
+ ]
73
+ },
74
+ {
75
+ "cell_type": "code",
76
+ "execution_count": 9,
77
+ "id": "f0ae4086",
78
+ "metadata": {},
79
+ "outputs": [],
80
+ "source": [
81
+ "def safe_validate_tar_gz(directory, **kwargs):\n",
82
+ " \"\"\"Validate a TAR.GZ archive and return a Boolean result.\n",
83
+ "\n",
84
+ " Args:\n",
85
+ " directory: Path to the TAR.GZ archive.\n",
86
+ " **kwargs: Additional keyword arguments passed to validate_tar_gz().\n",
87
+ "\n",
88
+ " Returns:\n",
89
+ " True if validation succeeds; False if validation raises an exception.\n",
90
+ " \"\"\"\n",
91
+ " try:\n",
92
+ " return validate_tar_gz(directory, **kwargs)\n",
93
+ " except Exception:\n",
94
+ " #comment this line below if you just want 'false'.\n",
95
+ " logger.exception('TAR.GZ validation failed for %s', directory) \n",
96
+ " return False"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "markdown",
101
+ "id": "47eea7f7",
102
+ "metadata": {},
103
+ "source": [
104
+ "## 4. Create a temporary TAR.GZ archive\n",
105
+ "\n",
106
+ "The archive contains a tiny text file. The cell checks that the resulting compressed archive is smaller than 100 KB."
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "execution_count": 3,
112
+ "id": "8c066279",
113
+ "metadata": {},
114
+ "outputs": [
115
+ {
116
+ "name": "stdout",
117
+ "output_type": "stream",
118
+ "text": [
119
+ "Created: /tmp/tmp3h__7xyf/demo.tar.gz\n",
120
+ "Archive size: 250 bytes (< 100 KB)\n"
121
+ ]
122
+ }
123
+ ],
124
+ "source": [
125
+ "temp_dir = tempfile.TemporaryDirectory()\n",
126
+ "work_dir = Path(temp_dir.name)\n",
127
+ "source_file = work_dir / 'hello.txt'\n",
128
+ "archive_path = work_dir / 'demo.tar.gz'\n",
129
+ "\n",
130
+ "source_file.write_text(\n",
131
+ " 'Hello! This is a Python File Audit validation demo.\\\\n',\n",
132
+ " encoding='utf-8',\n",
133
+ ")\n",
134
+ "\n",
135
+ "with tarfile.open(archive_path, mode='w:gz') as tar:\n",
136
+ " tar.add(source_file, arcname='hello.txt')\n",
137
+ "\n",
138
+ "archive_size = archive_path.stat().st_size\n",
139
+ "assert archive_size < 100 * 1024, f'Archive too large: {archive_size} bytes'\n",
140
+ "\n",
141
+ "print(f'Created: {archive_path}')\n",
142
+ "print(f'Archive size: {archive_size:,} bytes (< 100 KB)')"
143
+ ]
144
+ },
145
+ {
146
+ "cell_type": "markdown",
147
+ "id": "5e603147",
148
+ "metadata": {},
149
+ "source": [
150
+ "## 5. Direct validation with `validate_tar_gz`\n",
151
+ "\n",
152
+ "A valid archive should return `True`. If a check fails, the direct function raises an exception that the caller must handle."
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": 4,
158
+ "id": "f5c190ac",
159
+ "metadata": {},
160
+ "outputs": [
161
+ {
162
+ "name": "stdout",
163
+ "output_type": "stream",
164
+ "text": [
165
+ "Direct validation result: True\n"
166
+ ]
167
+ }
168
+ ],
169
+ "source": [
170
+ "try:\n",
171
+ " result = validate_tar_gz(archive_path)\n",
172
+ " print(f'Direct validation result: {result}')\n",
173
+ "except TarValidationError as exc:\n",
174
+ " print(f'Direct validation raised TarValidationError: {exc}')"
175
+ ]
176
+ },
177
+ {
178
+ "cell_type": "markdown",
179
+ "id": "51d2969e",
180
+ "metadata": {},
181
+ "source": [
182
+ "## 6. Demonstrate a validation failure\n",
183
+ "\n",
184
+ "This example deliberately sets `max_tar_members=0`. The archive contains one member, so it should fail this limit. The keyword argument is passed through to `validate_tar_gz`."
185
+ ]
186
+ },
187
+ {
188
+ "cell_type": "code",
189
+ "execution_count": 5,
190
+ "id": "1ef52fc7",
191
+ "metadata": {},
192
+ "outputs": [
193
+ {
194
+ "name": "stdout",
195
+ "output_type": "stream",
196
+ "text": [
197
+ "Caught validation exception: FileAudit Security Validation Failed - TAR validation failed: FileAudit Security Validation Failed - Archive contains 1 members, exceeding maximum of 0\n"
198
+ ]
199
+ }
200
+ ],
201
+ "source": [
202
+ "try:\n",
203
+ " validate_tar_gz(archive_path, max_tar_members=0)\n",
204
+ "except TarValidationError as exc:\n",
205
+ " print(f'Caught validation exception: {exc}')"
206
+ ]
207
+ },
208
+ {
209
+ "cell_type": "markdown",
210
+ "id": "79a905ba",
211
+ "metadata": {},
212
+ "source": [
213
+ "## 7. Safe validation\n",
214
+ "\n",
215
+ "The same successful and failing checks using the wrapper. A failure is logged with its traceback and represented by `False`."
216
+ ]
217
+ },
218
+ {
219
+ "cell_type": "code",
220
+ "execution_count": 10,
221
+ "id": "287cfd44",
222
+ "metadata": {},
223
+ "outputs": [
224
+ {
225
+ "name": "stdout",
226
+ "output_type": "stream",
227
+ "text": [
228
+ "Safe validation of valid archive: False\n",
229
+ "Safe validation with failing limit: False\n"
230
+ ]
231
+ }
232
+ ],
233
+ "source": [
234
+ "valid_result = safe_validate_tar_gz(archive_path)\n",
235
+ "print(f'Safe validation of valid archive: {valid_result}')\n",
236
+ "\n",
237
+ "invalid_result = safe_validate_tar_gz(archive_path, max_tar_members=0)\n",
238
+ "print(f'Safe validation with failing limit: {invalid_result}')"
239
+ ]
240
+ },
241
+ {
242
+ "cell_type": "markdown",
243
+ "id": "09e83f34",
244
+ "metadata": {},
245
+ "source": [
246
+ "## 8. Use the Boolean result in application code\n",
247
+ "\n",
248
+ "This is useful in batch jobs or workflows where the caller wants to branch on success or failure instead of handling an exception at every call site."
249
+ ]
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "execution_count": 7,
254
+ "id": "50de93c2",
255
+ "metadata": {},
256
+ "outputs": [
257
+ {
258
+ "name": "stdout",
259
+ "output_type": "stream",
260
+ "text": [
261
+ "Archive passed validation and can proceed to the next step.\n"
262
+ ]
263
+ }
264
+ ],
265
+ "source": [
266
+ "if safe_validate_tar_gz(archive_path):\n",
267
+ " print('Archive passed validation and can proceed to the next step.')\n",
268
+ "else:\n",
269
+ " print('Archive rejected. See the log for the validation reason.')"
270
+ ]
271
+ },
272
+ {
273
+ "cell_type": "markdown",
274
+ "id": "a4b44937",
275
+ "metadata": {},
276
+ "source": [
277
+ "## 9. Summary\n",
278
+ "\n",
279
+ "| Function | On success | On validation failure |\n",
280
+ "|---|---|---|\n",
281
+ "| `validate_tar_gz()` | Returns `True` | Raises an exception |\n",
282
+ "| `safe_validate_tar_gz()` | Returns `True` | Logs the exception and returns `False` |\n",
283
+ "\n",
284
+ "**Note:** Catching `Exception` makes the wrapper broad: it also catches unexpected errors, not only `TarValidationError`. For stricter handling, catch `TarValidationError` specifically if that is the exception type used by the validation failures you want to suppress. Validation passing means the archive passed the configured FileAudit checks; it is not a guarantee against every possible security threat.\n",
285
+ "\n",
286
+ "The temporary directory is automatically removed when the notebook kernel is restarted or when you run the cleanup cell below."
287
+ ]
288
+ },
289
+ {
290
+ "cell_type": "code",
291
+ "execution_count": 8,
292
+ "id": "b617724c",
293
+ "metadata": {},
294
+ "outputs": [
295
+ {
296
+ "name": "stdout",
297
+ "output_type": "stream",
298
+ "text": [
299
+ "Temporary demo files cleaned up.\n"
300
+ ]
301
+ }
302
+ ],
303
+ "source": [
304
+ "# Optional cleanup when you have finished the demo.\n",
305
+ "temp_dir.cleanup()\n",
306
+ "print('Temporary demo files cleaned up.')"
307
+ ]
308
+ }
309
+ ],
310
+ "metadata": {
311
+ "kernelspec": {
312
+ "display_name": "Python 3 (ipykernel)",
313
+ "language": "python",
314
+ "name": "python3"
315
+ },
316
+ "language_info": {
317
+ "codemirror_mode": {
318
+ "name": "ipython",
319
+ "version": 3
320
+ },
321
+ "file_extension": ".py",
322
+ "mimetype": "text/x-python",
323
+ "name": "python",
324
+ "nbconvert_exporter": "python",
325
+ "pygments_lexer": "ipython3",
326
+ "version": "3.15.1"
327
+ }
328
+ },
329
+ "nbformat": 4,
330
+ "nbformat_minor": 5
331
+ }