-
Notifications
You must be signed in to change notification settings - Fork 0
fix: deduplicate workspace provisioning #34
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ use crate::models::workspace::Workspace; | |
| use crate::tenant::validate_schema_name; | ||
| use sqlx::PgPool; | ||
|
|
||
| const WORKSPACE_SCHEMA_V1: &str = include_str!("../../../../migrations/workspace_v1.sql"); | ||
| const WORKSPACE_SCHEMA_V1: &str = include_str!("../../migrations/workspace_v1.sql"); | ||
|
|
||
| pub async fn create( | ||
| pool: &PgPool, | ||
|
|
@@ -30,7 +30,7 @@ pub async fn create( | |
| .await?; | ||
|
|
||
| // Create the schema and apply workspace DDL | ||
| provision_schema(pool, schema_name).await?; | ||
| provision_schema(pool, schema_name, "").await?; | ||
|
|
||
| // Update schema_version | ||
| sqlx::query( | ||
|
|
@@ -101,17 +101,24 @@ pub async fn resolve_schema( | |
| Ok(row.map(|r| r.0)) | ||
| } | ||
|
|
||
| async fn provision_schema(pool: &PgPool, schema_name: &str) -> Result<(), sqlx::Error> { | ||
| let create_schema = format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", schema_name); | ||
| sqlx::query(&create_schema).execute(pool).await?; | ||
| pub async fn provision_schema( | ||
| pool: &PgPool, | ||
| schema_name: &str, | ||
| table_prefix: &str, | ||
| ) -> Result<(), sqlx::Error> { | ||
| sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", schema_name)) | ||
| .execute(pool) | ||
| .await?; | ||
|
Comment on lines
+104
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add input validation before SQL interpolation in the now-public function.
Validate at function entry, consistent with the 🛡️ Proposed fix to add validation pub async fn provision_schema(
pool: &PgPool,
schema_name: &str,
table_prefix: &str,
) -> Result<(), sqlx::Error> {
+ use crate::tenant::validate_table_prefix;
+ assert!(
+ validate_schema_name(schema_name),
+ "Invalid schema name: {}",
+ schema_name
+ );
+ assert!(
+ validate_table_prefix(table_prefix),
+ "Invalid table prefix: {}",
+ table_prefix
+ );
+
sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS \"{}\"", schema_name))
.execute(pool)
.await?;🧰 Tools🪛 OpenGrep (1.22.0)[ERROR] 109-109: SQL query built via format!() passed to a database method. Use parameterized queries with bind parameters instead. (coderabbit.sql-injection.rust-format-query) 🤖 Prompt for AI AgentsSource: Linters/SAST tools |
||
|
|
||
| let mut conn = pool.acquire().await?; | ||
| let mut conn = crate::db::scoped::scoped_connection(pool, schema_name).await?; | ||
|
|
||
| sqlx::query(&format!("SET search_path TO \"{}\"", schema_name)) | ||
| .execute(&mut *conn) | ||
| .await?; | ||
| let p = if table_prefix.is_empty() { | ||
| String::new() | ||
| } else { | ||
| format!("{}_", table_prefix) | ||
| }; | ||
|
|
||
| let ddl = WORKSPACE_SCHEMA_V1.replace("{p}", ""); | ||
| let ddl = WORKSPACE_SCHEMA_V1.replace("{p}", &p); | ||
| for stmt in ddl.split(';') { | ||
| let stmt = stmt.trim(); | ||
| if !stmt.is_empty() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible to use bind parameters here @mahatoankitkumar instead of string concatenation ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No — PostgreSQL doesn't support bind parameters for identifiers (schema names, table names)