Search in sources :

Example 1 with NotAllowCommentException

use of com.alibaba.druid.sql.parser.NotAllowCommentException in project druid by alibaba.

the class MySqlLexer method scanComment.

public void scanComment() {
    Token lastToken = this.token;
    if (ch == '-') {
        char next_2 = charAt(pos + 2);
        if (isDigit(next_2)) {
            scanChar();
            token = Token.SUB;
            return;
        }
    } else if (ch != '/') {
        throw new IllegalStateException();
    }
    mark = pos;
    bufPos = 0;
    scanChar();
    // /*+ */
    if (ch == '*') {
        scanChar();
        bufPos++;
        while (ch == ' ') {
            scanChar();
            bufPos++;
        }
        boolean isHint = false;
        int startHintSp = bufPos + 1;
        if (//
        ch == '!' || // oceanbase hints
        ch == '+') {
            isHint = true;
            scanChar();
            bufPos++;
        }
        for (; ; ) {
            if (ch == EOI) {
                this.token = Token.ERROR;
                return;
            }
            if (ch == '*' && charAt(pos + 1) == '/') {
                bufPos += 3;
                scanChar();
                scanChar();
                break;
            }
            scanChar();
            bufPos++;
        }
        if (isHint) {
            stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 2);
            token = Token.HINT;
        } else {
            stringVal = subString(mark, bufPos);
            token = Token.MULTI_LINE_COMMENT;
            commentCount++;
            if (keepComments) {
                addComment(stringVal);
            }
        }
        endOfComment = isEOF();
        if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
            return;
        }
        if (!isHint && !isAllowComment() && !isSafeComment(stringVal)) {
            throw new NotAllowCommentException();
        }
        return;
    }
    if (ch == '/' || ch == '-') {
        scanChar();
        bufPos++;
        for (; ; ) {
            if (ch == '\r') {
                if (charAt(pos + 1) == '\n') {
                    bufPos += 2;
                    scanChar();
                    break;
                }
                bufPos++;
                break;
            } else if (ch == EOI) {
                break;
            }
            if (ch == '\n') {
                scanChar();
                bufPos++;
                break;
            }
            scanChar();
            bufPos++;
        }
        stringVal = subString(mark, bufPos + 1);
        token = Token.LINE_COMMENT;
        commentCount++;
        if (keepComments) {
            addComment(stringVal);
        }
        if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
            return;
        }
        endOfComment = isEOF();
        if (!isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
            throw new NotAllowCommentException();
        }
        return;
    }
}
Also used : NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException) Token(com.alibaba.druid.sql.parser.Token)

Example 2 with NotAllowCommentException

use of com.alibaba.druid.sql.parser.NotAllowCommentException in project druid by alibaba.

the class MySqlLexer method scanSharp.

public void scanSharp() {
    if (ch != '#') {
        throw new ParserException("illegal stat");
    }
    if (charAt(pos + 1) == '{') {
        scanVariable();
        return;
    }
    Token lastToken = this.token;
    scanChar();
    mark = pos;
    bufPos = 0;
    for (; ; ) {
        if (ch == '\r') {
            if (charAt(pos + 1) == '\n') {
                bufPos += 2;
                scanChar();
                break;
            }
            bufPos++;
            break;
        } else if (ch == EOI) {
            break;
        }
        if (ch == '\n') {
            scanChar();
            bufPos++;
            break;
        }
        scanChar();
        bufPos++;
    }
    stringVal = subString(mark - 1, bufPos + 1);
    token = Token.LINE_COMMENT;
    commentCount++;
    if (keepComments) {
        addComment(stringVal);
    }
    if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
        return;
    }
    endOfComment = isEOF();
    if (!isAllowComment() && (isEOF() || !isSafeComment(stringVal))) {
        throw new NotAllowCommentException();
    }
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException) Token(com.alibaba.druid.sql.parser.Token)

Example 3 with NotAllowCommentException

use of com.alibaba.druid.sql.parser.NotAllowCommentException in project druid by alibaba.

the class OdpsLexer method scanComment.

public void scanComment() {
    if (ch != '/' && ch != '-') {
        throw new IllegalStateException();
    }
    Token lastToken = this.token;
    mark = pos;
    bufPos = 0;
    scanChar();
    // /*+ */
    if (ch == '*') {
        scanChar();
        bufPos++;
        while (ch == ' ') {
            scanChar();
            bufPos++;
        }
        boolean isHint = false;
        int startHintSp = bufPos + 1;
        if (ch == '+') {
            isHint = true;
            scanChar();
            bufPos++;
        }
        for (; ; ) {
            if (ch == '*' && charAt(pos + 1) == '/') {
                bufPos += 2;
                scanChar();
                scanChar();
                break;
            }
            scanChar();
            bufPos++;
        }
        if (isHint) {
            stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 1);
            token = Token.HINT;
        } else {
            stringVal = subString(mark, bufPos + 1);
            token = Token.MULTI_LINE_COMMENT;
            commentCount++;
            if (keepComments) {
                addComment(stringVal);
            }
        }
        if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
            return;
        }
        if (token != Token.HINT && !isAllowComment()) {
            throw new NotAllowCommentException();
        }
        return;
    }
    if (!isAllowComment()) {
        throw new NotAllowCommentException();
    }
    if (ch == '/' || ch == '-') {
        scanChar();
        bufPos++;
        for (; ; ) {
            if (ch == '\r') {
                if (charAt(pos + 1) == '\n') {
                    line++;
                    bufPos += 2;
                    scanChar();
                    break;
                }
                bufPos++;
                break;
            } else if (ch == EOI) {
                break;
            }
            if (ch == '\n') {
                line++;
                scanChar();
                bufPos++;
                break;
            }
            scanChar();
            bufPos++;
        }
        stringVal = subString(mark, ch != EOI ? bufPos : bufPos + 1);
        token = Token.LINE_COMMENT;
        commentCount++;
        if (keepComments) {
            addComment(stringVal);
        }
        endOfComment = isEOF();
        if (commentHandler != null && commentHandler.handle(lastToken, stringVal)) {
            return;
        }
        return;
    }
}
Also used : NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException) Token(com.alibaba.druid.sql.parser.Token)

Example 4 with NotAllowCommentException

use of com.alibaba.druid.sql.parser.NotAllowCommentException in project druid by alibaba.

the class WallProvider method checkInternal.

private WallCheckResult checkInternal(String sql) {
    checkCount.incrementAndGet();
    WallContext context = WallContext.current();
    if (config.isDoPrivilegedAllow() && ispPrivileged()) {
        WallCheckResult checkResult = new WallCheckResult();
        checkResult.setSql(sql);
        return checkResult;
    }
    // first step, check whiteList
    boolean mulltiTenant = config.getTenantTablePattern() != null && config.getTenantTablePattern().length() > 0;
    if (!mulltiTenant) {
        WallCheckResult checkResult = checkWhiteAndBlackList(sql);
        if (checkResult != null) {
            checkResult.setSql(sql);
            return checkResult;
        }
    }
    hardCheckCount.incrementAndGet();
    final List<Violation> violations = new ArrayList<Violation>();
    List<SQLStatement> statementList = new ArrayList<SQLStatement>();
    boolean syntaxError = false;
    boolean endOfComment = false;
    try {
        SQLStatementParser parser = createParser(sql);
        parser.getLexer().setCommentHandler(WallCommentHandler.instance);
        if (!config.isCommentAllow()) {
            // deny comment
            parser.getLexer().setAllowComment(false);
        }
        if (!config.isCompleteInsertValuesCheck()) {
            parser.setParseCompleteValues(false);
            parser.setParseValuesSize(config.getInsertValuesCheckSize());
        }
        parser.parseStatementList(statementList);
        final Token lastToken = parser.getLexer().token();
        if (lastToken != Token.EOF && config.isStrictSyntaxCheck()) {
            violations.add(new IllegalSQLObjectViolation(ErrorCode.SYNTAX_ERROR, "not terminal sql, token " + lastToken, sql));
        }
        endOfComment = parser.getLexer().isEndOfComment();
    } catch (NotAllowCommentException e) {
        violations.add(new IllegalSQLObjectViolation(ErrorCode.COMMENT_STATEMENT_NOT_ALLOW, "comment not allow", sql));
        incrementCommentDeniedCount();
    } catch (ParserException e) {
        syntaxErrorCount.incrementAndGet();
        syntaxError = true;
        if (config.isStrictSyntaxCheck()) {
            violations.add(new SyntaxErrorViolation(e, sql));
        }
    } catch (Exception e) {
        if (config.isStrictSyntaxCheck()) {
            violations.add(new SyntaxErrorViolation(e, sql));
        }
    }
    if (statementList.size() > 1 && !config.isMultiStatementAllow()) {
        violations.add(new IllegalSQLObjectViolation(ErrorCode.MULTI_STATEMENT, "multi-statement not allow", sql));
    }
    WallVisitor visitor = createWallVisitor();
    visitor.setSqlEndOfComment(endOfComment);
    if (statementList.size() > 0) {
        boolean lastIsHint = false;
        for (int i = 0; i < statementList.size(); i++) {
            SQLStatement stmt = statementList.get(i);
            if ((i == 0 || lastIsHint) && stmt instanceof MySqlHintStatement) {
                lastIsHint = true;
                continue;
            }
            try {
                stmt.accept(visitor);
            } catch (ParserException e) {
                violations.add(new SyntaxErrorViolation(e, sql));
            }
        }
    }
    if (visitor.getViolations().size() > 0) {
        violations.addAll(visitor.getViolations());
    }
    WallSqlStat sqlStat = null;
    if (violations.size() > 0) {
        violationCount.incrementAndGet();
        if (sql.length() < MAX_SQL_LENGTH) {
            sqlStat = addBlackSql(sql, context.getTableStats(), context.getFunctionStats(), violations, syntaxError);
        }
    } else {
        if (sql.length() < MAX_SQL_LENGTH) {
            sqlStat = addWhiteSql(sql, context.getTableStats(), context.getFunctionStats(), syntaxError);
        }
    }
    Map<String, WallSqlTableStat> tableStats = null;
    Map<String, WallSqlFunctionStat> functionStats = null;
    if (context != null) {
        tableStats = context.getTableStats();
        functionStats = context.getFunctionStats();
        recordStats(tableStats, functionStats);
    }
    WallCheckResult result;
    if (sqlStat != null) {
        context.setSqlStat(sqlStat);
        result = new WallCheckResult(sqlStat, statementList);
    } else {
        result = new WallCheckResult(null, violations, tableStats, functionStats, statementList, syntaxError);
    }
    String resultSql;
    if (visitor.isSqlModified()) {
        resultSql = SQLUtils.toSQLString(statementList, dbType);
    } else {
        resultSql = sql;
    }
    result.setSql(resultSql);
    return result;
}
Also used : SyntaxErrorViolation(com.alibaba.druid.wall.violation.SyntaxErrorViolation) IllegalSQLObjectViolation(com.alibaba.druid.wall.violation.IllegalSQLObjectViolation) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SyntaxErrorViolation(com.alibaba.druid.wall.violation.SyntaxErrorViolation) ArrayList(java.util.ArrayList) IllegalSQLObjectViolation(com.alibaba.druid.wall.violation.IllegalSQLObjectViolation) Token(com.alibaba.druid.sql.parser.Token) MySqlHintStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException) NotAllowCommentException(com.alibaba.druid.sql.parser.NotAllowCommentException)

Aggregations

NotAllowCommentException (com.alibaba.druid.sql.parser.NotAllowCommentException)4 Token (com.alibaba.druid.sql.parser.Token)4 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 MySqlHintStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlHintStatement)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 IllegalSQLObjectViolation (com.alibaba.druid.wall.violation.IllegalSQLObjectViolation)1 SyntaxErrorViolation (com.alibaba.druid.wall.violation.SyntaxErrorViolation)1 ArrayList (java.util.ArrayList)1