From cf3f566e0682d53210480e9e9bad4aaff0551688 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 5 Jun 2023 11:59:45 -0400 Subject: [PATCH] escape database password as it may contain special characters --- packages/backend/native-utils/migration/Cargo.toml | 2 +- packages/backend/native-utils/migration/src/main.rs | 13 +++++++++---- packages/backend/src/db/postgre.ts | 4 +++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/backend/native-utils/migration/Cargo.toml b/packages/backend/native-utils/migration/Cargo.toml index 3813eccd8..4dee156ef 100644 --- a/packages/backend/native-utils/migration/Cargo.toml +++ b/packages/backend/native-utils/migration/Cargo.toml @@ -13,7 +13,6 @@ default = [] convert = ["dep:native-utils"] [dependencies] -async-std = { version = "1", features = ["attributes", "tokio1"] } serde_json = "1.0.96" native-utils = { path = "../", optional = true } indicatif = { version = "0.17.4", features = ["tokio"] } @@ -21,6 +20,7 @@ tokio = { version = "1.28.2", features = ["full"] } futures = "0.3.28" serde_yaml = "0.9.21" serde = { version = "1.0.163", features = ["derive"] } +urlencoding = "2.1.2" [dependencies.sea-orm-migration] version = "0.11.0" diff --git a/packages/backend/native-utils/migration/src/main.rs b/packages/backend/native-utils/migration/src/main.rs index a845a41ff..f0f761f65 100644 --- a/packages/backend/native-utils/migration/src/main.rs +++ b/packages/backend/native-utils/migration/src/main.rs @@ -1,24 +1,29 @@ use serde::Deserialize; use std::env; use std::fs; +use urlencoding::encode; use sea_orm_migration::prelude::*; #[cfg(feature = "convert")] mod vec_to_json; -#[async_std::main] +#[tokio::main] async fn main() { let cwd = env::current_dir().unwrap(); let yml = fs::File::open(cwd.join("../../.config/default.yml")) - .expect("Unable to read '.config/default.yml'"); - let config: Config = serde_yaml::from_reader(yml).expect("Unable to parse"); + .expect("Failed to open '.config/default.yml'"); + let config: Config = serde_yaml::from_reader(yml).expect("Failed to parse yaml"); env::set_var( "DATABASE_URL", format!( "postgres://{}:{}@{}:{}/{}", - config.db.user, config.db.pass, config.db.host, config.db.port, config.db.db + config.db.user, + encode(&config.db.pass), + config.db.host, + config.db.port, + config.db.db, ), ); diff --git a/packages/backend/src/db/postgre.ts b/packages/backend/src/db/postgre.ts index 9ffaf596c..0fa5fdff6 100644 --- a/packages/backend/src/db/postgre.ts +++ b/packages/backend/src/db/postgre.ts @@ -222,7 +222,9 @@ export const db = new DataSource({ export async function initDb(force = false) { await nativeInitDatabase( - `postgres://${config.db.user}:${config.db.pass}@${config.db.host}:${config.db.port}/${config.db.db}`, + `postgres://${config.db.user}:${encodeURIComponent(config.db.pass)}@${ + config.db.host + }:${config.db.port}/${config.db.db}`, ); if (force) { if (db.isInitialized) {