Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions trytond/doc/topics/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,24 @@ The name of the similarity function.

Default: ``similarity``

binary_scanner
~~~~~~~~~~~~~~

The command used to scan the content of Binary fields.
The command receive the quarantine directory as last argument.
If the command returns a non zero status, it is considered that at least one
file in the directory is malicious.

binary_scanner_directory
~~~~~~~~~~~~~~~~~~~~~~~~

The directory where the binary content are stored before being checked
by the scanner.

Default: The temporary directory as determined by Python's tempfile_ module.

.. _tempfile: https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir

.. _config-request:

request
Expand Down
3 changes: 3 additions & 0 deletions trytond/trytond/ir/message.xml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_trigger_invalid_condition">
<field name="text">Condition "%(condition)s" is not a valid PYSON expression for trigger "%(trigger)s".</field>
</record>
<record model="ir.message" id="msg_malicious_binary">
<field name="text">The content of field "%(field)s" has been detected as malicious.</field>
</record>
<record model="ir.message" id="msg_html_editor_save_fail">
<field name="text">Failed to save, please retry.</field>
</record>
Expand Down
42 changes: 42 additions & 0 deletions trytond/trytond/model/fields/binary.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import logging
import shlex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je connaissais pas ce module :o

import subprocess
import tempfile

from sql import Column, Null

from trytond.config import config
from trytond.filestore import filestore
from trytond.i18n import gettext
from trytond.tools import cached_property, grouped_slice, reduce_ids
from trytond.transaction import Transaction

from .field import Field

logger = logging.getLogger(__name__)


def caster(d):
if isinstance(d, bytes):
Expand All @@ -17,6 +26,32 @@ def caster(d):
return bytes(d, encoding='utf8')


def check_content(field_name, *binaries):
from trytond.model.modelstorage import BinaryScanError

scanner = config.get('database', 'binary_scanner')
scanner_dir = config.get(
'database', 'binary_scanner_directory', default=tempfile.gettempdir())
if not scanner:
return

with tempfile.TemporaryDirectory(dir=scanner_dir) as tempdir:
for binary in binaries:
with tempfile.NamedTemporaryFile(dir=tempdir, delete=False) as fd:
fd.write(binary)

try:
subprocess.check_call(
shlex.split(scanner) + [tempdir],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except subprocess.CalledProcessError as error:
logger.critical(
"'%s %s' exited with code '%s'",
scanner, tempdir, error.returncode)
raise BinaryScanError(gettext(
'ir.msg_malicious_binary', field=field_name))


class Binary(Field):
_type = 'binary'
_sql_type = 'BLOB'
Expand Down Expand Up @@ -112,6 +147,9 @@ def set(self, Model, name, ids, value, *args):
if prefix is None:
prefix = transaction.database.name

self._check_contents(
Model.__names__(name)['field'], *((ids, value) + args)[1::2])

args = iter((ids, value) + args)
for ids, value in zip(args, args):
if self.file_id:
Expand All @@ -124,6 +162,10 @@ def set(self, Model, name, ids, value, *args):
cursor.execute(*table.update(columns, values,
where=reduce_ids(table.id, ids)))

@classmethod
def _check_contents(cls, field, *values):
check_content(field, *values)

def definition(self, model, language):
definition = super().definition(model, language)
definition['searchable'] = False
Expand Down
4 changes: 4 additions & 0 deletions trytond/trytond/model/modelstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def __init__(self, record):
self.record = record


class BinaryScanError(ValidationError):
pass


def is_leaf(expression):
return (isinstance(expression, (list, tuple))
and len(expression) > 2
Expand Down