Bug 426125

Summary: The 'Link to application' option 'Run in terminal' can overwrite an application configuration file
Product: [Frameworks and Libraries] frameworks-kio Reporter: Keven L. Ates <atescomp>
Component: generalAssignee: KIO Bugs <kio-bugs-null>
Status: REPORTED ---    
Severity: major CC: a.samirh78, kdelibs-bugs-null, nate, plasma-bugs-null
Priority: NOR    
Version First Reported In: 5.68.0   
Target Milestone: ---   
Platform: Kubuntu   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Keven L. Ates 2020-09-02 21:44:41 UTC
SUMMARY

When I turn on the 'Run in terminal' option in a 'Link to application' on the desktop, some process hijacks the options for the application and overwrites the applications configuration file.  This, in turn, destroys the application's ability to operate.

STEPS TO REPRODUCE
1. Create a 'Link to application'
2. Setup the application command to include a "--config" option for the application
3. In "Advanced Options" select "Run in terminal" and "Do not close when command exits"
4. Select OK and OK to create the desktop link
5. Run the new desktop link

OBSERVED RESULT
The file pointed to by the "--config" option for the application is corrupted / overwritten losing all setting to effectively operate the application.

EXPECTED RESULT
The config file for the application is not altered / corrupted / overwritten by other processes.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Kubuntu 20.04
(available in About System)
KDE Plasma Version: 5.18.5
KDE Frameworks Version: 5.68.0
Qt Version: 5.12.8

ADDITIONAL INFORMATION
By making the applications file Read Only, the system reports the culprit when the application is started in a dialog:

konsole - Dialog
(i) Configuration file "/home/<..redacted..>/Bin/agraph-7.0.2/lib/agraph.cfg" not writable. Please contact your system administrator.

When starting konsole with a --config option and a file it reports: "Unknown option 'config'."  Help on konsole also does not report the option.  So, what is the major malfunction?  Indeed, some process within or about konsole is taking the options for their own.  The application still receives the options and proceeds.  However, at some point, the config file is overwritten by the other process and corrupting the application.  When the file is Read/Write, the overwritten file contents show the following:
----------------------------------------
[Desktop Entry]
DefaultProfile=

[Favorite Profiles]
Favorites=

[MainWindow]
State=AAAA/wAAAAD9AAAAAAAAAoAAAAHCAAAABAAAAAQAAAAIAAAACPwAAAAA
ToolBarsMovable=Disabled
----------------------------------------

I've placed the application command in quotes to try to mitigate to no avail.
Comment 1 Ahmad Samir 2020-09-15 16:33:24 UTC
What's the "Command" you're using?
Comment 2 Keven L. Ates 2020-09-15 22:23:56 UTC
(In reply to Ahmad Samir from comment #1)
> What's the "Command" you're using?

In my case, I'm starting AllegroGraph.  An example is:

/home/joe/tmp/ag7.0.2/bin/agraph-control --config /home/joe/tmp/ag7.0.2/lib/agraph.cfg start

However, I suspect any command using a --config option will produce the same type of result.

The command "grep --config file" should do it even if grep fails as the underlying desktop shell seems to be grabbing the file of the --config option and overwriting with desktop app state config information.  I'll test such a scenario and report back.
Comment 3 Ahmad Samir 2020-09-15 22:42:31 UTC
Technically what the .desktop file does is pass the command to konsole (assuming that's your default terminal emulator), and since you specified "do not close on exit" the command will be something like:
konsole --noclose -e "/home/joe/tmp/ag7.0.2/bin/agraph-control --config /home/joe/tmp/ag7.0.2/lib/agraph.cfg start"

could you try this, in terminal:
export QT_LOGGING_RULES="*kio*=true;"
dolphin

note where the output in the terminal is then go to the desktop folder, you should find the link you created there, open it, and post the output from the terminal (from after you open the .desktop file) here.

Generally speaking most command parse "--someword" as a parameter, for example if you want to grep "--config" in a file you'll have to use something like:
grep -- --config /path/to/somefile

the -- tells grep that all the string after it are not parameters.

In konsoles case everything after "-e" isn't treated as parameters/options to konsole.
Comment 4 Keven L. Ates 2020-09-16 22:01:35 UTC
(In reply to Ahmad Samir from comment #3)

So, yes, I understood the konsole process and, yes, konsole is the default terminal emulator.  The structure of the grep command is irrelevant for the example.  In fact the command is irrelevant.  I was pointing out that it probably didn't matter where or how the "--config" option is supplied.  The value after the option is used by some mechanism (konsole?) to created/overwrite a file named by the value--even if the command fails.

For my test:
1. I opened konsole and executed this on the command line:
       konsole --noclose -e grep --console ~/Bin/test.cfg
   This did *not* produce the "test.cfg" file (as expected).

2. I created a .desktop file, "TermTest.desktop", containing:
      [Desktop Entry]
      Comment[en_US]=
      Comment=
      Exec=grep --config ~/Bin/test.cfg
      GenericName[en_US]=
      GenericName=
      Icon=network-server-database
      MimeType=
      Name[en_US]=TermTest
      Name=TermTest
      Path=/home/me/Bin
      StartupNotify=true
      Terminal=true
      TerminalOptions=\s--noclose
      Type=Application
      X-DBUS-ServiceName=
      X-DBUS-StartupType=none
      X-KDE-RunOnDiscreteGpu=false
      X-KDE-SubstituteUID=false
      X-KDE-Username=
NOTE: I found that the "Path" key must be set to an absolute directory. If I set it to "~/Bin", the desktop file would not execute.  But, that's another issue...

3. I double click the "TermTest.desktop" icon to execute.  The konsole app opens and the command fails (as expected):
      [=============================================]
      [            TermTest -- Konsole              ]
      [                                             ]
      [ /bin/grep: unrecognized option '--config'   ]
      [ Usage: grep [OPTION]... PATTERNS [FILE]...  ]
      [ Try 'grep --help' for more information.     ]
      [                                             ]
      [=============================================]
However, a new file is generated in the ~/Bin directory, test.cfg (*not* as expected):
      [MainWindow]
      State=AAAA/wAAAAD9AAAAAAAAAoAAAAHCAAAABAAAAAQAAAAIAAAACPwAAAAA
      ToolBarsMovable=Disabled
After the app is closed, the file, test.cfg, is updated:
      [Desktop Entry]
      DefaultProfile=
      
      [Favorite Profiles]
      Favorites=
      
      [MainWindow]
      State=AAAA/wAAAAD9AAAAAAAAAoAAAAHCAAAABAAAAAQAAAAIAAAACPwAAAAA
      ToolBarsMovable=Disabled

4. Doing as you suggested:
> export QT_LOGGING_RULES="*kio*=true;"
> dolphin
Double clicking the "TermTest.desktop" file creates a dialog asking to "open", "execute", or "cancel".  Choosing "execute" produced no following results in the terminal output.
<(from after you open the .desktop file) here.
So, there is nothing to post.  However, the same results for step 3 were produced: test.cfg created (*not* as expected).

5. I changed the command in the .desktop file from "grep" to "xyzzy", a non-existent command.  The same results for step 3 were produced: test.cfg created (*not* as expected).  Of course, the output was different:


      Warning: Could not find 'xyzzy', starting '/bin/bash' instead.
      Please check your profile settings.

      /bin/bash: --config: invalid option
      Usage:  /bin/bash [GNU long option] [option] ...
              /bin/bash [GNU long option] [option] script-file ...
     ...blah, blah, blah...

6. I execute "TermTest.desktop" as per 3, but unchecked "Do not close when command exits". The same end result for test 3 is produced: test.cfg created (*not* as expected).

7. I execute "TermTest.desktop" as per 3, but unchecked "Run in terminal".  I get a dialog: 
      [======================================]
      [           Sorry--Plasma              ]
      [                                      ]
      [  Could not find the program 'xyzzy'  ]
      [                                      ]
      [======================================]
No "test.cfg" file is produced (as expected).

So, obviously, the .desktop file process is executing some alien terminal processing command that is taking the --config option as its own.  If it's konsole, it is inconsistent with the konsole executed on the command line.  Additionally, konsole does not report that it uses a --config option.
Comment 5 Keven L. Ates 2021-06-14 16:18:08 UTC
I have validated that the overwritten/created configuration file is equivalent to a default /home/<user>/.config/konsolerc file.  So, why is konsole behaving bad in this use case?

>$ konsole --version
>konsole 19.12.3
Comment 6 Keven L. Ates 2021-06-14 16:56:55 UTC
Reviewing comment 3, there was no instruction for running a ".desktop" file in a terminal.  However, searches indicated this might be done.  The result of such a run follow.  The command to run a desktop file from a terminal is:

  kioclient5 exec <your.desktop>

The TermTest.desktop file:
NOTE: The <user> (the current user) in the "Path=" line is a holder for an actual user directory and the "Bin" directory from the "Exec=" and "Path=" lines exists in the <user> directory.

> [Desktop Entry]
> Comment[en_US]=
> Comment=
> Exec=xyzzy --config ~/Bin/test.cfg
> GenericName[en_US]=
> GenericName=
> Icon=network-server-database
> MimeType=
> Name[en_US]=TermTest
> Name=TermTest
> Path=/home/<user>/Bin
> StartupNotify=true
> Terminal=true
> TerminalOptions=\s--noclose
> Type=Application
> X-DBUS-ServiceName=
> X-DBUS-StartupType=none
> X-KDE-RunOnDiscreteGpu=false
> X-KDE-SubstituteUID=false
> X-KDE-Username=

Executing the TermTest.desktop file in a terminal with the QT Logging Rules:

>~$ cd Desktop
>~/Desktop$ export QT_LOGGING_RULES="*kio*=true;"
>~/Desktop$ kioclient5 exec TermTest.desktop
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/recentlyused.so" supports protocols ("recentlyused")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/ftp.so" supports protocols ("ftp")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/audiocd.so" supports protocols ("audiocd")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/remote.so" supports protocols ("remote")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/file.so" supports protocols ("file")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/http.so" supports protocols ("http", "https", "webdav", "webdavs")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/gdrive.so" supports protocols ("gdrive")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/trash.so" supports protocols ("trash")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/help.so" supports protocols ("help")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/ghelp.so" supports protocols ("ghelp")
>kf5.kio.core: "/usr/lib/x86_64-linux-gnu/qt5/plugins/kf5/kio/kdeconnect.so" supports protocols ("kdeconnect")
>~/Desktop$ "Could not find binary: " "xyzzy"

Additionally, as show in the "Exec=" line, this process erroneously produces a ~/Bin/test.cfg file containing a default konsolerc configuration:

> [Desktop Entry]
> DefaultProfile=
> 
> [Favorite Profiles]
> Favorites=
> 
> [MainWindow]
> State=AAAA/wAAAAD9AAAAAAAAAoAAAAHCAAAABAAAAAQAAAAIAAAACPwAAAAA
> ToolBarsMovable=Disabled
Comment 7 Keven L. Ates 2021-06-14 17:15:02 UTC
Replacing the "Exec=" line:
> Exec=xyzzy --config ~/Bin/test.cfg
with:
> Exec="xyzzy --config ~/Bin/test.cfg"
and running:
>~/Desktop$ kioclient5 exec TermTest.desktop
does NOT produce the "test.cfg" file.

Changing the same with the AllegroGraph desktop file also fixes the problem.
Should this be fixed to automate the quotes when executing?
At the very least, it should be documented with the side effects explained.