Skip to content

Too much escaping in PostgreSQL JOINs #7712

Backlog

Description

@ejn

Since the changes made in #7546 there is I think too much escaping being used in JOINs using PostgreSQL.

Before that change it was possible to define TABLE for the join including a schema name, e.g. JOIN ... TABLE "my_schema.my_table", which would be turned into SQL ... FROM myschema.mytable.

Since #7546, the TABLE is sent through msDefaultEscapePropertyName(), turning the generated SQL into ... FROM "myschema.mytable". Since msDefaultEscapePropertyName() functions thoroughly, it is not possible to persuade Mapserver to generate the SQL as previously.

My proposed fix is to revert the changes made in #7546 w.r.t. the escaping for TABLE. It would imho then be consistent to also revert the changes made w.r.t. escaping for TO. There should then be a documentation note added, that the author of the mapfile is responsible for providing TABLE and TO as suitably escaped strings for direct use in SQL.

Basically I'm proposing to effectively remove the following in mappostgresql.c which was added in #7546:

    char *pszEscapedTable = msDefaultEscapePropertyName(join->table);
    char *pszEscapedTo = msDefaultEscapePropertyName(join->to);

In my view this doesn't have any security impact as both these items can only be controlled by the author of the mapfile - neither the data contents nor the request parameters are relevant here and so an attacker cannot use them for SQL-injection.

The changes for escaping of joininfo->from_value must of course remain.

Any thoughts on this before I prepare a PR? Have I missed something in my analysis?

Activity

  1. added theissue type on Dec 1, 2026
  2. jratike80 commented on Dec 1, 2026

    @jratike80

    I believe that JOIN ... TABLE "my_schema.my_table" should be JOIN ... TABLE "my_schema"."my_table" in proper SQL. But I do not know how MapServer alters the query.

  3. ejn commented on Dec 1, 2026

    @ejn
    ContributorAuthor

    @jratike80 precisely, the problem is that myschema.mytable gets escaped as a single simple name, when it's actually a scoped name or two names ([schema name].[table name]).

    The problem with doing anything between "escape as a single name" and "nothing" is that things like "my.schema"."my.table" (a table my.table in a schema my.schema) or just "my.table" (table my.table in some schema such as public on the search path) is completely valid, so any attempt to split a string at . and escape each part separately is doomed to failure at some point.

  4. ejn commented on Dec 1, 2026

    @ejn
    ContributorAuthor

    Note that if we are escaping names in PostgreSQL then we should be really using the function PQescapeIdentifier (available since PG9)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions