Generated authorized_keys does not work on Windows #5110

Closed
opened 2025-11-02 06:14:45 -06:00 by GiteaMirror · 12 comments
Owner

Originally created by @richard-browne on GitHub (Mar 24, 2020).

  • Gitea version (or commit ref): 1.11.3
  • Git version: 2.22.0.windows.1
  • Operating system: Windows Server 2019
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist:

Description

gitea doesn't work on Windows with OpenSSH (not in-built ssh server). The problem is with the single quotes with the command in authorized_keys:

command="C:/Gitea/gitea.exe --config='C:/Gitea/custom/conf/app.ini' serv key-1"

When I run this command on Windows (without any ssh in the picture) we get the following debug output:

2020/03/25 09:36:43 ...s/setting/setting.go:531:NewContext() [W] Custom config 'C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini'' not found, ignore this if you're running first time 2020/03/25 09:36:43 ...s/setting/setting.go:785:NewContext() [F] failed to create 'C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini'': mkdir C:\Gitea\custom'C:: The filename, directory name, or volume label syntax is incorrect.`

There are two problems:

  1. The single quotes around the --config file cause gitea on Windows to report that it cannot find the file.
  2. gitea then goes ahead and tries to create "C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini''. This is an invalid path and gitea aborts.

The quick fix which works for me is to manually change the command in authorized_keys to:

command="C:/Gitea/gitea.exe --config=\"C:/Gitea/custom/conf/app.ini\" serv key-1"

With double quotes I can now use gitea over OpenSSH (not build-in ssh) on Windows. The problem now is that authorized_keys will be overwritten next time we create a user for example.

To fix I believe what needs to be done:

  1. Change gitea to output double quotes in authorized_keys, or
  2. Figure out why single quotes don't work (perhaps a go problem?)
  3. Fix gitea's parsing of argv[0] so it can handle 'C:/...'

I also question why gitea would ever be creating a template app.ini when invoked with the serv command. That doesn't seem like the right thing to do.

Originally created by @richard-browne on GitHub (Mar 24, 2020). - Gitea version (or commit ref): 1.11.3 - Git version: 2.22.0.windows.1 - Operating system: Windows Server 2019 - Database (use `[x]`): - [ ] PostgreSQL - [ ] MySQL - [ ] MSSQL - [x] SQLite - Can you reproduce the bug at https://try.gitea.io: - [ ] Yes (provide example URL) - [ ] No - [x] Not relevant - Log gist: ## Description gitea doesn't work on Windows with OpenSSH (not in-built ssh server). The problem is with the single quotes with the command in authorized_keys: > command="C:/Gitea/gitea.exe --config='C:/Gitea/custom/conf/app.ini' serv key-1" When I run this command on Windows (without any ssh in the picture) we get the following debug output: `2020/03/25` 09:36:43 ...s/setting/setting.go:531:NewContext() [W] Custom config 'C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini'' not found, ignore this if you're running first time` `2020/03/25 09:36:43 ...s/setting/setting.go:785:NewContext() [F] failed to create 'C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini'': mkdir C:\Gitea\custom\'C:: The filename, directory name, or volume label syntax is incorrect.` There are two problems: 1. The single quotes around the --config file cause gitea on Windows to report that it cannot find the file. 2. gitea then goes ahead and tries to create "C:/Gitea/custom/'C:/Gitea/custom/conf/app.ini''. This is an invalid path and gitea aborts. The quick fix which works for me is to manually change the command in authorized_keys to: > command="C:/Gitea/gitea.exe --config=\\"C:/Gitea/custom/conf/app.ini\\" serv key-1" With double quotes I can now use gitea over OpenSSH (not build-in ssh) on Windows. The problem now is that authorized_keys will be overwritten next time we create a user for example. To fix I believe what needs to be done: 1. Change gitea to output double quotes in authorized_keys, or 2. Figure out why single quotes don't work (perhaps a go problem?) 3. Fix gitea's parsing of argv[0] so it can handle 'C:/...' I also question why gitea would ever be creating a template app.ini when invoked with the serv command. That doesn't seem like the right thing to do.
GiteaMirror added the type/bug label 2025-11-02 06:14:45 -06:00
Author
Owner

@zeripath commented on GitHub (Mar 26, 2020):

The single quotes should not be being passed to Gitea AFAICS it's probably something to do with the shell.

@zeripath commented on GitHub (Mar 26, 2020): The single quotes should not be being passed to Gitea AFAICS it's probably something to do with the shell.
Author
Owner

@richard-browne commented on GitHub (Mar 26, 2020):

The single quotes should not be being passed to Gitea AFAICS it's probably something to do with the shell.

No it's nothing to do with the shell. On Windows, the command line is passed to each program as a string. Each executable parses its own command line. On Windows, single quotes are by convention not supported, only double quotes. Go follows this convention. Therefore, passing single quotes to the gitea command line on Windows won't work.

IMO this is a bug not a feature enhancement. The authorized_keys generated by gitea on Windows, is incompatible with command line parsing on Windows, and won't work.

@richard-browne commented on GitHub (Mar 26, 2020): > The single quotes should not be being passed to Gitea AFAICS it's probably something to do with the shell. No it's nothing to do with the shell. On Windows, the command line is passed to each program as a string. Each executable parses its own command line. On Windows, single quotes are by convention not supported, only double quotes. Go follows this convention. Therefore, passing single quotes to the gitea command line on Windows won't work. IMO this is a bug not a feature enhancement. The authorized_keys generated by gitea on Windows, is incompatible with command line parsing on Windows, and won't work.
Author
Owner

@zeripath commented on GitHub (Mar 26, 2020):

Dude. I literally pushed a pr that fixes it and marked the pr as a bug as I made that comment.

Thank you for figuring this out - it's been a problem in Gitea for a while.

@zeripath commented on GitHub (Mar 26, 2020): Dude. I literally pushed a pr that fixes it and marked the pr as a bug as I made that comment. Thank you for figuring this out - it's been a problem in Gitea for a while.
Author
Owner

@richard-browne commented on GitHub (Mar 28, 2020):

Thanks for the quick fix. I'll verify it as soon as the next official build is released.

@richard-browne commented on GitHub (Mar 28, 2020): Thanks for the quick fix. I'll verify it as soon as the next official build is released.
Author
Owner

@aoi-umi commented on GitHub (Sep 23, 2020):

have same error while the path having space,need to add double quotes

command="\"D:/Program Files/gitea/gitea.exe\"

@aoi-umi commented on GitHub (Sep 23, 2020): have same error while the path having space,need to add double quotes > command="\\"D:/Program Files/gitea/gitea.exe\\"
Author
Owner

@zeripath commented on GitHub (Sep 23, 2020):

@aoi-umi what version of Gitea?

@zeripath commented on GitHub (Sep 23, 2020): @aoi-umi what version of Gitea?
Author
Owner

@aoi-umi commented on GitHub (Sep 24, 2020):

1.12.4

@aoi-umi commented on GitHub (Sep 24, 2020): > 1.12.4
Author
Owner

@zeripath commented on GitHub (Sep 25, 2020):

ah I see ... you mean that:

274430f40b/models/ssh_key.go (L41)

should be:

	tplCommand       = "%q --config=%q serv key-%d"

if the setting.AppPath contains spaces.

@zeripath commented on GitHub (Sep 25, 2020): ah I see ... you mean that: https://github.com/go-gitea/gitea/blob/274430f40b50900c0a76431019e5cf1a6ade2182/models/ssh_key.go#L41 should be: ```go tplCommand = "%q --config=%q serv key-%d" ``` if the setting.AppPath contains spaces.
Author
Owner

@aoi-umi commented on GitHub (Sep 25, 2020):

no, should be

tplCommand = "\"%s\" --config=%q serv key-%d"

@aoi-umi commented on GitHub (Sep 25, 2020): no, should be > tplCommand = "\\"%s\\" --config=%q serv key-%d"
Author
Owner

@zeripath commented on GitHub (Sep 25, 2020):

@aoi-umi I think you'll find that %q is more correct: https://golang.org/pkg/fmt/

Consider what happens if there is a " in the command name.

@zeripath commented on GitHub (Sep 25, 2020): @aoi-umi I think you'll find that %q is more correct: https://golang.org/pkg/fmt/ Consider what happens if there is a `"` in the command name.
Author
Owner

@aoi-umi commented on GitHub (Sep 25, 2020):

oh,I'm sorry, i don't know golang

@aoi-umi commented on GitHub (Sep 25, 2020): oh,I'm sorry, i don't know golang
Author
Owner

@zeripath commented on GitHub (Sep 25, 2020):

of course here it would be easiest to just use shellquote but of course that doesn't work on Windows. (because it seems that singlequoting isn't handled properly.)

@zeripath commented on GitHub (Sep 25, 2020): of course here it would be easiest to just use shellquote but of course that doesn't work on Windows. (because it seems that singlequoting isn't handled properly.)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#5110