Skip to content
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
66 changes: 39 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,61 @@ on:
pull_request:
branches: [main, master]

permissions:
contents: read

jobs:
integration-test:
name: go-sqlsmith
name: go-sqlsmith (MySQL ${{ matrix.mysql }})
runs-on: ubuntu-24.04

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h 127.0.0.1 -uroot -proot"
--health-interval=10s
--health-timeout=5s
--health-retries=5
strategy:
matrix:
mysql: ['5.7', '8.4']
env:
SQLSMITH_TEST_DB: sqlsmith_ci

steps:
- name: Checkout code
uses: actions/checkout@v4
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false

- name: Set up Go
uses: actions/setup-go@v5
uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5
with:
go-version: 1.21.0
go-version-file: go.mod

- name: Wait for MySQL to be ready
- name: Start isolated MySQL with a schema-scoped account
env:
MYSQL_VERSION: ${{ matrix.mysql }}
run: |
for i in {1..30}; do
if mysqladmin ping -h 127.0.0.1 -uroot -proot --silent; then
echo "MySQL is ready"
break
password="$(openssl rand -hex 32)"
echo "::add-mask::$password"
echo "SQLSMITH_PASSWORD=$password" >> "$GITHUB_ENV"
docker run -d --name sqlsmith-ci -p 127.0.0.1:3306:3306 \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-e MYSQL_DATABASE="$SQLSMITH_TEST_DB" \
-e MYSQL_USER=sqlsmith -e MYSQL_PASSWORD="$password" \
"mysql:$MYSQL_VERSION"
for i in {1..60}; do
if docker exec -e MYSQL_PWD="$password" sqlsmith-ci \
mysql -usqlsmith -e 'SELECT 1' >/dev/null 2>&1; then
exit 0
fi
echo "Waiting for MySQL..."
sleep 2
done
echo 'MySQL did not become ready' >&2
exit 1

- name: Install dependencies
run: go mod download
- name: Unit, security integration and race tests
run: go test -race ./...

- name: Build
run: make

- name: Run integration tests
run: make integration-test
- name: Run integration binary against the disposable schema
run: make integration-test TEST_FLAGS='-host 127.0.0.1 -port 3306 -user sqlsmith -db sqlsmith_ci -clear'

- name: Stop isolated MySQL
if: always()
run: docker rm -f sqlsmith-ci || true
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
TEST_FLAGS ?=
LDFLAGS :=
GOMOD :=

GOARCH := $(if $(GOARCH),$(GOARCH),amd64)
GO=GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) GO111MODULE=on go
GOTEST=GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GO111MODULE=on go test # go race detector requires cgo
VERSION := $(if $(VERSION),$(VERSION),latest)
GOBUILD=$(GO) build -ldflags '$(LDFLAGS)'

sqlsmith:
$(GOBUILD) $(GOMOD) -o bin/sqlsmith cmd/*.go
$(GOBUILD) $(GOMOD) -o bin/sqlsmith ./cmd

integration-test:
$(GOBUILD) $(GOMOD) -o bin/test tests/*.go
./bin/test -socket /var/run/mysqld/mysqld.sock
$(GOBUILD) $(GOMOD) -o bin/test ./tests
./bin/test $(TEST_FLAGS)

.PHONY: sqlsmith
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,85 @@ func gosmith() {
sql, err := ss.Walk(node)
}
```

## CLI connection and test safety

Use a database account scoped to a disposable test schema. The binaries no longer
need `SUPER` or change global `sql_mode`; the driver sets the existing generator
mode (`NO_ENGINE_SUBSTITUTION`) on each physical session, including worker and
reconnected sessions. Generated DDL and DML still run with the chosen account's
privileges.

Both binaries accept the existing `-host`, `-port`, `-user`, `-passwd`, `-socket`
and `-db` options. Prefer `SQLSMITH_PASSWORD` or `-password-file` to putting a
password in process arguments. A password file may end with LF or CRLF; other
characters are preserved. A nonempty `-passwd` takes precedence over the
environment; `-password-file` takes precedence over the environment and cannot
be combined with a nonempty `-passwd`. Connection logs omit the password.

Remote TCP connections require `-tls`, which verifies the server certificate
and hostname. Use `-tls-ca /path/to/ca.pem` for a private CA, and optionally
`-tls-server-name db.example` when the connection address differs from the
certificate name. There is no certificate-verification bypass. Local loopback
and Unix-socket connections continue to support plaintext. An isolated remote
test network without TLS requires explicit `-allow-insecure-transport`.
Passwordless disposable instances require explicit `-allow-empty-password`.
The existing `root` user default remains for local test compatibility; use
`-user sqlsmith` with a schema-scoped account for normal runs.

For example, an administrator can provision a dedicated schema and grant only
its privileges to an existing test user:

```sql
CREATE DATABASE sqlsmith_test;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX
ON sqlsmith_test.* TO 'sqlsmith'@'<runner-host>';
```

Run the fuzzer using a mounted password file:

```sh
make
./bin/sqlsmith -host db.example -user sqlsmith -db sqlsmith_test \
-password-file /run/secrets/mysql-password -tls -tls-ca /path/to/ca.pem
```

The integration binary replaces its selected schema and refuses to connect
unless `-clear` is supplied. It uses `-db` consistently instead of always
dropping the database named `test`. `make integration-test` no longer selects
the host MySQL socket implicitly; pass the intended disposable target:

```sh
make integration-test TEST_FLAGS='-host 127.0.0.1 -port 3306 -user sqlsmith -db sqlsmith_test -password-file /run/secrets/mysql-password -clear'
```

The same `-clear` opt-in remains available on `bin/sqlsmith`. Bootstrap
statements and successful worker DDL have timestamped audit context without
connection credentials.

## Security regression tests

`go test ./...` covers SQL identifier and literal round trips, comment-safe
optimizer hints, reservation boundaries, stable function generation and
connection options. To include real-database tests (also under `-race`), supply
a disposable schema already granted to the `sqlsmith` account:

```sh
SQLSMITH_TEST_DB=sqlsmith_test SQLSMITH_TEST_PORT=3306 \
SQLSMITH_TEST_PASSWORD_FILE=/run/secrets/mysql-password \
go test -race ./...
```

`SQLSMITH_PASSWORD` can be used instead of `SQLSMITH_TEST_PASSWORD_FILE`.
`SQLSMITH_TEST_SOCKET` selects a local socket. `SQLSMITH_TEST_TLS_CA` additionally
enables the live TLS handshake tests over TCP: a trusted certificate succeeds,
while an untrusted CA or wrong hostname fails. CI exercises MySQL 5.7 and 8.4
with a generated password and a schema-scoped user.

The library's public SQL generation signatures remain unchanged. Identifiers
are quoted rather than restricted to ASCII: reserved words, spaces, Unicode
and embedded backticks remain usable. Optional optimizer hints are omitted
when a referenced identifier contains a comment terminator. Use one mutable
`SQLSmith` instance per goroutine, or synchronize external access, including
access to its exported schema maps. The CLI synchronizes its shared generator
while allowing database executions to proceed concurrently.
5 changes: 5 additions & 0 deletions builtin/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func GenerateFuncCallExpr(table *types.Table, args int, stable bool) ast.ExprNod
funcCallExpr := ast.FuncCallExpr{}

fns := getValidArgsFunc(args, stable)
if len(fns) == 0 {
// Some modes have no function with this arity. A literal is still a
// valid expression and preserves the requested stability guarantee.
return ast.NewValueExpr(util.GenerateRandDataItem(), "", "")
}
fn := copyFunc(fns[util.Rd(len(fns))])
funcCallExpr.FnName = ast.NewCIStr(fn.name)
for i := 0; i < args; i++ {
Expand Down
16 changes: 16 additions & 0 deletions builtin/hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package builtin
import (
"log"
"math/rand"
"strings"

"github.com/pingcap/tidb/pkg/parser/ast"

Expand Down Expand Up @@ -84,6 +85,21 @@ var disabledHintKeywords = []*hintClass{

// GenerateHintExpr ...
func GenerateHintExpr(table *types.Table) (h *ast.TableOptimizerHint) {
// Optimizer hints live inside a SQL comment. Backtick quoting cannot
// protect a comment terminator, so omit hints for these names only.
if table == nil || strings.Contains(table.Table, "*/") {
return nil
}
for _, t := range table.InnerTableList {
if t == nil || strings.Contains(t.Table, "*/") {
return nil
}
}
for _, index := range table.Indexes {
if strings.Contains(index, "*/") {
return nil
}
}
enabledKeywords := hintKeywords
if len(table.Indexes) > 0 {
enabledKeywords = append(enabledKeywords, indexHintKeywords...)
Expand Down
44 changes: 44 additions & 0 deletions builtin/security_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package builtin

import (
"testing"

"github.com/chaos-mesh/go-sqlsmith/types"
"github.com/chaos-mesh/go-sqlsmith/util"
"github.com/pingcap/tidb/pkg/parser"
_ "github.com/pingcap/tidb/pkg/types/parser_driver"
"github.com/stretchr/testify/require"
)

func TestHintCommentTerminator(t *testing.T) {
for _, table := range []*types.Table{
{Table: "x*/WHERE 1=1/*"},
{Table: "safe", InnerTableList: []*types.Table{{Table: "x*/WHERE 1=1/*"}}},
{Table: "safe", Indexes: []string{"x*/WHERE 1=1/*"}},
} {
require.Nil(t, GenerateHintExpr(table))
}
safe := &types.Table{Table: "select", InnerTableList: []*types.Table{{Table: "has space"}}, Indexes: []string{"a`b"}}
generated := 0
for i := 0; i < 100; i++ {
hint := GenerateHintExpr(safe)
if hint == nil {
continue
}
sql, err := util.BufferOut(hint)
require.NoError(t, err)
require.NotContains(t, sql, "*/")
generated++
}
require.Positive(t, generated)
}

func TestStableFunctionsWithEmptyArityCatalog(t *testing.T) {
for _, args := range []int{0, 1, 2, 3, 20} {
expr := GenerateFuncCallExpr(nil, args, true)
sql, err := util.BufferOut(expr)
require.NoError(t, err)
_, err = parser.New().ParseOneStmt("SELECT "+sql, "", "")
require.NoError(t, err, sql)
}
}
Loading
Loading