Bez popisu

common.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. import os
  19. from datetime import datetime
  20. from jinja2.sandbox import SandboxedEnvironment
  21. from werkzeug.utils import secure_filename
  22. from app import app
  23. def build_upload_path(case_customer, case_name, module, create=False):
  24. """
  25. Create a path for the upload of the files, according to the specifications of the case
  26. :param case_customer: Customer name linked to the case
  27. :param case_name: Name of the case
  28. :param module: Name of the module which will handle the data
  29. :param create: True if the path needs to be created, else false
  30. :return: The built full path, None if errors
  31. """
  32. try:
  33. if case_name and case_customer and module:
  34. path = "{customer}/{case}/{module}/".format(
  35. customer=case_customer.strip().replace('.', '').replace(' ', '').replace('/', ''),
  36. case=case_name.strip().replace('.', '').replace(' ', '_').replace('/', '').lower(),
  37. module=module.replace('.', '').replace(' ', '_').replace('/', '')
  38. )
  39. path = secure_filename(path)
  40. fpath = os.path.join(app.config['UPLOADED_PATH'], path)
  41. if create:
  42. os.makedirs(os.path.join(app.config['UPLOADED_PATH'], path), exist_ok=True)
  43. return fpath
  44. return None
  45. except Exception as e:
  46. print(e)
  47. return None
  48. def parse_bf_date_format(input_str):
  49. date_value = input_str.strip()
  50. if len(date_value) == 10 and '-' not in date_value and '.' not in date_value and '/' not in date_value:
  51. # Assume linux timestamp, from 1966 to 2286
  52. date = datetime.fromtimestamp(int(date_value))
  53. return date
  54. elif len(date_value) == 13 and '-' not in date_value and '.' not in date_value and '/' not in date_value:
  55. # Assume microsecond timestamp
  56. date = datetime.fromtimestamp(int(date_value) / 1000)
  57. return date
  58. else:
  59. # brute force formats
  60. for fmt in ('%Y-%m-%d', '%Y-%m-%d %H:%M', '%Y-%m-%d %H:%M:%S', '%Y-%m-%d %H:%M:%S.%f',
  61. '%Y-%m-%d %H:%M%z', '%Y-%m-%d %H:%M:%S%z', '%Y-%m-%d %H:%M:%S.%f%z',
  62. '%Y-%m-%d %H:%M %Z', '%Y-%m-%d %H:%M:%S %Z', '%Y-%m-%d %H:%M:%S.%f %Z',
  63. '%Y-%m-%d - %H:%M:%S.%f%z',
  64. '%b %d %H:%M:%S', '%Y %b %d %H:%M:%S', '%b %d %H:%M:%S %Y', '%b %d %Y %H:%M:%S',
  65. '%y %b %d %H:%M:%S', '%b %d %H:%M:%S %y', '%b %d %y %H:%M:%S',
  66. '%Y-%m-%d', '%Y-%m-%dT%H:%M', '%Y-%m-%dT%H:%M:%S', '%Y-%m-%dT%H:%M:%S.%f',
  67. '%Y-%m-%dT%H:%M%z', '%Y-%m-%dT%H:%M:%S%z', '%Y-%m-%dT%H:%M:%S.%f%z',
  68. '%Y-%m-%dT%H:%M %Z', '%Y-%m-%dT%H:%M:%S %Z', '%Y-%m-%dT%H:%M:%S.%f %Z',
  69. '%Y-%d-%m', '%Y-%d-%m %H:%M', '%Y-%d-%m %H:%M:%S', '%Y-%d-%m %H:%M:%S.%f',
  70. '%Y-%d-%m %H:%M%z', '%Y-%d-%m %H:%M:%S%z', '%Y-%d-%m %H:%M:%S.%f%z',
  71. '%Y-%d-%m %H:%M %Z', '%Y-%d-%m %H:%M:%S %Z', '%Y-%d-%m %H:%M:%S.%f %Z',
  72. '%d/%m/%Y %H:%M', '%d/%m/%Y %H:%M:%S', '%d/%m/%Y %H:%M:%S.%f',
  73. '%d.%m.%Y %H:%M', '%d.%m.%Y %H:%M:%S', '%d.%m.%Y %H:%M:%S.%f',
  74. '%d-%m-%Y %H:%M', '%d-%m-%Y %H:%M:%S', '%d-%m-%Y %H:%M:%S.%f',
  75. '%b %d %Y %H:%M', '%b %d %Y %H:%M:%S', '%b %d %Y %H:%M:%S',
  76. '%a, %d %b %Y %H:%M:%S', '%a, %d %b %Y %H:%M:%S %Z', '%a, %d %b %Y %H:%M:%S.%f',
  77. '%a, %d %b %y %H:%M:%S', '%a, %d %b %y %H:%M:%S %Z', '%a, %d %b %y %H:%M:%S.%f',
  78. '%d %b %Y %H:%M', '%d %b %Y %H:%M:%S', '%d %b %Y %H:%M:%S.%f',
  79. '%d %b %y %H:%M', '%d %b %y %H:%M:%S', '%d %b %y %H:%M:%S.%f',
  80. '%Y-%m-%d', '%d.%m.%Y', '%d/%m/%Y', "%A, %B %d, %Y", "%A %B %d, %Y", "%A %B %d %Y",
  81. '%d %B %Y'):
  82. try:
  83. date = datetime.strptime(date_value, fmt)
  84. return date
  85. except ValueError:
  86. pass
  87. return None
  88. class IrisJinjaEnv(SandboxedEnvironment):
  89. def is_safe_attribute(self, obj, attr, value):
  90. # Extend the list of blocked attributes with magic methods and other potential unsafe attributes
  91. unsafe_attributes = [
  92. 'os', 'subprocess', 'eval', 'exec', 'open', 'input', '__import__',
  93. '__class__', '__bases__', '__mro__', '__subclasses__', '__globals__'
  94. ]
  95. # Block access to all attributes starting and ending with double underscores
  96. if attr in unsafe_attributes or attr.startswith('__') and attr.endswith('__'):
  97. return False
  98. return super().is_safe_attribute(obj, attr, value)
  99. def call(self, obj, *args, **kwargs):
  100. # Block calling of functions if necessary
  101. # For example, block if obj is a built-in function or method
  102. if isinstance(obj, (type,)):
  103. raise Exception("Calling of built-in types is not allowed.")
  104. return super().call(obj, *args, **kwargs)