Skip to content

Commit cca189e

Browse files
Save files in local storage as umask (#21198)
Go creates temporary files as 600, but sometimes we want the group to be able to read them (for example, for another user to back up the storage) This PR applies the umask to the renamed tmp files in local storage. Co-authored-by: wxiaoguang <[email protected]>
1 parent 83680c9 commit cca189e

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

Diff for: modules/storage/local.go

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func (l *LocalStorage) Save(path string, r io.Reader, size int64) (int64, error)
102102
if err := util.Rename(tmp.Name(), p); err != nil {
103103
return 0, err
104104
}
105+
// Golang's tmp file (os.CreateTemp) always have 0o600 mode, so we need to change the file to follow the umask (as what Create/MkDir does)
106+
if err := util.ApplyUmask(p, os.ModePerm); err != nil {
107+
return 0, err
108+
}
105109

106110
tmpRemoved = true
107111

Diff for: modules/util/file_unix.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !windows
6+
7+
package util
8+
9+
import (
10+
"os"
11+
12+
"golang.org/x/sys/unix"
13+
)
14+
15+
var defaultUmask int
16+
17+
func init() {
18+
// at the moment, the umask could only be gotten by calling unix.Umask(newUmask)
19+
// use 0o077 as temp new umask to reduce the risks if this umask is used anywhere else before the correct umask is recovered
20+
tempUmask := 0o077
21+
defaultUmask = unix.Umask(tempUmask)
22+
unix.Umask(defaultUmask)
23+
}
24+
25+
func ApplyUmask(f string, newMode os.FileMode) error {
26+
mod := newMode & ^os.FileMode(defaultUmask)
27+
return os.Chmod(f, mod)
28+
}

Diff for: modules/util/file_unix_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !windows
6+
7+
package util
8+
9+
import (
10+
"os"
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestApplyUmask(t *testing.T) {
17+
f, err := os.CreateTemp(t.TempDir(), "test-filemode-")
18+
assert.NoError(t, err)
19+
20+
err = os.Chmod(f.Name(), 0o777)
21+
assert.NoError(t, err)
22+
st, err := os.Stat(f.Name())
23+
assert.NoError(t, err)
24+
assert.EqualValues(t, 0o777, st.Mode().Perm()&0o777)
25+
26+
oldDefaultUmask := defaultUmask
27+
defaultUmask = 0o037
28+
defer func() {
29+
defaultUmask = oldDefaultUmask
30+
}()
31+
err = ApplyUmask(f.Name(), os.ModePerm)
32+
assert.NoError(t, err)
33+
st, err = os.Stat(f.Name())
34+
assert.NoError(t, err)
35+
assert.EqualValues(t, 0o740, st.Mode().Perm()&0o777)
36+
}

Diff for: modules/util/file_windows.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build windows
6+
7+
package util
8+
9+
import (
10+
"os"
11+
)
12+
13+
func ApplyUmask(f string, newMode os.FileMode) error {
14+
// do nothing for Windows, because Windows doesn't use umask
15+
return nil
16+
}

0 commit comments

Comments
 (0)