Skip to content

Commit 29415c0

Browse files
vstinnerpicnixz
andauthored
gh-149473: Emit audit event on calling os.environ.clear() (#149768)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent 51a5715 commit 29415c0

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
@@ -13692,6 +13692,10 @@ static PyObject *
1369213692
os__clearenv_impl(PyObject *module)
1369313693
/*[clinic end generated code: output=2d6705d62c014b51 input=47d2fa7f323c43ca]*/
1369413694
{
13695+
if (PySys_Audit("os._clearenv", NULL) < 0) {
13696+
return NULL;
13697+
}
13698+
1369513699
errno = 0;
1369613700
int err = clearenv();
1369713701
if (err) {

0 commit comments

Comments
 (0)