Skip to content

gh-134587: Suggest fix for tempfile.mkdtemp() in Windows AppContainer#134607

Draft
vedant713 wants to merge 10 commits intopython:mainfrom
vedant713:suggest-appcontainer-fix
Draft

gh-134587: Suggest fix for tempfile.mkdtemp() in Windows AppContainer#134607
vedant713 wants to merge 10 commits intopython:mainfrom
vedant713:suggest-appcontainer-fix

Conversation

@vedant713
Copy link
Copy Markdown

@vedant713 vedant713 commented May 24, 2025


Proposed Solution

  • Detect whether the process is running inside an AppContainer.
  • Retrieve the AppContainer SID using whoami /user.
  • Use icacls to grant Full Control access to that SID for the created directory.

Suggested Helper Functions

if _os.name == 'nt':
    import ctypes as _ctypes
    import ctypes.wintypes as _wintypes
    import subprocess as _subprocess

    def _is_appcontainer():
        TOKEN_QUERY = 0x0008
        TokenIsAppContainer = 29
        h_token = _wintypes.HANDLE()
        if not _ctypes.windll.advapi32.OpenProcessToken(
            _ctypes.windll.kernel32.GetCurrentProcess(),
            TOKEN_QUERY,
            _ctypes.byref(h_token)
        ):
            return False

        is_container = _wintypes.BOOL()
        return_length = _wintypes.DWORD()
        if not _ctypes.windll.advapi32.GetTokenInformation(
            h_token,
            TokenIsAppContainer,
            _ctypes.byref(is_container),
            _ctypes.sizeof(is_container),
            _ctypes.byref(return_length)
        ):
            return False

        return bool(is_container.value)

    def _grant_appcontainer_access(path):
        try:
            sid = _subprocess.check_output("whoami /user", shell=True)
            sid_str = sid.decode().splitlines()[1].split()[1]
            _subprocess.run(f'icacls \"{path}\" /grant *{sid_str}:(OI)(CI)F', shell=True, check=True)
        except Exception:
            pass

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.

1 participant