Skip to content

Commit 0ad9396

Browse files
miss-islingtonvstinnerpicnixz
authored
[3.15] gh-149473: Emit audit event on calling os.environ.clear() (GH-149768) (#150094)
gh-149473: Emit audit event on calling os.environ.clear() (GH-149768) (cherry picked from commit 29415c0) Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 6b46aac commit 0ad9396

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Doc/library/os.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,25 @@ process and user.
219219
:data:`os.environ`, and when one of the :meth:`~dict.pop` or
220220
:meth:`~dict.clear` methods is called.
221221

222+
If the :manpage:`clearenv(3)` function is available, the :meth:`~dict.clear` method
223+
uses it and emits a single ``os._clearenv`` audit event. Otherwise, it emits
224+
an ``os.unsetenv`` event on each deleted variable.
225+
226+
.. audit-event:: os.unsetenv key os.unsetenv
227+
228+
.. audit-event:: os._clearenv "" os._clearenv
229+
222230
.. seealso::
223231

224232
The :func:`os.reload_environ` function.
225233

226234
.. versionchanged:: 3.9
227235
Updated to support :pep:`584`'s merge (``|``) and update (``|=``) operators.
228236

237+
.. versionchanged:: 3.15
238+
The :meth:`~dict.clear` method can now emit an ``os._clearenv`` audit
239+
event.
240+
229241

230242
.. data:: environb
231243

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Calling ``os.environ.clear()`` now emits ``os._clearenv`` auditing event.
2+
Patch by Victor Stinner.

Modules/posixmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13678,6 +13678,10 @@ static PyObject *
1367813678
os__clearenv_impl(PyObject *module)
1367913679
/*[clinic end generated code: output=2d6705d62c014b51 input=47d2fa7f323c43ca]*/
1368013680
{
13681+
if (PySys_Audit("os._clearenv", NULL) < 0) {
13682+
return NULL;
13683+
}
13684+
1368113685
errno = 0;
1368213686
int err = clearenv();
1368313687
if (err) {

0 commit comments

Comments
 (0)