Skip to content

Commit cfcd863

Browse files
Sanitize Database Name, Licenses 2025-07-28 (#21)
* Sanitize Database Name, Licenses 2025-07-28 Signed-off-by: Julio Jimenez <julio@clickhouse.com> * Update lib/sanitize.sh Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * tests Signed-off-by: Julio Jimenez <julio@clickhouse.com> * echo output Signed-off-by: Julio Jimenez <julio@clickhouse.com> * fix tests Signed-off-by: Julio Jimenez <julio@clickhouse.com> * fix database Signed-off-by: Julio Jimenez <julio@clickhouse.com> * fix database Signed-off-by: Julio Jimenez <julio@clickhouse.com> * fix database Signed-off-by: Julio Jimenez <julio@clickhouse.com> * fix database Signed-off-by: Julio Jimenez <julio@clickhouse.com> --------- Signed-off-by: Julio Jimenez <julio@clickhouse.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c18a92d commit cfcd863

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

lib/sanitize.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ sanitize_email() {
165165
echo "$sanitized"
166166
}
167167

168+
# Sanitize database/table names
169+
sanitize_database_name() {
170+
local name="$1"
171+
172+
# Database names should only contain alphanumeric and underscores
173+
local sanitized
174+
sanitized=$(echo "$name" | sed 's/[^a-zA-Z0-9_]//g' | sed 's/^[0-9]/_&/')
175+
176+
echo "$sanitized"
177+
}
178+
168179
# Main sanitization function - sanitizes all environment variables
169180
sanitize_inputs() {
170181
log_debug "Sanitizing input parameters..."
@@ -293,10 +304,10 @@ sanitize_inputs() {
293304
log_debug "Sanitized CLICKHOUSE_URL: $CLICKHOUSE_URL"
294305
fi
295306

296-
# if [[ -n "${CLICKHOUSE_DATABASE:-}" ]]; then
297-
# CLICKHOUSE_DATABASE=$(sanitize_database_name "$CLICKHOUSE_DATABASE")
298-
# log_debug "Sanitized CLICKHOUSE_DATABASE: $CLICKHOUSE_DATABASE"
299-
# fi
307+
if [[ -n "${CLICKHOUSE_DATABASE:-}" ]]; then
308+
CLICKHOUSE_DATABASE=$(sanitize_database_name "$CLICKHOUSE_DATABASE")
309+
log_debug "Sanitized CLICKHOUSE_DATABASE: $CLICKHOUSE_DATABASE"
310+
fi
300311

301312
if [[ -n "${CLICKHOUSE_USERNAME:-}" ]]; then
302313
CLICKHOUSE_USERNAME=$(sanitize_string "$CLICKHOUSE_USERNAME" 100)

license-mappings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,28 @@
166166
"github.com/aws/smithy-go": "Apache-2.0",
167167
"github.com/aymanbagabas/go-osc52/v2": "MIT",
168168
"github.com/aymerick/douceur": "MIT",
169+
"github.com/bahlo/generic-list-go": "BSD-3-Clause",
170+
"github.com/baidubce/bce-sdk-go": "Apache-2.0",
171+
"github.com/bboreham/go-loser": "Apache-2.0",
172+
"github.com/benbjohnson/clock": "MIT",
173+
"github.com/beorn7/perks": "MIT",
174+
"github.com/bkielbasa/cyclop": "MIT",
175+
"github.com/blang/semver/v4": "MIT",
176+
"github.com/blizzy78/varnamelen": "MIT",
177+
"github.com/bmatcuk/doublestar/v4": "MIT",
178+
"github.com/bombsimon/logrusr/v2": "MIT",
179+
"github.com/bombsimon/wsl/v4": "MIT",
180+
"github.com/bradleyfalzon/ghinstallation/v2": "Apache-2.0",
181+
"github.com/braintree/manners": "MIT",
182+
"github.com/breml/bidichk": "MIT",
183+
"github.com/breml/errchkjson": "MIT",
184+
"github.com/briandowns/spinner": "Apache-2.0",
185+
"github.com/bufbuild/protocompile": "Apache-2.0",
186+
"github.com/buger/jsonparser": "MIT",
187+
"github.com/butuzov/ireturn": "MIT",
188+
"github.com/butuzov/mirror": "MIT",
189+
"github.com/bytedance/sonic": "Apache-2.0",
190+
"github.com/bytedance/sonic/loader": "Apache-2.0",
169191
"github.com/Azure/azure-amqp-common-go/v3": "MIT",
170192
"github.com/Azure/azure-pipeline-go": "MIT",
171193
"github.com/Azure/azure-sdk-for-go-extensions": "MIT",

test/simple.bats

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,45 @@ EOF
665665
[ "$status" -eq 1 ]
666666
[[ "$output" == *"Invalid email format"* ]]
667667
}
668+
669+
# Test 73: sanitize_email accepts valid name
670+
@test "sanitize_database_name accepts valid name" {
671+
run sanitize_database_name "test_database"
672+
[ "$status" -eq 0 ]
673+
[[ "$output" == "test_database" ]]
674+
}
675+
676+
# Test 74: sanitize_database_name accepts name with underscores
677+
@test "sanitize_database_name accepts name starting with underscore" {
678+
run sanitize_database_name "_test_database"
679+
[ "$status" -eq 0 ]
680+
[[ "$output" == "_test_database" ]]
681+
}
682+
683+
# Test 75: sanitize_database_name accepts name with numbers
684+
@test "sanitize_database_name accepts name with numbers" {
685+
run sanitize_database_name "test_database_123"
686+
[ "$status" -eq 0 ]
687+
[[ "$output" == "test_database_123" ]]
688+
}
689+
690+
# Test 76: sanitize_database_name removes dangerous characters
691+
@test "sanitize_database_name removes dangerous characters" {
692+
run sanitize_database_name "test-database.name"
693+
[ "$status" -eq 0 ]
694+
[[ "$output" == "testdatabasename" ]]
695+
}
696+
697+
# Test 77: sanitize_database_name rejects name with starting with number
698+
@test "sanitize_database_name rejects name starting with number" {
699+
run sanitize_database_name "1test_database"
700+
[ "$status" -eq 0 ]
701+
[[ "$output" == "_1test_database" ]]
702+
}
703+
704+
# Test 78: sanitize_database_name rejects name with spaces
705+
@test "sanitize_database_name rejects name with spaces" {
706+
run sanitize_database_name "test database"
707+
[ "$status" -eq 0 ]
708+
[[ "$output" == "testdatabase" ]]
709+
}

0 commit comments

Comments
 (0)