fix: encode URI components in fetch requests to prevent errors with special characters

This commit is contained in:
Aadesh Kheria 2026-04-20 14:44:09 -07:00
parent c329a46e33
commit 26ce83f935
3 changed files with 5 additions and 5 deletions

View File

@ -50,7 +50,7 @@ async function rawCallReducer(token: string, reducer: string, args: unknown[]):
const base = httpBase();
if (!base) throw new StackAssertionError("SpacetimeDB not configured");
const dbName = getEnvVariable("STACK_SPACETIMEDB_DB_NAME");
const res = await fetch(`${base}/v1/database/${dbName}/call/${reducer}`, {
const res = await fetch(`${base}/v1/database/${encodeURIComponent(dbName)}/call/${encodeURIComponent(reducer)}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -105,7 +105,7 @@ export async function callSql<T = Record<string, unknown>>(sql: string): Promise
const base = httpBase();
if (!base) return [];
const dbName = getEnvVariable("STACK_SPACETIMEDB_DB_NAME");
const res = await fetch(`${base}/v1/database/${dbName}/sql`, {
const res = await fetch(`${base}/v1/database/${encodeURIComponent(dbName)}/sql`, {
method: "POST",
headers: { "Authorization": `Bearer ${token}` },
body: sql,

View File

@ -74,7 +74,7 @@ export async function callReducer(
args: unknown[],
): Promise<ReducerCallResult> {
const { baseUrl, dbName } = getSpacetimedbConfig();
const res = await fetch(`${baseUrl}/v1/database/${dbName}/call/${reducer}`, {
const res = await fetch(`${baseUrl}/v1/database/${encodeURIComponent(dbName)}/call/${encodeURIComponent(reducer)}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@ -113,7 +113,7 @@ export async function findCorrelationIdByQuestion(
export async function sqlQuery(token: string, sql: string): Promise<SqlQueryResult> {
const { baseUrl, dbName } = getSpacetimedbConfig();
const res = await fetch(`${baseUrl}/v1/database/${dbName}/sql`, {
const res = await fetch(`${baseUrl}/v1/database/${encodeURIComponent(dbName)}/sql`, {
method: "POST",
headers: {
"Content-Type": "text/plain",

View File

@ -90,7 +90,7 @@ async function probeToken(spacetimeHttpUrl, dbName, token) {
try {
// Cheapest valid request: a SQL query that the module owner / any identity
// can run. Returns HTTP 200 if token signature is valid, 401 if not.
const res = await fetch(`${spacetimeHttpUrl}/v1/database/${dbName}/sql`, {
const res = await fetch(`${spacetimeHttpUrl}/v1/database/${encodeURIComponent(dbName)}/sql`, {
method: "POST",
headers: { "Authorization": `Bearer ${token}` },
body: "SELECT 1",