You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: add support for installing additional postgres extensions - #11888
This PR addresses #7584 to introduces the functionality to install additional Postgres extensions during connection setup. Verify changes through updated tests for custom extension installation. Documentation reflects the new extension options.
Pull-Request Checklist
Code is up-to-date with the master branch
This pull request links relevant issues as Fixes #7584
There are new or updated tests validating the change (tests/**.test.ts)
Documentation has been updated to reflect this change (docs/docs/**.md)
Provide an option to add additional postgres extensions when connecting to a database
Extensions should be configurable via an array of strings in the connection configuration
Extensions should be installed at the same time as other built-in extensions (like uuid-ossp)
Should work with development workflows using synchronize
Should ensure necessary database features are enabled automatically
Requires further human verification:
Verify that the extension installation works correctly in actual PostgreSQL environments with various extension types
Test the behavior when extensions require superuser privileges
Validate the warning messages are displayed appropriately in different scenarios
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 Security concerns
SQL injection: The extension names from the extensions configuration option are directly interpolated into SQL queries using template literals (line 597: CREATE EXTENSION IF NOT EXISTS "${extension}"). While PostgreSQL identifiers are quoted, there's no validation that the extension name is a valid identifier or doesn't contain malicious characters. An attacker who can control the configuration could potentially inject SQL by crafting extension names that break out of the quoted context or exploit PostgreSQL-specific syntax. Extension names should be validated against a whitelist pattern (e.g., alphanumeric and underscores only) before being used in SQL queries.
Extension names from user configuration are directly interpolated into SQL queries without validation or sanitization. Malicious extension names could potentially break out of the quoted identifier context.
The catch block uses an empty catch with underscore parameter, silently swallowing all errors. This makes debugging difficult and could hide important error information beyond just permission issues.
}catch(_){logger.log("warn",`The '${extension}' extension cannot be installed automatically. Please install it manually using superuser rights`,)}
The condition checks if availableExtensions.size > 0 before validating extension availability, but an empty set would bypass validation entirely. This could lead to attempting to install extensions without proper checking.
protected async enableCustomExtensions(
availableExtensions: Set<string>,
extensionsToInstall: string[],
connection: any,
) {
if (!extensionsToInstall) return
const logger = this.connection.logger
for (const extension of extensionsToInstall) {
+ // Validate extension name to prevent SQL injection+ if (!/^[a-zA-Z0-9_]+$/.test(extension)) {+ logger.log(+ "warn",+ `Invalid extension name "${extension}". Extension names must contain only alphanumeric characters and underscores.`,+ )+ continue+ }
if (availableExtensions.has(extension)) {
try {
await this.executeQuery(
connection,
`CREATE EXTENSION IF NOT EXISTS "${extension}"`,
)
} catch (_) {
logger.log(
"warn",
`The extension "${extension}" cannot be installed automatically. Please install it manually using superuser rights`,
)
}
} else {
logger.log(
"warn",
`The extension "${extension}" is not available on this database. Please install it manually using superuser rights`,
)
}
}
}
Apply / Chat
Suggestion importance[1-10]: 9
__
Why: The suggestion correctly identifies a potential SQL injection vulnerability by directly using a user-configured string in a query and proposes a valid fix by sanitizing the input, which is a critical security enhancement.
protected async enableCustomExtensions(
availableExtensions: Set<string>,
extensionsToInstall: string[],
connection: any,
) {
if (!extensionsToInstall) return
const logger = this.connection.logger
for (const extension of extensionsToInstall) {
+ // Validate extension name to prevent SQL injection+ if (!/^[a-zA-Z0-9_]+$/.test(extension)) {+ logger.log(+ "warn",+ `Invalid extension name "${extension}". Extension names must contain only alphanumeric characters and underscores.`,+ )+ continue+ }
if (availableExtensions.has(extension)) {
try {
await this.executeQuery(
connection,
`CREATE EXTENSION IF NOT EXISTS "${extension}"`,
)
} catch (_) {
logger.log(
"warn",
`The extension "${extension}" cannot be installed automatically. Please install it manually using superuser rights`,
)
}
} else {
logger.log(
"warn",
`The extension "${extension}" is not available on this database. Please install it manually using superuser rights`,
)
}
}
}
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a potential security vulnerability where an unsanitized string from connection options is used in a raw query, and provides a valid fix to harden the code.
Medium
General
Avoid unnecessary database query for extensions
Avoid an unnecessary database query by checking if extensionsToInstall is not empty before calling getAvailableExtensions.
Why: The suggestion correctly points out an unnecessary database call and proposes a valid optimization by adding a length check, improving performance when extensions is an empty array.
await this.executeQuery(
connection,
- `CREATE EXTENSION IF NOT EXISTS "${extension}"`,+ `CREATE EXTENSION IF NOT EXISTS ${connection.escapeIdentifier(extension)}`,
)
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies a potential SQL injection vulnerability by using an unescaped variable in a raw query. While the proposed fix using connection.escapeIdentifier is not a valid method on the connection object, it highlights a critical security flaw that should be addressed, making it a high-impact suggestion.
Medium
General
Log error details for debugging
In the catch block of getAvailableExtensions, log the actual error details instead of a generic warning message to improve debuggability of database connectivity or permission issues.
-} catch (_) {+} catch (error) {
logger.log(
"warn",
- "Could not retrieve available extensions. Extension installation may fail if extensions are not available.",+ `Could not retrieve available extensions: ${error.message}. Extension installation may fail if extensions are not available.`,
)
}
Suggestion importance[1-10]: 6
__
Why: The suggestion correctly points out that swallowing the error details makes debugging harder. Logging the error message would significantly improve the developer experience when troubleshooting issues with extension installation, such as permission errors.
Low
Remove redundant null check
Remove the redundant if (!extensionsToInstall) return check from the enableCustomExtensions method, as it is already guaranteed to be truthy by the calling code in afterConnect.
Why: The suggestion correctly points out a redundant null check. Removing it would be a minor code quality improvement, but the check also makes the method safer for future use in other contexts, so its removal has low impact.
if (extensionsToInstall && extensionsToInstall.length > 0) {
for (const extension of extensionsToInstall) {
if (
availableExtensions &&
availableExtensions.size > 0 &&
!availableExtensions.has(extension)
) {
logger.log(
"warn",
`Extension '${extension}' is not available in your PostgreSQL installation. Please check the extension name or install it using superuser rights.`,
)
continue
}
try {
+ // Escape double quotes to prevent SQL injection.+ const escapedExtension = extension.replace(/"/g, '""')
await this.executeQuery(
connection,
- `CREATE EXTENSION IF NOT EXISTS "${extension}"`,+ `CREATE EXTENSION IF NOT EXISTS "${escapedExtension}"`,
)
} catch (_) {
logger.log(
"warn",
`The '${extension}' extension cannot be installed automatically. Please install it manually using superuser rights`,
)
}
}
}
Suggestion importance[1-10]: 10
__
Why: The suggestion correctly identifies a critical SQL injection vulnerability in the newly added code and provides an accurate and effective solution to prevent it.
1. SQL injection risk in extension names 📘 Rule Violation
Description
• User-provided extension names from this.options.extensions are directly interpolated into SQL
queries without validation or sanitization.
• Malicious extension names could contain SQL injection payloads like "; DROP TABLE users; --.
• While double quotes provide some protection, they don't prevent all injection vectors and the code
lacks explicit input validation.
+ await this.executeQuery(+ connection,+ `CREATE EXTENSION IF NOT EXISTS "${extension}"`,+ )
Evidence
The compliance checklist requires that all external inputs are validated and sanitized to prevent
injection vulnerabilities. The extensions array comes from user configuration
(PostgresConnectionOptions) and is directly interpolated into SQL at line 590 using template
literals. While the existing code uses double quotes around extension names, there's no explicit
validation to ensure extension names contain only safe characters (alphanumeric, underscore,
hyphen). This creates a potential SQL injection vector.
## Issue description
User-provided extension names are directly interpolated into SQL queries without validation, creating a potential SQL injection vulnerability.
## Issue Context
The `extensions` configuration option accepts an array of strings from user configuration. These strings are used directly in CREATE EXTENSION SQL statements. While PostgreSQL identifiers have restrictions, the code should explicitly validate inputs to prevent injection attacks.
## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[585-591]
- src/driver/postgres/PostgresDriver.ts[415-415]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
2. Error details swallowed in catch blocks 📘 Rule Violation
Description
• The catch blocks at lines 452, 592 in enableCustomExtensions and getAvailableExtensions
methods discard error details by using catch (_), preventing proper debugging and monitoring.
• This violates the requirement for error messages to provide actionable context about what failed
and why.
• Without logging error details, production debugging becomes difficult when extension installation
fails.
+ } catch (_) {+ logger.log(+ "warn",+ "Could not retrieve available extensions. Extension installation may fail if extensions are not available.",+ )+ }
Evidence
The compliance rule requires that errors include relevant variable values and operation context for
production debugging. The code catches errors but discards them with catch (_), only logging
generic warning messages without the actual error details. This pattern appears in both
getAvailableExtensions (line 452) and enableCustomExtensions (line 592), making it impossible to
diagnose why extension queries or installations fail.
## Issue description
The catch blocks discard error details by using `catch (_)`, preventing proper debugging when extension operations fail.
## Issue Context
When extension queries or installations fail, developers need the actual error details (error message, stack trace, error code) to diagnose issues in production. The current implementation only logs generic warnings without the underlying error information.
## Fix Focus Areas
- src/driver/postgres/PostgresDriver.ts[452-457]
- src/driver/postgres/PostgresDriver.ts[592-597]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of change
This PR addresses #7584 to introduces the functionality to install additional Postgres extensions during connection setup. Verify changes through updated tests for custom extension installation. Documentation reflects the new extension options.
Pull-Request Checklist
masterbranchFixes #7584tests/**.test.ts)docs/docs/**.md)