Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix sql query #3282

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions internal/pkg/store/sql/database.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2022 EMQ Technologies Co., Ltd.
// Copyright 2022-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,8 +14,31 @@

package sql

import "database/sql"
import (
"database/sql"
"regexp"
)

type Database interface {
Apply(f func(db *sql.DB) error) error
}

// isValidTableName checks if the given string is a valid database table name.
func isValidTableName(tableName string) bool {
// Check if the table name is empty
if tableName == "" {
return false
}

Check warning on line 31 in internal/pkg/store/sql/database.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/database.go#L30-L31

Added lines #L30 - L31 were not covered by tests

// Regular expression to match valid table names
// ^[a-zA-Z_][a-zA-Z0-9_]*$
// ^[a-zA-Z_] ensures the name starts with a letter or underscore
// [a-zA-Z0-9_]*$ ensures the rest of the name consists of letters, digits, or underscores
validTableNamePattern := `^[a-zA-Z_][a-zA-Z0-9/_]*$`

// Compile the regular expression
re := regexp.MustCompile(validTableNamePattern)

// Check if the table name matches the pattern
return re.MatchString(tableName)
}
9 changes: 6 additions & 3 deletions internal/pkg/store/sql/sqlKv.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,6 +31,9 @@ type sqlKvStore struct {
}

func createSqlKvStore(database Database, table string) (*sqlKvStore, error) {
if !isValidTableName(table) {
return nil, fmt.Errorf("invalid table name: %s", table)
}
store := &sqlKvStore{
database: database,
table: table,
Expand Down Expand Up @@ -157,12 +160,12 @@ func (kv *sqlKvStore) Delete(key string) error {
if nil != err || 0 == len(tmp) {
return errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("%s is not found", key))
}
query = fmt.Sprintf("DELETE FROM '%s' WHERE key='%s';", kv.table, key)
query = fmt.Sprintf("DELETE FROM '%s' WHERE key=?;", kv.table)
stmt, err = db.Prepare(query)
if err != nil {
return err
}
_, err = stmt.Exec()
_, err = stmt.Exec(key)
return err
})
}
Expand Down
26 changes: 25 additions & 1 deletion internal/pkg/store/sql/sqlKv_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,9 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/v2/internal/pkg/store/definition"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store/sql/sqlite"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store/test/common"
Expand Down Expand Up @@ -78,6 +81,27 @@ func TestSqlKvGetKeyedState(t *testing.T) {
common.TestKvGetKeyedState(ks, t)
}

func TestInvalidTableName(t *testing.T) {
absPath, err := filepath.Abs("test")
require.NoError(t, err)
err = deleteIfExists(absPath)
assert.NoError(t, err)
config := definition.Config{
Type: "sqlite",
Redis: definition.RedisConfig{},
Sqlite: definition.SqliteConfig{
Path: absPath,
Name: SDbName,
},
}
db, _ := sqlite.NewSqliteDatabase(config, "sqliteKV.db")
err = db.Connect()
require.NoError(t, err)
builder := NewStoreBuilder(db.(Database))
_, err = builder.CreateStore("1_abc")
require.EqualError(t, err, "invalid table name: 1_abc")
}

func deleteIfExists(abs string) error {
absPath := path.Join(abs, SDbName)
if f, _ := os.Stat(absPath); f != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/store/sql/sqlTs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 EMQ Technologies Co., Ltd.
// Copyright 2022-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,9 @@ type ts struct {
}

func createSqlTs(database Database, table string) (*ts, error) {
if !isValidTableName(table) {
return nil, fmt.Errorf("invalid table name: %s", table)
}
store := &ts{
database: database,
table: table,
Expand Down
25 changes: 24 additions & 1 deletion internal/pkg/store/sql/sqlTs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 EMQ Technologies Co., Ltd.
// Copyright 2021-2024 EMQ Technologies Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,9 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/lf-edge/ekuiper/v2/internal/pkg/store/definition"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store/sql/sqlite"
"github.com/lf-edge/ekuiper/v2/internal/pkg/store/test/common"
Expand Down Expand Up @@ -108,6 +111,26 @@ func setupTSqlKv() (ts2.Tskv, definition.Database, string) {
return store, db, absPath
}

func TestInvalidTsTableName(t *testing.T) {
absPath, err := filepath.Abs("test")
require.NoError(t, err)
err = deleteIfExists(absPath)
assert.NoError(t, err)
config := definition.Config{
Type: "sqlite",
Sqlite: definition.SqliteConfig{
Path: absPath,
Name: TDbName,
},
}
db, _ := sqlite.NewSqliteDatabase(config, "sqliteKV.db")
err = db.Connect()
require.NoError(t, err)
builder := NewTsBuilder(db.(Database))
_, err = builder.CreateTs("1_abc")
require.EqualError(t, err, "invalid table name: 1_abc")
}

func cleanTSqlKv(db definition.Database, abs string) {
if err := db.Disconnect(); err != nil {
panic(err)
Expand Down
Loading