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.

272 lines
9.3 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. query = self.env.cr.mogrify(self.query, params).decode('utf-8')
  129. if mode in ('fetchone', 'fetchall'):
  130. pass
  131. elif mode == 'stdout':
  132. query = "COPY (%s) TO STDOUT WITH %s" % (query, copy_options)
  133. elif mode in 'view':
  134. query = "CREATE VIEW %s AS (%s);" % (query, view_name)
  135. elif mode in 'materialized_view':
  136. self._check_materialized_view_available()
  137. query = "CREATE MATERIALIZED VIEW %s AS (%s);" % (query, view_name)
  138. else:
  139. raise UserError(_("Unimplemented mode : '%s'" % mode))
  140. if rollback:
  141. rollback_name = self._create_savepoint()
  142. try:
  143. if mode == 'stdout':
  144. output = BytesIO()
  145. self.env.cr.copy_expert(query, output)
  146. res = base64.b64encode(output.getvalue())
  147. output.close()
  148. else:
  149. self.env.cr.execute(query)
  150. if mode == 'fetchall':
  151. res = self.env.cr.fetchall()
  152. if header:
  153. colnames = [
  154. desc[0] for desc in self.env.cr.description
  155. ]
  156. res.insert(0, colnames)
  157. elif mode == 'fetchone':
  158. res = self.env.cr.fetchone()
  159. finally:
  160. self._rollback_savepoint(rollback_name)
  161. return res
  162. # Private Section
  163. @api.model
  164. def _create_savepoint(self):
  165. rollback_name = '%s_%s' % (
  166. self._name.replace('.', '_'), uuid.uuid1().hex)
  167. # pylint: disable=sql-injection
  168. req = "SAVEPOINT %s" % (rollback_name)
  169. self.env.cr.execute(req)
  170. return rollback_name
  171. @api.model
  172. def _rollback_savepoint(self, rollback_name):
  173. # pylint: disable=sql-injection
  174. req = "ROLLBACK TO SAVEPOINT %s" % (rollback_name)
  175. self.env.cr.execute(req)
  176. @api.model
  177. def _check_materialized_view_available(self):
  178. self.env.cr.execute("SHOW server_version;")
  179. res = self.env.cr.fetchone()[0].split('.')
  180. minor_version = float('.'.join(res[:2]))
  181. if minor_version < 9.3:
  182. raise UserError(_(
  183. "Materialized View requires PostgreSQL 9.3 or greater but"
  184. " PostgreSQL %s is currently installed.") % (minor_version))
  185. @api.multi
  186. def _clean_query(self):
  187. self.ensure_one()
  188. query = self.query.strip()
  189. while query[-1] == ';':
  190. query = query[:-1]
  191. self.query = query
  192. @api.multi
  193. def _check_prohibited_words(self):
  194. """Check if the query contains prohibited words, to avoid maliscious
  195. SQL requests"""
  196. self.ensure_one()
  197. query = self.query.lower()
  198. for word in self.PROHIBITED_WORDS:
  199. expr = r'\b%s\b' % word
  200. is_not_safe = re.search(expr, query)
  201. if is_not_safe:
  202. raise UserError(_(
  203. "The query is not allowed because it contains unsafe word"
  204. " '%s'") % (word))
  205. @api.multi
  206. def _check_execution(self):
  207. """Ensure that the query is valid, trying to execute it. A rollback
  208. is done after."""
  209. self.ensure_one()
  210. query = self._prepare_request_check_execution()
  211. rollback_name = self._create_savepoint()
  212. res = False
  213. try:
  214. self.env.cr.execute(query)
  215. res = self._hook_executed_request()
  216. except ProgrammingError as e:
  217. logger.exception("Failed query: %s", query)
  218. raise UserError(
  219. _("The SQL query is not valid:\n\n %s") % e)
  220. finally:
  221. self._rollback_savepoint(rollback_name)
  222. return res
  223. @api.multi
  224. def _prepare_request_check_execution(self):
  225. """Overload me to replace some part of the query, if it contains
  226. parameters"""
  227. self.ensure_one()
  228. return self.query
  229. def _hook_executed_request(self):
  230. """Overload me to insert custom code, when the SQL request has
  231. been executed, before the rollback.
  232. """
  233. self.ensure_one()
  234. return False