<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xml:base="https://docs.spec-kitty.ai/development/testing/permission-denied-tests/">
  <id>https://docs.spec-kitty.ai/development/testing/permission-denied-tests/</id>
  <title>Write Permission-Denied Tests</title>
  <updated>2026-10-22T00:00:00.000Z</updated>
  <author><name>Spec Kitty</name></author>
  <link rel="alternate" type="text/html" href="https://docs.spec-kitty.ai/development/testing/permission-denied-tests/"/>
  <summary>Inject PermissionError at the filesystem seam so a denied-access test gives the same verdict as root and as a regular user.</summary>
  <content type="html">&lt;span id=&quot;write-permission-denied-tests&quot;&gt;&lt;/span&gt;
&lt;p&gt;A test that covers an &lt;code dir=&quot;auto&quot;&gt;EACCES&lt;/code&gt; branch must reach that branch for every uid.&lt;/p&gt;
&lt;p&gt;Root ignores mode bits. CI containers and cloud sessions run as uid 0, so a test that denies access with &lt;code dir=&quot;auto&quot;&gt;chmod&lt;/code&gt; either skips there or passes without reaching the branch it names (#5654, #5691). Inject the denial at the filesystem seam instead, with the helpers in &lt;a href=&quot;https://github.com/spec-kitty/spec-kitty/blob/main/tests/_support/eacces.py&quot;&gt;&lt;code dir=&quot;auto&quot;&gt;tests/_support/eacces.py&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;pick-the-seam-the-code-under-test-calls&quot;&gt;Pick the seam the code under test calls&lt;/h2&gt;





















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;The code under test&lt;/th&gt;&lt;th&gt;Use&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;calls a &lt;code dir=&quot;auto&quot;&gt;Path&lt;/code&gt; method (&lt;code dir=&quot;auto&quot;&gt;read_text&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;read_bytes&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;write_text&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;stat&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;exists&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;iterdir&lt;/code&gt;, …)&lt;/td&gt;&lt;td&gt;&lt;code dir=&quot;auto&quot;&gt;deny_path_method(monkeypatch, &quot;&amp;#x3C;method&gt;&quot;, path)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;calls the builtin &lt;code dir=&quot;auto&quot;&gt;open()&lt;/code&gt; in one module&lt;/td&gt;&lt;td&gt;&lt;code dir=&quot;auto&quot;&gt;deny_open_in(monkeypatch, module, path)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;removes an entry of a read-only directory (&lt;code dir=&quot;auto&quot;&gt;Path.unlink&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;Path.rmdir&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;shutil.rmtree&lt;/code&gt;)&lt;/td&gt;&lt;td&gt;&lt;code dir=&quot;auto&quot;&gt;deny_removal_from(monkeypatch, directory)&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Each helper denies one target and delegates every other call. &lt;code dir=&quot;auto&quot;&gt;deny_path_method&lt;/code&gt; and &lt;code dir=&quot;auto&quot;&gt;deny_open_in&lt;/code&gt; compare resolved paths, so a symlinked spelling of the target is denied too. &lt;code dir=&quot;auto&quot;&gt;deny_removal_from&lt;/code&gt; applies the POSIX rule that removing an entry needs a writable parent, and lifts it once the code restores the write bit, so a retry-after-chmod branch runs as it does for a regular user.&lt;/p&gt;
&lt;div class=&quot;expressive-code&quot;&gt;&lt;figure class=&quot;frame not-content&quot;&gt;&lt;figcaption class=&quot;header&quot;&gt;&lt;/figcaption&gt;&lt;pre data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt; tests._support.eacces &lt;/span&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt; deny_path_method&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;test_unreadable_env_file_fails_loud&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#7FDBCA;--1:#096E72&quot;&gt;tmp_path&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;--0:#7FDBCA;--1:#096E72&quot;&gt;monkeypatch&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;:&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;env_path &lt;/span&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#B2CCD6;--1:#096E72&quot;&gt;write_env&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;tmp_path&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#ECC48D;--1:#984E4D&quot;&gt;FOO=bar&lt;/span&gt;&lt;span style=&quot;--0:#F78C6C;--1:#AA0982&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#B2CCD6;--1:#096E72&quot;&gt;deny_path_method&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;monkeypatch&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#ECC48D;--1:#984E4D&quot;&gt;read_text&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt; env_path&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;
&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt; pytest.&lt;/span&gt;&lt;span style=&quot;--0:#B2CCD6;--1:#096E72&quot;&gt;raises&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;OperatorEnvFileUnreadableError&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;):&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;ec-line&quot;&gt;&lt;div class=&quot;code&quot;&gt;&lt;span class=&quot;indent&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;--0:#B2CCD6;--1:#096E72&quot;&gt;load_operator_env_file&lt;/span&gt;&lt;span style=&quot;--1:#403F53&quot;&gt;&lt;span style=&quot;--0:#D6DEEB&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;--0:#D7DBE0&quot;&gt;start&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;tmp_path&lt;/span&gt;&lt;span style=&quot;--0:#D9F5DD;--1:#111111&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt; &lt;/span&gt;&lt;span style=&quot;--0:#D7DBE0;--1:#403F53&quot;&gt;environ&lt;/span&gt;&lt;span style=&quot;--0:#C792EA;--1:#8844AE&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;--0:#82AAFF;--1:#3B61B0&quot;&gt;{}&lt;/span&gt;&lt;span style=&quot;--0:#D6DEEB;--1:#403F53&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;&lt;/div&gt;
&lt;p&gt;Notes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Deny the call the code actually makes. A test whose seam is missed goes red on its first run, because the read succeeds.&lt;/li&gt;
&lt;li&gt;Deny every method that reaches the file when there is more than one, for example both &lt;code dir=&quot;auto&quot;&gt;read_text&lt;/code&gt; and &lt;code dir=&quot;auto&quot;&gt;read_bytes&lt;/code&gt; for a guarded reader.&lt;/li&gt;
&lt;li&gt;&lt;code dir=&quot;auto&quot;&gt;sqlite3.connect&lt;/code&gt; opens in C, so no helper reaches it. Deny the parent &lt;code dir=&quot;auto&quot;&gt;Path.mkdir&lt;/code&gt; instead, as #5654 did.&lt;/li&gt;
&lt;li&gt;A stat through a locked directory is a &lt;code dir=&quot;auto&quot;&gt;Path.stat&lt;/code&gt; denial on the entry, for example the symlink a &lt;code dir=&quot;auto&quot;&gt;#3194&lt;/code&gt; test plants into the locked directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;prove-the-test-bites&quot;&gt;Prove the test bites&lt;/h2&gt;
&lt;p&gt;Plant a break in the product’s error handling, run the test as root, and watch it go red. Revert the break; never commit it. Then run the test as a regular user as well and confirm the same verdict.&lt;/p&gt;
&lt;h2 id=&quot;when-the-real-kernel-check-is-the-point&quot;&gt;When the real kernel check is the point&lt;/h2&gt;
&lt;p&gt;Keep &lt;code dir=&quot;auto&quot;&gt;chmod&lt;/code&gt;, and skip honestly with &lt;code dir=&quot;auto&quot;&gt;mode_bits_enforced&lt;/code&gt; from the same module, only when the test must exercise the kernel permission check itself: for example &lt;code dir=&quot;auto&quot;&gt;test_safe_stat_predicates.py&lt;/code&gt;, which pins that a real &lt;code dir=&quot;auto&quot;&gt;stat()&lt;/code&gt; through a &lt;code dir=&quot;auto&quot;&gt;0o000&lt;/code&gt; directory raises, or a test that runs a child process no in-process seam reaches. Add such a test to &lt;code dir=&quot;auto&quot;&gt;tests/architectural/_exemptions/root_vacuous_denial.txt&lt;/code&gt; with the &lt;code dir=&quot;auto&quot;&gt;kernel-check&lt;/code&gt; category and a reason.&lt;/p&gt;
&lt;h2 id=&quot;the-gate&quot;&gt;The gate&lt;/h2&gt;
&lt;p&gt;&lt;code dir=&quot;auto&quot;&gt;tests/architectural/test_no_root_vacuous_denial_tests.py&lt;/code&gt; fails on a test function that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;checks for root (&lt;code dir=&quot;auto&quot;&gt;os.geteuid() == 0&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;not os.getuid()&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;getpass.getuser() == &quot;root&quot;&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;calls &lt;code dir=&quot;auto&quot;&gt;mode_bits_enforced&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;skips or returns on &lt;code dir=&quot;auto&quot;&gt;os.access(...)&lt;/code&gt;, including inside &lt;code dir=&quot;auto&quot;&gt;skipif(...)&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;or &lt;code dir=&quot;auto&quot;&gt;chmod&lt;/code&gt;s a path to a mode that denies its owner (no read, or a locked directory such as &lt;code dir=&quot;auto&quot;&gt;0o555&lt;/code&gt;) without calling an &lt;code dir=&quot;auto&quot;&gt;eacces&lt;/code&gt; seam helper in its own body.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A managed read-only file (&lt;code dir=&quot;auto&quot;&gt;0o444&lt;/code&gt;, &lt;code dir=&quot;auto&quot;&gt;0o400&lt;/code&gt;) is not flagged: replacing it does not need write permission on it, so the verdict is the same for every uid. Each exemption lists the kinds the detector reports for its function, so an exempted test that gains a root skip goes red. The exemption file is shrink-only.&lt;/p&gt;
&lt;h2 id=&quot;see-also&quot;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;/development/testing/&quot;&gt;Testing index&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;/development/testing/testing-flakiness/&quot;&gt;Test-flakiness handling policy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
</entry>
