You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.4 KiB

  1. # Copyright (C) 2015 Akretion (<http://www.akretion.com>)
  2. # Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
  3. # @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import re
  6. import uuid
  7. import logging
  8. from io import BytesIO
  9. import base64
  10. from psycopg2 import ProgrammingError
  11. from odoo import _, api, fields, models
  12. from odoo.exceptions import UserError
  13. logger = logging.getLogger(__name__)
  14. class SQLRequestMixin(models.AbstractModel):
  15. _name = 'sql.request.mixin'
  16. _description = 'SQL Request Mixin'
  17. _clean_query_enabled = True
  18. _check_prohibited_words_enabled = True
  19. _check_execution_enabled = True
  20. _sql_request_groups_relation = False
  21. _sql_request_users_relation = False
  22. STATE_SELECTION = [
  23. ('draft', 'Draft'),
  24. ('sql_valid', 'SQL Valid'),
  25. ]
  26. PROHIBITED_WORDS = [
  27. 'delete',
  28. 'drop',
  29. 'insert',
  30. 'alter',
  31. 'truncate',
  32. 'execute',
  33. 'create',
  34. 'update',
  35. 'ir_config_parameter',
  36. ]
  37. # Default Section
  38. @api.model
  39. def _default_group_ids(self):
  40. ir_model_obj = self.env['ir.model.data']
  41. return [ir_model_obj.xmlid_to_res_id(
  42. 'sql_request_abstract.group_sql_request_user')]
  43. @api.model
  44. def _default_user_ids(self):
  45. return []
  46. # Columns Section
  47. name = fields.Char('Name', required=True)
  48. query = fields.Text(
  49. string='Query', required=True, help="You can't use the following words"
  50. ": DELETE, DROP, CREATE, INSERT, ALTER, TRUNCATE, EXECUTE, UPDATE.")
  51. state = fields.Selection(
  52. string='State', selection=STATE_SELECTION, default='draft',
  53. help="State of the Request:\n"
  54. " * 'Draft': Not tested\n"
  55. " * 'SQL Valid': SQL Request has been checked and is valid")
  56. group_ids = fields.Many2many(
  57. comodel_name='res.groups', string='Allowed Groups',
  58. relation=_sql_request_groups_relation,
  59. column1='sql_id', column2='group_id',
  60. default=_default_group_ids)
  61. user_ids = fields.Many2many(
  62. comodel_name='res.users', string='Allowed Users',
  63. relation=_sql_request_users_relation,
  64. column1='sql_id', column2='user_id',
  65. default=_default_user_ids)
  66. # Action Section
  67. @api.multi
  68. def button_validate_sql_expression(self):
  69. for item in self:
  70. if item._clean_query_enabled:
  71. item._clean_query()
  72. if item._check_prohibited_words_enabled:
  73. item._check_prohibited_words()
  74. if item._check_execution_enabled:
  75. item._check_execution()
  76. item.state = 'sql_valid'
  77. return True
  78. @api.multi
  79. def button_set_draft(self):
  80. self.write({'state': 'draft'})
  81. # API Section
  82. @api.multi
  83. def _execute_sql_request(
  84. self, params=None, mode='fetchall', rollback=True,
  85. view_name=False, copy_options="CSV HEADER DELIMITER ';'",
  86. header=False):
  87. """Execute a SQL request on the current database.
  88. ??? This function checks before if the user has the
  89. right to execute the request.
  90. :param params: (dict) of keys / values that will be replaced in
  91. the sql query, before executing it.
  92. :param mode: (str) result type expected. Available settings :
  93. * 'view': create a view with the select query. Extra param
  94. required 'view_name'.
  95. * 'materialized_view': create a MATERIALIZED VIEW with the
  96. select query. Extra parameter required 'view_name'.
  97. * 'fetchall': execute the select request, and return the
  98. result of 'cr.fetchall()'.
  99. * 'fetchone' : execute the select request, and return the
  100. result of 'cr.fetchone()'
  101. :param rollback: (boolean) mention if a rollback should be played after
  102. the execution of the query. Please keep this feature enabled
  103. for security reason, except if necessary.
  104. (Ignored if @mode in ('view', 'materialized_view'))
  105. :param view_name: (str) name of the view.
  106. (Ignored if @mode not in ('view', 'materialized_view'))
  107. :param copy_options: (str) mentions extra options for
  108. "COPY request STDOUT WITH xxx" request.
  109. (Ignored if @mode != 'stdout')
  110. :param header: (boolean) if true, the header of the query will be
  111. returned as first element of the list if the mode is fetchall.
  112. (Ignored if @mode != fetchall)
  113. ..note:: The following exceptions could be raised:
  114. psycopg2.ProgrammingError: Error in the SQL Request.
  115. odoo.exceptions.UserError:
  116. * 'mode' is not implemented.
  117. * materialized view is not supported by the Postgresql Server.
  118. """
  119. self.ensure_one()
  120. res = False
  121. # Check if the request is in a valid state
  122. if self.state == 'draft':
  123. raise UserError(_(
  124. "It is not allowed to execute a not checked request."))
  125. # Disable rollback if a creation of a view is asked
  126. if mode in ('view', 'materialized_view'):
  127. rollback = False
  128. # pylint: disable=sql-injection
  129. if params:
  130. query = self.query % params
  131. else:
  132. query = self.query
  133. query = query
  134. if mode in ('fetchone', 'fetchall'):
  135. pass
  136. elif mode == 'stdout':
  137. query = "COPY (%s) TO STDOUT WITH %s" % (query, copy_options)
  138. elif mode in 'view':
  139. query = "CREATE VIEW %s AS (%s);" % (query, view_name)
  140. elif mode in 'materialized_view':
  141. self._check_materialized_view_available()
  142. query = "CREATE MATERIALIZED VIEW %s AS (%s);" % (query, view_name)
  143. else:
  144. raise UserError(_("Unimplemented mode : '%s'" % mode))
  145. if rollback:
  146. rollback_name = self._create_savepoint()
  147. try:
  148. if mode == 'stdout':
  149. output = BytesIO()
  150. self.env.cr.copy_expert(query, output)
  151. res = base64.b64encode(output.getvalue())
  152. output.close()
  153. else:
  154. self.env.cr.execute(query)
  155. if mode == 'fetchall':
  156. res = self.env.cr.fetchall()
  157. if header:
  158. colnames = [
  159. desc[0] for desc in self.env.cr.description
  160. ]
  161. res.insert(0, colnames)
  162. elif mode == 'fetchone':
  163. res = self.env.cr.fetchone()
  164. finally:
  165. self._rollback_savepoint(rollback_name)
  166. return res
  167. # Private Section
  168. @api.model
  169. def _create_savepoint(self):
  170. rollback_name = '%s_%s' % (
  171. self._name.replace('.', '_'), uuid.uuid1().hex)
  172. # pylint: disable=sql-injection
  173. req = "SAVEPOINT %s" % (rollback_name)
  174. self.env.cr.execute(req)
  175. return rollback_name
  176. @api.model
  177. def _rollback_savepoint(self, rollback_name):
  178. # pylint: disable=sql-injection
  179. req = "ROLLBACK TO SAVEPOINT %s" % (rollback_name)
  180. self.env.cr.execute(req)
  181. @api.model
  182. def _check_materialized_view_available(self):
  183. self.env.cr.execute("SHOW server_version;")
  184. res = self.env.cr.fetchone()[0].split('.')
  185. minor_version = float('.'.join(res[:2]))
  186. if minor_version < 9.3:
  187. raise UserError(_(
  188. "Materialized View requires PostgreSQL 9.3 or greater but"
  189. " PostgreSQL %s is currently installed.") % (minor_version))
  190. @api.multi
  191. def _clean_query(self):
  192. self.ensure_one()
  193. query = self.query.strip()
  194. while query[-1] == ';':
  195. query = query[:-1]
  196. self.query = query
  197. @api.multi
  198. def _check_prohibited_words(self):
  199. """Check if the query contains prohibited words, to avoid maliscious
  200. SQL requests"""
  201. self.ensure_one()
  202. query = self.query.lower()
  203. for word in self.PROHIBITED_WORDS:
  204. expr = r'\b%s\b' % word
  205. is_not_safe = re.search(expr, query)
  206. if is_not_safe:
  207. raise UserError(_(
  208. "The query is not allowed because it contains unsafe word"
  209. " '%s'") % (word))
  210. @api.multi
  211. def _check_execution(self):
  212. """Ensure that the query is valid, trying to execute it. A rollback
  213. is done after."""
  214. self.ensure_one()
  215. query = self._prepare_request_check_execution()
  216. rollback_name = self._create_savepoint()
  217. res = False
  218. try:
  219. self.env.cr.execute(query)
  220. res = self._hook_executed_request()
  221. except ProgrammingError as e:
  222. logger.exception("Failed query: %s", query)
  223. raise UserError(
  224. _("The SQL query is not valid:\n\n %s") % e)
  225. finally:
  226. self._rollback_savepoint(rollback_name)
  227. return res
  228. @api.multi
  229. def _prepare_request_check_execution(self):
  230. """Overload me to replace some part of the query, if it contains
  231. parameters"""
  232. self.ensure_one()
  233. return self.query
  234. def _hook_executed_request(self):
  235. """Overload me to insert custom code, when the SQL request has
  236. been executed, before the rollback.
  237. """
  238. self.ensure_one()
  239. return False