OtterMind/Chat2DB · error · IllegalArgumentException

Invalid MongoDB database name: {name}

Error message

Invalid MongoDB database name: {name}

What it means

Thrown by MongodbSqlGuards.requireDatabaseName when the token interpolated into a Mongo 'use <database>' command fails the pattern ^[A-Za-z0-9_$-]+$. The guard exists because the 'use' command is a non-escapable position: the name is pasted raw into shell text, so an invalid character would either break the command or enable command injection. A null name is also rejected.

Source

Thrown at chat2db-community-server/chat2db-community-plugins/chat2db-community-mongodb/src/main/java/ai/chat2db/plugin/mongodb/MongodbSqlGuards.java:20

import java.util.regex.Pattern;

/**
 * Context-specific validation and JSON escaping for Mongo shell command text.
 */
public final class MongodbSqlGuards {

    private static final Pattern DATABASE_NAME_PATTERN = Pattern.compile("^[A-Za-z0-9_$-]+$");

    private MongodbSqlGuards() {
    }

    /**
     * Validates the unquoted token used by the Mongo {@code use <database>} command.
     */
    public static String requireDatabaseName(String name) {
        if (name == null || !DATABASE_NAME_PATTERN.matcher(name).matches()) {
            throw new IllegalArgumentException("Invalid MongoDB database name: " + name);
        }
        return name;
    }

    /**
     * Returns a property-safe collection accessor. MongoDB collection names are
     * not JavaScript identifiers, so dot-property interpolation is not valid for
     * names containing dots, hyphens, or a leading digit.
     */
    public static String collectionAccessor(String name) {
        requireNonEmptyName(name, "collection name");
        return "getCollection(" + quoteJsonString(name) + ")";
    }

    /**
     * Returns a quoted object key for a MongoDB field name.
     */
    public static String quoteFieldName(String name) {

View on GitHub (pinned to 5ee1e990e7)

Solutions

  1. Trim the input and strip surrounding quotes/whitespace before calling requireDatabaseName.
  2. Ensure the database name matches ^[A-Za-z0-9_$-]+$; reject dots, spaces, and slashes upstream in the UI/API layer.
  3. If the name is genuinely invalid for Mongo, surface a validation error to the user instead of reaching the builder.
  4. Confirm the caller is not accidentally passing the connection string path or a fully-qualified collection name as the database name.

Example fix

// before
String db = request.getDatabase();
MongodbSqlGuards.requireDatabaseName(db);

// after
String db = StringUtils.trimToNull(request.getDatabase());
if (db == null || !db.matches("^[A-Za-z0-9_$-]+$")) {
    throw new IllegalArgumentException("Invalid MongoDB database name: " + db);
}
MongodbSqlGuards.requireDatabaseName(db);
Defensive patterns

Strategy: validation

Validate before calling

String name = StringUtils.trimToNull(rawName);
if (name == null || !name.matches("^[A-Za-z0-9_$-]+$")) {
    return; // or throw a user-facing validation error
}
MongodbSqlGuards.requireDatabaseName(name);

Type guard

static boolean isValidMongoDatabaseName(String name) {
    return name != null && name.matches("^[A-Za-z0-9_$-]+$");
}

Try / catch

try {
    MongodbSqlGuards.requireDatabaseName(db);
} catch (IllegalArgumentException e) {
    // surface a field-validation error; do not log the raw name if it may contain secrets
    throw new BusinessException("mongodb.database.nameInvalid");
}

Prevention

When it happens

Trigger: Calling requireDatabaseName(name) (directly or via a Mongo DDL builder that issues 'use <db>') with a name containing a dot, slash, space, leading digit-only, quote, or any char outside [A-Za-z0-9_$-], or passing null.

Common situations: A user supplies a database name copied from an external source that contains a '.' (common since MongoDB databases sometimes appear dotted in tooling), a space, or a unicode character; or the caller passes an untrimmed/empty string; or a name is read from a config file that includes surrounding quotes.

Related errors


AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14). Data as JSON: /api/errors/1ca2c7127157ef83. Report an issue: GitHub.