Skip to content

enhance row count retrieval: add AWS-specific estimation and fallback mechanism#1270

Merged
Artuomka merged 1 commit into
mainfrom
backend_cassandra_fixes
Jul 28, 2025
Merged

enhance row count retrieval: add AWS-specific estimation and fallback mechanism#1270
Artuomka merged 1 commit into
mainfrom
backend_cassandra_fixes

Conversation

@Artuomka

Copy link
Copy Markdown
Collaborator

No description provided.

@Artuomka Artuomka merged commit a271895 into main Jul 28, 2025
11 of 12 checks passed
@Artuomka Artuomka deleted the backend_cassandra_fixes branch July 28, 2025 12:51
private isAWSConnection(): boolean {
const { host } = this.connection;

if (host.includes('cassandra') && host.includes('amazonaws.com')) {

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

'
amazonaws.com
' can be anywhere in the URL, and arbitrary hosts may come before or after it.

Copilot Autofix

AI 11 months ago

To fix the issue, we need to parse the host value and validate it against a whitelist of allowed AWS domains or patterns. Instead of using host.includes('amazonaws.com'), we should use a stricter check that ensures the host ends with .amazonaws.com and does not contain any unexpected subdomains. This can be achieved using a combination of URL parsing and regular expressions.

The fix involves:

  1. Parsing the host value to ensure it is a valid domain.
  2. Checking that the host ends with .amazonaws.com and matches expected patterns (e.g., *.amazonaws.com or ec2-*.compute.amazonaws.com).
  3. Replacing the substring checks with these stricter validations.

Suggested changeset 1
shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts b/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
--- a/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
+++ b/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
@@ -710,13 +710,9 @@
 
-    if (host.includes('cassandra') && host.includes('amazonaws.com')) {
-      return true;
-    }
-
-    if (host.includes('amazonaws.com')) {
-      return true;
-    }
-
+    const awsHostRegex = /^([a-zA-Z0-9-]+\.)*amazonaws\.com$/i;
     const ec2HostRegex = /^(ec2-).*([.]compute[.]amazonaws[.]com)$/i;
-    if (ec2HostRegex.test(host)) {
-      return true;
+
+    if (awsHostRegex.test(host)) {
+      if (host.includes('cassandra') || ec2HostRegex.test(host)) {
+        return true;
+      }
     }
EOF
@@ -710,13 +710,9 @@

if (host.includes('cassandra') && host.includes('amazonaws.com')) {
return true;
}

if (host.includes('amazonaws.com')) {
return true;
}

const awsHostRegex = /^([a-zA-Z0-9-]+\.)*amazonaws\.com$/i;
const ec2HostRegex = /^(ec2-).*([.]compute[.]amazonaws[.]com)$/i;
if (ec2HostRegex.test(host)) {
return true;

if (awsHostRegex.test(host)) {
if (host.includes('cassandra') || ec2HostRegex.test(host)) {
return true;
}
}
Copilot is powered by AI and may make mistakes. Always verify output.
return true;
}

if (host.includes('amazonaws.com')) {

Check failure

Code scanning / CodeQL

Incomplete URL substring sanitization High

'
amazonaws.com
' can be anywhere in the URL, and arbitrary hosts may come before or after it.

Copilot Autofix

AI 11 months ago

To fix the problem, we should parse the host string and check if it matches exactly amazonaws.com or is a subdomain of amazonaws.com (e.g., cassandra.amazonaws.com, ec2-xx-xx-xx-xx.compute.amazonaws.com). This can be done by splitting the host into its labels and checking if the last two labels are amazonaws.com, or by using a regular expression that matches only valid AWS hostnames. The fix should be applied in the isAWSConnection method, replacing the substring check with a more robust check. No new dependencies are required, as this can be done with standard string or regex operations.


Suggested changeset 1
shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts b/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
--- a/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
+++ b/shared-code/src/data-access-layer/data-access-objects/data-access-object-cassandra.ts
@@ -710,3 +710,7 @@
 
-    if (host.includes('cassandra') && host.includes('amazonaws.com')) {
+    // Check for AWS Cassandra service host
+    if (
+      host === 'cassandra.amazonaws.com' ||
+      host.endsWith('.cassandra.amazonaws.com')
+    ) {
       return true;
@@ -714,3 +718,7 @@
 
-    if (host.includes('amazonaws.com')) {
+    // Check for any subdomain of amazonaws.com (e.g., ec2-xx-xx-xx-xx.compute.amazonaws.com)
+    if (
+      host === 'amazonaws.com' ||
+      host.endsWith('.amazonaws.com')
+    ) {
       return true;
EOF
@@ -710,3 +710,7 @@

if (host.includes('cassandra') && host.includes('amazonaws.com')) {
// Check for AWS Cassandra service host
if (
host === 'cassandra.amazonaws.com' ||
host.endsWith('.cassandra.amazonaws.com')
) {
return true;
@@ -714,3 +718,7 @@

if (host.includes('amazonaws.com')) {
// Check for any subdomain of amazonaws.com (e.g., ec2-xx-xx-xx-xx.compute.amazonaws.com)
if (
host === 'amazonaws.com' ||
host.endsWith('.amazonaws.com')
) {
return true;
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants