Skip to content

Commit 17bda32

Browse files
vstinnerpicnixz
authored andcommitted
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 65b2554 commit 17bda32

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
@@ -13663,6 +13663,10 @@ static PyObject *
1366313663
os__clearenv_impl(PyObject *module)
1366413664
/*[clinic end generated code: output=2d6705d62c014b51 input=47d2fa7f323c43ca]*/
1366513665
{
13666+
if (PySys_Audit("os._clearenv", NULL) < 0) {
13667+
return NULL;
13668+
}
13669+
1366613670
errno = 0;
1366713671
int err = clearenv();
1366813672
if (err) {

0 commit comments

Comments
 (0)