pwg_query is escaping 'groups' and 'rank' in the query, without taking into account if the word is not already in a quoted part of the query (in strings in particular).
This bug already shows up in #1292
I've also been hit by this bug with the Ldap_Login plugin.
This plugin calls mass_updates that calls mass_insert that calls pwg_query
Managing ldap config, it tries for example to insert the following data (from mariadb logs):
You see the problem: the group in the data must not be quoted here.
I'm under the impression that the decision of escaping word in query without parsing it in detail (to avoid to quote in quoted string) is wrong.
What do you suggest? I'm thinking of various fixes:
changing the default behavior of pwg_query to avoid quoting by default
adding the $escape_reserved_words to all core functions that calls pwg_query (at least mass_updates and mass_insert for the Ldap_Login plugin (seems intrusive to me)
fixing the quoting in pwg_query to avoid to modify already quoted strings (probably difficult to implement)
removing the whole quoting stuff in pwg_query and fixing code that does not quote correctly its data/column names/etc (seems the good way for me)
Regards
Vincent
`pwg_query` is escaping 'groups' and 'rank' in the query, without taking into account if the word is not already in a quoted part of the query (in strings in particular).
This bug already shows up in #1292
I've also been hit by this bug with the Ldap_Login plugin.
This plugin calls `mass_updates` that calls `mass_insert` that calls `pwg_query`
Managing ldap config, it tries for example to insert the following data (from mariadb logs):
```
2021-02-15T20:53:01.691167Z 265 Query INSERT INTO `piwigo_ldap_login_config_1613422381676754`
(`param`,`value`)
VALUES
[...]
, ('ld_group_basedn','cn=`groups`,dc=domain,dc=tld')
[...]
```
You see the problem: the `group` in the data must not be quoted here.
I'm under the impression that the decision of escaping word in query without parsing it in detail (to avoid to quote in quoted string) is wrong.
What do you suggest? I'm thinking of various fixes:
1) changing the default behavior of `pwg_query` to avoid quoting by default
2) adding the `$escape_reserved_words` to all core functions that calls `pwg_query` (at least `mass_updates` and `mass_insert` for the Ldap_Login plugin (seems intrusive to me)
3) fixing the quoting in `pwg_query` to avoid to modify already quoted strings (probably difficult to implement)
4) removing the whole quoting stuff in `pwg_query` and fixing code that does not quote correctly its data/column names/etc (seems the good way for me)
Regards
Vincent
<!-- github-sync-id:808827497 -->
Note: modifying safe_unserialize as suggested in #1292 seems wrong to me. In this case, we wont be able to insert data such as ' groups and groups ' => currently, both will be quoted. Modifying safe_unserialize will lead to both being unquoted...
Note: modifying `safe_unserialize` as suggested in #1292 seems wrong to me. In this case, we wont be able to insert data such as ' groups and `groups` ' => currently, both will be quoted. Modifying `safe_unserialize` will lead to both being unquoted...
<!-- github-comment-id:779456061 -->
Fixing the actual now wrong code places to escape groups column names would be preferable over hitting all with a big club and introducing new problems.
See also the still pending plugins PRs:
https://github.com/Piwigo/AMenuManager/pull/7
https://github.com/Piwigo/GrumPluginClasses/pull/3
https://github.com/Piwigo/PWG_Stuffs/pull/12
Fixing the actual now wrong code places to escape `groups` column names would be preferable over hitting all with a big club and introducing new problems.
<!-- github-comment-id:782596813 -->
I agree solution 4 is the best one. The "short term" solution I implemented brings too much unexpected side effects.
I agree solution 4 is the best one. The "short term" solution I implemented brings too much unexpected side effects.
<!-- github-comment-id:782703122 -->
If it can help a bit, here is the patch I did to remove this quoting in my development version. Not sure I found all the places (in particular, I did not look for 'groups'), but it is a start. patch.txt
If it can help a bit, here is the patch I did to remove this quoting in my development version. Not sure I found all the places (in particular, I did not look for 'groups'), but it is a start.
[patch.txt](https://github.com/Piwigo/Piwigo/files/6018339/patch.txt)
<!-- github-comment-id:782911957 -->
@vdanjean I would like to keep the solution 4 for Piwigo 12 (I don't want plugins relying on this auto-escape to be broken in Piwigo 11.4.0). Can you find a way to avoid the automatic escaping in Ldap plugin?
@vdanjean I would like to keep the solution 4 for Piwigo 12 (I don't want plugins relying on this auto-escape to be broken in Piwigo 11.4.0). Can you find a way to avoid the automatic escaping in Ldap plugin?
<!-- github-comment-id:792787927 -->
with default $conf['session_save_handler'] = 'db, if the session contains "groups" (*), session can not be read:
session_start(): Failed to decode session object. Session has been destroyed
Suggested fix: pwg_query($query, false) in function pwg_session_write
Workaround: force the default PHP session handle with $conf['session_save_handler'] = null;
(*) example : $_SESSION['foo'] = 'groups'
Similar issue with pwg_session_* :
with default `$conf['session_save_handler'] = 'db`, if the session contains "groups" (*), session can not be read:
```
session_start(): Failed to decode session object. Session has been destroyed
```
Suggested fix: `pwg_query($query, false)` in function `pwg_session_write`
Workaround: force the default PHP session handle with `$conf['session_save_handler'] = null;`
(*) example : `$_SESSION['foo'] = 'groups'`
<!-- github-comment-id:806534946 -->
Same problem if a physical to be synced directory contains a `groups` name, see https://piwigo.org/forum/viewtopic.php?id=31183
<!-- github-comment-id:817139078 -->
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
pwg_queryis escaping 'groups' and 'rank' in the query, without taking into account if the word is not already in a quoted part of the query (in strings in particular).This bug already shows up in #1292
I've also been hit by this bug with the Ldap_Login plugin.
This plugin calls
mass_updatesthat callsmass_insertthat callspwg_queryManaging ldap config, it tries for example to insert the following data (from mariadb logs):
You see the problem: the
groupin the data must not be quoted here.I'm under the impression that the decision of escaping word in query without parsing it in detail (to avoid to quote in quoted string) is wrong.
What do you suggest? I'm thinking of various fixes:
pwg_queryto avoid quoting by default$escape_reserved_wordsto all core functions that callspwg_query(at leastmass_updatesandmass_insertfor the Ldap_Login plugin (seems intrusive to me)pwg_queryto avoid to modify already quoted strings (probably difficult to implement)pwg_queryand fixing code that does not quote correctly its data/column names/etc (seems the good way for me)Regards
Vincent
Note: modifying
safe_unserializeas suggested in #1292 seems wrong to me. In this case, we wont be able to insert data such as ' groups andgroups' => currently, both will be quoted. Modifyingsafe_unserializewill lead to both being unquoted...See also the still pending plugins PRs:
https://github.com/Piwigo/AMenuManager/pull/7
https://github.com/Piwigo/GrumPluginClasses/pull/3
https://github.com/Piwigo/PWG_Stuffs/pull/12
Fixing the actual now wrong code places to escape
groupscolumn names would be preferable over hitting all with a big club and introducing new problems.I agree solution 4 is the best one. The "short term" solution I implemented brings too much unexpected side effects.
If it can help a bit, here is the patch I did to remove this quoting in my development version. Not sure I found all the places (in particular, I did not look for 'groups'), but it is a start.
patch.txt
@vdanjean I would like to keep the solution 4 for Piwigo 12 (I don't want plugins relying on this auto-escape to be broken in Piwigo 11.4.0). Can you find a way to avoid the automatic escaping in Ldap plugin?
Similar issue with pwg_session_* :
with default
$conf['session_save_handler'] = 'db, if the session contains "groups" (*), session can not be read:Suggested fix:
pwg_query($query, false)in functionpwg_session_writeWorkaround: force the default PHP session handle with
$conf['session_save_handler'] = null;(*) example :
$_SESSION['foo'] = 'groups'Same problem if a physical to be synced directory contains a
groupsname, see https://piwigo.org/forum/viewtopic.php?id=31183