Skip to main content

rbatis_plus_extension/inner/
block_attack.rs

1// Source: mybatis-plus-jsqlparser-5.0/.../inner/BlockAttackInnerInterceptor.java
2
3use crate::inner::inner_interceptor::InnerInterceptor;
4use async_trait::async_trait;
5use rbatis::executor::Executor;
6use rbatis::{Action, Error};
7use rbdc::db::ExecResult;
8use rbs::Value;
9
10/// Block-attack inner interceptor.
11///
12/// Mirrors Java `BlockAttackInnerInterceptor`.
13///
14/// Prevents full-table UPDATE or DELETE operations (no WHERE clause).
15/// Throws an error if an UPDATE/DELETE SQL has no WHERE condition.
16#[derive(Debug, Clone, Default)]
17pub struct BlockAttackInnerInterceptor;
18
19impl BlockAttackInnerInterceptor {
20    pub fn new() -> Self {
21        Self
22    }
23
24    fn has_where(sql: &str) -> bool {
25        let lower = sql.trim_start().to_lowercase();
26        lower.contains(" where ")
27    }
28}
29
30#[async_trait]
31impl InnerInterceptor for BlockAttackInnerInterceptor {
32    async fn before_update(
33        &self,
34        _executor: &dyn Executor,
35        sql: &mut String,
36        _args: &mut Vec<Value>,
37        _result: &mut Result<ExecResult, Error>,
38    ) -> Result<Action, Error> {
39        let lower = sql.trim_start().to_lowercase();
40        let is_update_or_delete =
41            lower.starts_with("update ") || lower.starts_with("delete from");
42
43        if is_update_or_delete && !Self::has_where(sql) {
44            return Err(Error::from(
45                "[rbatis-plus] Prohibition of full table update/delete operation",
46            ));
47        }
48        Ok(Action::Next)
49    }
50}