Skip to content

Conversation

albertvillanova
Copy link
Member

@albertvillanova albertvillanova commented Mar 10, 2025

Forbid all modules by default except whitelist authorized_imports.

This PR implements a more robust security approach: use a deny-by-default policy and only whitelist specific modules that are known to be safe for your use case, rather than trying to maintain a comprehensive blacklist

As discussed in: #929 (review)

@albertvillanova albertvillanova changed the title Forbid all modules by default Forbid all modules by default except whitelist authorized_imports Mar 10, 2025
@albertvillanova albertvillanova marked this pull request as ready for review March 10, 2025 21:46
Comment on lines +43 to +57
# Non-exhaustive list of dangerous modules that should not be imported
DANGEROUS_MODULES = [
"builtins",
"io",
"multiprocessing",
"os",
"pathlib",
"pty",
"shutil",
"socket",
"subprocess",
"sys",
]


Copy link
Contributor

@sysradium sysradium Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Non-exhaustive list of dangerous modules that should not be imported
DANGEROUS_MODULES = [
"builtins",
"io",
"multiprocessing",
"os",
"pathlib",
"pty",
"shutil",
"socket",
"subprocess",
"sys",
]

I guess this is no-longer needed? Also the test_vulnerability_via_sys_for_all_dangerous_modules test is to be deleted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just here for test coverage: whichever modification of the code, we want these modules to be forbidden.

Copy link
Collaborator

@aymeric-roucher aymeric-roucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix! So maybe we should add in the doc that now every module and submodules should be specified in h additional_authorized_imports

@albertvillanova
Copy link
Member Author

albertvillanova commented Mar 12, 2025

@aymeric-roucher, before this PR, only modules specified in additional_authorized_imports could be imported. That hasn't changed.

However, previously, modules could be hackily accessed and used without explicitly importing them, which was a potential security risk. This PR addresses that issue by adding a security layer to prevent accessing or using modules that have not been explicitly imported.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@albertvillanova albertvillanova merged commit ba17042 into huggingface:main Mar 14, 2025
4 checks passed
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.

4 participants