Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-1619625: Insufficient character unescaping #1868

Open
bauer-at-work opened this issue Aug 9, 2024 · 6 comments
Open

SNOW-1619625: Insufficient character unescaping #1868

bauer-at-work opened this issue Aug 9, 2024 · 6 comments
Assignees
Labels
bug status-pr_pending_merge A PR is made and is under review status-triage_done Initial triage done, will be further handled by the driver team

Comments

@bauer-at-work
Copy link

  1. What version of JDBC driver are you using?
    latest from git

  2. What operating system and processor architecture are you using?
    Fedora 40, amd64

  3. What version of Java are you using?
    OpenJDK 64-Bit Server VM (Red_Hat-21.0.4.0.7-2) (build 21.0.4+7, mixed mode, sharing)

  4. What did you do?

    Properties properties = new Properties();
    properties.put("db", "DEV");
    properties.put("schema", "\\\\_ESCAPED\\\\_UNDERSCORE");
    properties.put("CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX", "TRUE");
    
    ...
    
    connection.getMetaData().getSchemas(); // Returns a line for the precise schema
    connection.getMetaData().getTables(null, null, null, null); // Query fails

    Snowflake Query Monitor reveals the following queries being executed:

    show /* JDBC:DatabaseMetaData.getSchemas() */ schemas like 'ESCAPED\\_UNDERSCORE' in database "DEV";
    show /* JDBC:DatabaseMetaData.getTables() */ objects in schema "DEV"."ESCAPED\_UNDERSCORE";
  5. What did you expect to see?

    The first SQL statement is expected and correct. Two backslashes are required for the sequence to be interpreted as an escaping of the underscore character.

    The second SQL statement is unexpected. Now that that metadata call results not in a LIKE operation but in a statement expecting a valid schema name, both backslash characters should have been 'unescaped'.

    The problem can be traced back to SnowflakeDatabaseMetaData::unescapeChars(...).
    The function is only removing one of the two backslahes.

  6. Fix

    I had success with switching the order of statements as follows:

    private String unescapeChars(String escapedString) {
      String unescapedString = escapedString.replace("\\\\", "\\");
      unescapedString = unescapedString.replace("\\_", "_");
      unescapedString = unescapedString.replace("\\%", "%");
      unescapedString = escapeSqlQuotes(unescapedString);
      return unescapedString;
    }

    However, I might be missing other edge cases now.

@github-actions github-actions bot changed the title Insufficient character unescaping SNOW-1619625: Insufficient character unescaping Aug 9, 2024
@bauer-at-work
Copy link
Author

also, THERE SHOULD BE A TEST CASE FOR unescapeChars!

@sfc-gh-jszczerbinski sfc-gh-jszczerbinski self-assigned this Aug 21, 2024
@sfc-gh-dszmolka sfc-gh-dszmolka added the status-triage_done Initial triage done, will be further handled by the driver team label Sep 12, 2024
@bauer-at-work
Copy link
Author

Hi @sfc-gh-snow-drivers-warsaw-dl Hi, can you please give an estimate on when this can be fixed?
We @ Siemens are experiencing big slowdowns in integrating with our Snowflake instance.

@sfc-gh-dszmolka
Copy link
Contributor

hi - the team is looking into this issue, which seems to be more complex than it might seem at the first glance. We would like to also take a holistic approach to the issue and rework today's (little bit inconsistent) behaviour . The full rewrite can be only expected in the next major version of the JDBC driver.

In the meantime, we're looking into making a workaround available which could be even implemented before the next major release. Please stay tuned and thank you for bearing with us - i'll keep this thread posted.

@sfc-gh-dszmolka sfc-gh-dszmolka added the status-in_progress Issue is worked on by the driver team label Dec 19, 2024
@sfc-gh-dprzybysz
Copy link
Collaborator

Hi @bauer-at-work
I prepared a PR with fix for schema lookup guarded by the session parameter ENABLE_EXACT_SCHEMA_SEARCH_ENABLED.
I think the correct approach should be to not escape schema in connection parameters and the metadata queries with CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX=true should use the schema not as a wildcard.
You can take a look if you want.

@bauer-at-work
Copy link
Author

Hi @sfc-gh-dprzybysz,
I thought exactly the same when coming back to this topic today -
Interpreting the underscores as wildcards constitutes undefined behavior.

When do you think the correct approach can be implemented?
With your current fix (workaround?), how do I need to specify my schema? Do I have to escape the underscores in the schema name still?

@sfc-gh-dprzybysz
Copy link
Collaborator

With the fix in PR the schema should be without any escaping applied when passed as connection parameter. When schema is test_bla%123 it should be passed as test_bla%123.

There are some flags and workarounds in fetching metadata functions - there are some plans to simplify the configuration and functions but probably it will result in behaviour changes so it won't become a default behaviour for current 3.x line

@sfc-gh-dszmolka sfc-gh-dszmolka added status-pr_pending_merge A PR is made and is under review and removed status-in_progress Issue is worked on by the driver team labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug status-pr_pending_merge A PR is made and is under review status-triage_done Initial triage done, will be further handled by the driver team
Projects
None yet
Development

No branches or pull requests

5 participants