Skip to content

Commit e253a08

Browse files
authored
feat(server): add sql endpoint toggle (#408)
* feat(server): gate sql endpoint * refactor(server): rename sql flag * refactor(server): rename sql flag to raw_sql * fix(cli): allow bool value for http.sql
1 parent eef820a commit e253a08

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

crates/cli/src/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ pub struct ServerOptions {
330330
#[arg(value_delimiter = ',')]
331331
pub http_cors_origins: Option<Vec<String>>,
332332

333+
/// Enable the SQL playground and query endpoint at /sql.
334+
#[arg(
335+
long = "http.sql",
336+
action = clap::ArgAction::Set,
337+
default_value_t = true,
338+
help = "Enable the SQL playground and query endpoint at /sql."
339+
)]
340+
pub raw_sql: bool,
341+
333342
/// Path to the SSL certificate file (.pem)
334343
#[arg(
335344
long = "http.tls_cert_path",
@@ -360,6 +369,7 @@ impl Default for ServerOptions {
360369
http_addr: DEFAULT_HTTP_ADDR,
361370
http_port: DEFAULT_HTTP_PORT,
362371
http_cors_origins: None,
372+
raw_sql: true,
363373
tls_cert_path: None,
364374
tls_key_path: None,
365375
mkcert: false,

crates/grpc/server/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,10 @@ impl<P: Provider + Sync + Send + 'static> proto::world::world_server::World for
11831183
&self,
11841184
request: Request<proto::types::SqlQueryRequest>,
11851185
) -> Result<Response<proto::types::SqlQueryResponse>, Status> {
1186+
if !self._config.raw_sql {
1187+
return Err(Status::permission_denied("SQL endpoint is disabled."));
1188+
}
1189+
11861190
let proto::types::SqlQueryRequest { query } = request.into_inner();
11871191

11881192
// Execute the query
@@ -1227,6 +1231,7 @@ pub struct GrpcConfig {
12271231
pub http2_keepalive_interval: Duration,
12281232
pub http2_keepalive_timeout: Duration,
12291233
pub max_message_size: usize,
1234+
pub raw_sql: bool,
12301235
}
12311236

12321237
impl Default for GrpcConfig {
@@ -1238,6 +1243,7 @@ impl Default for GrpcConfig {
12381243
http2_keepalive_interval: Duration::from_secs(30),
12391244
http2_keepalive_timeout: Duration::from_secs(10),
12401245
max_message_size: 16 * 1024 * 1024,
1246+
raw_sql: true,
12411247
}
12421248
}
12431249
}

crates/runner/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ impl Runner {
740740
self.args.grpc.http2_keepalive_timeout,
741741
),
742742
max_message_size: self.args.grpc.max_message_size,
743+
raw_sql: self.args.server.raw_sql,
743744
},
744745
Some(grpc_bind_addr),
745746
)
@@ -757,6 +758,7 @@ impl Runner {
757758
None,
758759
absolute_path.clone(),
759760
Arc::new(readonly_pool.clone()),
761+
self.args.server.raw_sql,
760762
storage.clone(),
761763
provider.clone(),
762764
self.version_spec.clone(),
@@ -837,7 +839,11 @@ impl Runner {
837839
info!(target: LOG_TARGET, endpoint = %addr, protocol = %protocol, "Starting torii endpoint.");
838840
info!(target: LOG_TARGET, endpoint = %grpc_addr, "Serving gRPC endpoint.");
839841
info!(target: LOG_TARGET, endpoint = %gql_endpoint, "Serving Graphql playground.");
840-
info!(target: LOG_TARGET, endpoint = %sql_endpoint, "Serving SQL playground.");
842+
if self.args.server.raw_sql {
843+
info!(target: LOG_TARGET, endpoint = %sql_endpoint, "Serving SQL playground.");
844+
} else {
845+
info!(target: LOG_TARGET, "SQL endpoint is disabled.");
846+
}
841847
info!(target: LOG_TARGET, endpoint = %mcp_endpoint, "Serving MCP endpoint.");
842848
info!(target: LOG_TARGET, url = %explorer_url, "Serving World Explorer.");
843849
info!(target: LOG_TARGET, path = %artifacts_path, "Serving ERC artifacts at path");

crates/server/src/handlers/sql.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ use super::Handler;
1212
#[derive(Debug)]
1313
pub struct SqlHandler {
1414
pool: Arc<SqlitePool>,
15+
enabled: bool,
1516
}
1617

1718
impl SqlHandler {
18-
pub fn new(pool: Arc<SqlitePool>) -> Self {
19-
Self { pool }
19+
pub fn new(pool: Arc<SqlitePool>, enabled: bool) -> Self {
20+
Self { pool, enabled }
2021
}
2122

2223
pub async fn execute_query(&self, query: String) -> Response<Body> {
@@ -46,6 +47,14 @@ impl SqlHandler {
4647
}
4748
}
4849

50+
fn disabled_response(&self) -> Response<Body> {
51+
Response::builder()
52+
.status(StatusCode::FORBIDDEN)
53+
.header(CONTENT_TYPE, "text/plain")
54+
.body(Body::from("SQL endpoint is disabled."))
55+
.unwrap()
56+
}
57+
4958
async fn serve_playground(&self) -> Response<Body> {
5059
let html = include_str!("../../static/sql-playground.html");
5160

@@ -58,6 +67,10 @@ impl SqlHandler {
5867
}
5968

6069
async fn handle_request(&self, req: Request<Body>) -> Response<Body> {
70+
if !self.enabled {
71+
return self.disabled_response();
72+
}
73+
6174
if req.method() == Method::GET && req.uri().query().unwrap_or_default().is_empty() {
6275
self.serve_playground().await
6376
} else {

crates/server/src/proxy.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ impl<P: Provider + Sync + Send + Debug + 'static> Proxy<P> {
167167
graphql_addr: Option<SocketAddr>,
168168
artifacts_dir: Utf8PathBuf,
169169
pool: Arc<SqlitePool>,
170+
raw_sql: bool,
170171
storage: Arc<S>,
171172
provider: P,
172173
version_spec: String,
@@ -176,7 +177,7 @@ impl<P: Provider + Sync + Send + Debug + 'static> Proxy<P> {
176177
let grpc_proxy_client = Arc::new(create_grpc_proxy_client(&proxy_settings));
177178
let websocket_proxy_client = Arc::new(create_websocket_proxy_client());
178179

179-
let handlers: Arc<RwLock<Vec<Box<dyn Handler>>>> = Arc::new(RwLock::new(vec![
180+
let handlers: Vec<Box<dyn Handler>> = vec![
180181
Box::new(GraphQLHandler::new(
181182
graphql_addr,
182183
grpc_proxy_client.clone(),
@@ -185,9 +186,11 @@ impl<P: Provider + Sync + Send + Debug + 'static> Proxy<P> {
185186
Box::new(GrpcHandler::new(grpc_addr, grpc_proxy_client.clone())),
186187
Box::new(McpHandler::new(pool.clone())),
187188
Box::new(MetadataHandler::new(storage.clone(), provider)),
188-
Box::new(SqlHandler::new(pool.clone())),
189+
Box::new(SqlHandler::new(pool.clone(), raw_sql)),
189190
Box::new(StaticHandler::new(artifacts_dir, (*pool).clone())),
190-
]));
191+
];
192+
193+
let handlers: Arc<RwLock<Vec<Box<dyn Handler>>>> = Arc::new(RwLock::new(handlers));
191194

192195
Self {
193196
addr,

0 commit comments

Comments
 (0)