Skip to content

Commit 3eb3cd9

Browse files
ilaigoldclaude
andcommitted
fix(tm3): skip umask-masked modes and report permissive umask values
The TM3 "Unsafe Defaults" permission pattern `(?:mode|permission|umask)\s*=\s*(?:0?o?777|0?o?666)` reported `mode = 0o666 & ~umask`, which is the umask-respecting default (#644, item B6). It also read umask values backwards: `umask = 0o777`, the most restrictive value, was reported, while `umask 000` and `os.umask(0)`, which make new files world-writable, were not. - mode/permission: a 777/666 mode followed only by `& ~<name>umask` (or `& ~<name>.umask(...)`) up to the end of the expression is skipped. Bits added after the mask (`& ~umask | 0o777`) and masks that are not the umask (`& ~0`) are still reported. - umask assignment or shell command: report a value whose last octal digit leaves the world-write bit unmasked (0, 000, 0000, 004). - umask(...) call: report a zero or leading-0 octal argument with the same rule, except when the result is assigned (`old = os.umask(0)`), which is how Python reads the current umask before restoring it. Signed-off-by: Ilai Goldschmidt <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 025c65e commit 3eb3cd9

2 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎src/skillspector/nodes/analyzers/static_patterns_tool_misuse.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,20 @@
355355
# Overly permissive CORS / access
356356
(r"(?:CORS|cors)[^=]*=\s*['\"]?\*['\"]?", 0.65),
357357
(r"(?:allow|access)[_-]?(?:origin|hosts?)\s*=\s*['\"]?\*['\"]?", 0.7),
358-
# Unsafe permissions
359-
(r"(?:mode|permission|umask)\s*=\s*(?:0?o?777|0?o?666)", 0.8),
358+
# Unsafe permissions. A mode masked with the umask (0o666 & ~umask) respects it,
359+
# like a plain open(), unless more bits are added after the mask.
360+
(
361+
r"(?:mode|permission)\s*=\s*(?:0?o?777|0?o?666)"
362+
r"(?![ \t]*&[ \t]*~[ \t]*[\w.]*umask\b(?:\([^()\n]*\))?[ \t]*(?:[),;\]}#]|//|$))",
363+
0.8,
364+
),
365+
# A umask whose last octal digit leaves the world-write bit (0o002) unmasked, such
366+
# as 000, makes new files world-writable. A umask such as 0o777 is restrictive.
367+
(r"umask(?:\s*=\s*|[ \t]+)(?:0o)?[0-7]*[0145]\b", 0.8),
368+
# umask(0) as a statement or inside an expression. In a call, a number without a
369+
# leading 0 is decimal. `old = os.umask(0)` is how Python reads the umask before
370+
# restoring it, so a call whose result is assigned is not reported.
371+
(r"(?<![\w.])(?<!=)(?<!= )(?:\w+\.)*umask\(\s*0(?:o?[0-7]*[0145])?\s*\)", 0.8),
360372
(r"world[_-]?(?:readable|writable|executable)", 0.7),
361373
# Debug/dev mode in production
362374
(r"(?:debug|dev|development)[_-]?mode\s*=\s*(?:True|true|1|on|yes|enable)", 0.6),

‎tests/unit/test_patterns_new.py‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,108 @@ def test_tm1_rm_outside_dockerfile_stays_high(self) -> None:
17901790
def test_tm3_detected(self, content: str, filename: str, filetype: str) -> None:
17911791
assert any(f.rule_id == "TM3" for f in tm_mod.analyze(content, filename, filetype))
17921792

1793+
@pytest.mark.parametrize(
1794+
"content,filename,filetype,expected",
1795+
[
1796+
pytest.param("mode = 0o777", "fs.py", "python", "mode = 0o777", id="mode_777"),
1797+
pytest.param(
1798+
"permission = 0o666", "fs.py", "python", "permission = 0o666", id="permission_666"
1799+
),
1800+
pytest.param(
1801+
"mode = 0o777 & ~umask | 0o777",
1802+
"fs.py",
1803+
"python",
1804+
"mode = 0o777",
1805+
id="bits_added_after_umask_mask",
1806+
),
1807+
pytest.param(
1808+
"mode = 0o777 & ~0", "fs.py", "python", "mode = 0o777", id="mask_is_not_the_umask"
1809+
),
1810+
pytest.param("umask 000", "run.sh", "shell", "umask 000", id="shell_umask_000"),
1811+
pytest.param("UMASK=0000", "env.sh", "shell", "UMASK=0000", id="env_umask_0000"),
1812+
pytest.param("umask = 0o000", "cfg.py", "python", "umask = 0o000", id="assign_0o000"),
1813+
pytest.param("umask 004", "run.sh", "shell", "umask 004", id="world_write_unmasked"),
1814+
pytest.param(
1815+
"umask=0o7770", "cfg.py", "python", "umask=0o7770", id="long_value_ending_in_0"
1816+
),
1817+
pytest.param("os.umask(0)", "fs.py", "python", "os.umask(0)", id="py_umask_call"),
1818+
pytest.param(
1819+
"process.umask(0o000);", "fs.js", "javascript", "process.umask(0o000)", id="js_call"
1820+
),
1821+
pytest.param("umask(0);", "main.c", "c", "umask(0)", id="c_umask_call"),
1822+
pytest.param(
1823+
"fd = os.open(p, flags, mode=0o666 & ~os.umask(0))",
1824+
"fs.py",
1825+
"python",
1826+
"os.umask(0)",
1827+
id="umask_zero_inside_mask_expression",
1828+
),
1829+
],
1830+
)
1831+
def test_tm3_world_writable_mode_or_umask_detected(
1832+
self, content: str, filename: str, filetype: str, expected: str
1833+
) -> None:
1834+
"""A world-writable mode, or a umask that leaves world-write unmasked, is TM3."""
1835+
findings = tm_mod.analyze(content, filename, filetype)
1836+
assert [f.matched_text for f in findings if f.rule_id == "TM3"] == [expected]
1837+
1838+
@pytest.mark.parametrize(
1839+
"content,filename,filetype",
1840+
[
1841+
pytest.param("mode = 0o666 & ~umask", "fs.py", "python", id="mode_masked_by_umask"),
1842+
pytest.param(
1843+
"os.chmod(path, mode=0o777 & ~current_umask)",
1844+
"fs.py",
1845+
"python",
1846+
id="mode_masked_in_call",
1847+
),
1848+
pytest.param(
1849+
"const mode = 0o666 & ~process.umask();",
1850+
"fs.js",
1851+
"javascript",
1852+
id="mode_masked_by_process_umask",
1853+
),
1854+
pytest.param("umask = 0o777", "cfg.py", "python", id="most_restrictive_umask"),
1855+
pytest.param("UMASK=0777", "env.sh", "shell", id="restrictive_env_umask"),
1856+
pytest.param("umask 077", "run.sh", "shell", id="umask_077"),
1857+
pytest.param("umask 022", "run.sh", "shell", id="umask_022"),
1858+
pytest.param("os.umask(0o022)", "fs.py", "python", id="umask_call_022"),
1859+
pytest.param("os.umask(18)", "fs.py", "python", id="decimal_umask_call_022"),
1860+
pytest.param(
1861+
"umask = os.umask(0)\nos.umask(umask)\nos.chmod(tmp, 0o666 & ~umask)",
1862+
"fs.py",
1863+
"python",
1864+
id="read_and_restore_umask",
1865+
),
1866+
pytest.param(
1867+
"const previous = process.umask(0);", "fs.js", "javascript", id="js_read_umask"
1868+
),
1869+
],
1870+
)
1871+
def test_tm3_umask_masked_mode_or_restrictive_umask_not_flagged(
1872+
self, content: str, filename: str, filetype: str
1873+
) -> None:
1874+
"""A mode masked with the umask and a restrictive umask are not unsafe defaults."""
1875+
findings = tm_mod.analyze(content, filename, filetype)
1876+
assert not [f.matched_text for f in findings if f.rule_id == "TM3"]
1877+
1878+
@pytest.mark.parametrize(
1879+
"content",
1880+
[
1881+
pytest.param("mode=0o777 & ~" + "a" * 50_000, id="long_mask_operand"),
1882+
pytest.param("mode=0o777 &" + " " * 50_000 + "~umask |", id="long_gap_before_mask"),
1883+
pytest.param("mode=0o777 & ~os.umask(" + "0" * 50_000, id="unclosed_mask_call"),
1884+
pytest.param("umask " + "0" * 50_000 + "2", id="long_umask_value"),
1885+
pytest.param("umask(0" + "0" * 50_000, id="unclosed_umask_call"),
1886+
pytest.param("a." * 25_000 + "umask(", id="long_dotted_name"),
1887+
pytest.param("umask umask " * 4_000, id="repeated_umask_words"),
1888+
],
1889+
)
1890+
def test_tm3_permission_patterns_are_linear(self, content: str) -> None:
1891+
started = time.perf_counter()
1892+
tm_mod.analyze(content, "fs.py", "python")
1893+
assert time.perf_counter() - started < 1.0
1894+
17931895
@pytest.mark.parametrize(
17941896
"content,filename,filetype",
17951897
[

0 commit comments

Comments
 (0)