From 871e3f53aefeca84927628697467d0fb38b07e7f Mon Sep 17 00:00:00 2001 From: Abood059 Date: Sat, 14 Nov 2026 17:25:46 +0200 Subject: [PATCH 1/2] Fix yara.compile(file=fp) reading from raw file descriptor offset Read rules via Python stream's read() method instead of extracting and duplicating the raw OS file descriptor. This respects Python stream semantics and logical position (tell()), prevents silent rule loss on advanced file pointers, and enables in-memory streams such as io.BytesIO. --- tests.py | 2 +- tests/test_compile.py | 72 +++++++++++++++++++++++++++++++++++++++++++ yara-python.c | 43 +++++++++++++++++++------- 3 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 tests/test_compile.py diff --git a/tests.py b/tests.py index 3a694bd..e34717c 100644 --- a/tests.py +++ b/tests.py @@ -843,7 +843,7 @@ def testTooManyArguments(self): def testCompileFile(self): - f = tempfile.TemporaryFile('wt') + f = tempfile.TemporaryFile('w+t') f.write('rule test { condition: true }') f.flush() diff --git a/tests/test_compile.py b/tests/test_compile.py new file mode 100644 index 0000000..78fd6c9 --- /dev/null +++ b/tests/test_compile.py @@ -0,0 +1,72 @@ +# +# Copyright (c) 2007-2026. The YARA Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import io +import os +import sys + +# Ensure repository root is on sys.path for local in-place extension loading +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) + +import pytest +import yara + + +def test_compile_from_advanced_file_object_small(tmp_path): + """Rules should not be lost when the file object was read from before compile().""" + rule_file = tmp_path / "rules.yar" + rule_file.write_text( + "// header\n" + "rule rule_alpha { condition: true }\n" + "rule rule_beta { condition: true }\n" + ) + + with open(rule_file, "r") as fp: + fp.readline() + rules = yara.compile(file=fp) + + identifiers = [r.identifier for r in rules] + assert identifiers == ["rule_alpha", "rule_beta"], ( + f"expected all rules to compile; got {identifiers}" + ) + + +def test_compile_from_advanced_file_object_large(tmp_path): + """Partial rule loss should not occur for files larger than the buffer size.""" + header = "// header\n" + rule_a = "rule rule_alpha { condition: true }\n" + padding_len = 8192 - len(header) - len(rule_a) + padding = " " * padding_len + rule_b = "rule rule_beta { condition: true }\n" + rule_file = tmp_path / "rules.yar" + rule_file.write_text(header + rule_a + padding + rule_b) + + with open(rule_file, "r") as fp: + fp.readline() + rules = yara.compile(file=fp) + + identifiers = [r.identifier for r in rules] + assert identifiers == ["rule_alpha", "rule_beta"], ( + f"expected all rules to compile; got {identifiers}" + ) + + +def test_compile_from_bytesio(): + """file= should accept any object with a .read() method.""" + import io + source = b"rule r { condition: true }" + rules = yara.compile(file=io.BytesIO(source)) + assert [r.identifier for r in rules] == ["r"] diff --git a/yara-python.c b/yara-python.c index 25566e0..d86e07b 100644 --- a/yara-python.c +++ b/yara-python.c @@ -2679,7 +2679,6 @@ static PyObject* yara_compile( Py_ssize_t pos = 0; - int fd; int error = 0; char* filepath = NULL; @@ -2845,21 +2844,43 @@ static PyObject* yara_compile( } else if (file != NULL) { - fd = PyObject_AsFileDescriptor(file); + PyObject* read_result = PyObject_CallMethod(file, "read", NULL); - if (fd != -1) + if (read_result != NULL) { - Py_BEGIN_ALLOW_THREADS - fh = fdopen(dup(fd), "r"); - error = yr_compiler_add_file(compiler, fh, NULL, NULL); - fclose(fh); - Py_END_ALLOW_THREADS + const char* buffer = NULL; + Py_ssize_t size = 0; + + if (PyUnicode_Check(read_result)) + { + buffer = PyUnicode_AsUTF8AndSize(read_result, &size); + } + else if (PyBytes_Check(read_result)) + { + size = PyBytes_GET_SIZE(read_result); + buffer = PyBytes_AS_STRING(read_result); + } + else + { + result = PyErr_Format( + PyExc_TypeError, + "'file' read() must return string or bytes"); + } + + (void) size; + + if (buffer != NULL) + { + Py_BEGIN_ALLOW_THREADS + error = yr_compiler_add_string(compiler, buffer, NULL); + Py_END_ALLOW_THREADS + } + + Py_DECREF(read_result); } else { - result = PyErr_Format( - PyExc_TypeError, - "'file' is not a file object"); + result = NULL; } } else if (sources_dict != NULL) From 88f447906f6b1d2ab0606b53b00583049b17e139 Mon Sep 17 00:00:00 2001 From: Abood059 Date: Sat, 14 Nov 2026 17:53:18 +0200 Subject: [PATCH 2/2] Remove unnecessary test path setup --- tests/test_compile.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/test_compile.py b/tests/test_compile.py index 78fd6c9..7a7a06c 100644 --- a/tests/test_compile.py +++ b/tests/test_compile.py @@ -15,11 +15,6 @@ # import io -import os -import sys - -# Ensure repository root is on sys.path for local in-place extension loading -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) import pytest import yara