ALR

Форк
1
/
restricted.go 
80 строк · 2.2 Кб
1
/*
2
 * ALR - Any Linux Repository
3
 * Copyright (C) 2024 Евгений Храмов
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18

19
package handlers
20

21
import (
22
	"context"
23
	"io"
24
	"io/fs"
25
	"path/filepath"
26
	"strings"
27
	"time"
28

29
	"golang.org/x/exp/slices"
30
	"mvdan.cc/sh/v3/interp"
31
)
32

33
func RestrictedReadDir(allowedPrefixes ...string) interp.ReadDirHandlerFunc {
34
	return func(ctx context.Context, s string) ([]fs.FileInfo, error) {
35
		path := filepath.Clean(s)
36
		for _, allowedPrefix := range allowedPrefixes {
37
			if strings.HasPrefix(path, allowedPrefix) {
38
				return interp.DefaultReadDirHandler()(ctx, s)
39
			}
40
		}
41

42
		return nil, fs.ErrNotExist
43
	}
44
}
45

46
func RestrictedStat(allowedPrefixes ...string) interp.StatHandlerFunc {
47
	return func(ctx context.Context, s string, b bool) (fs.FileInfo, error) {
48
		path := filepath.Clean(s)
49
		for _, allowedPrefix := range allowedPrefixes {
50
			if strings.HasPrefix(path, allowedPrefix) {
51
				return interp.DefaultStatHandler()(ctx, s, b)
52
			}
53
		}
54

55
		return nil, fs.ErrNotExist
56
	}
57
}
58

59
func RestrictedOpen(allowedPrefixes ...string) interp.OpenHandlerFunc {
60
	return func(ctx context.Context, s string, i int, fm fs.FileMode) (io.ReadWriteCloser, error) {
61
		path := filepath.Clean(s)
62
		for _, allowedPrefix := range allowedPrefixes {
63
			if strings.HasPrefix(path, allowedPrefix) {
64
				return interp.DefaultOpenHandler()(ctx, s, i, fm)
65
			}
66
		}
67

68
		return NopRWC{}, nil
69
	}
70
}
71

72
func RestrictedExec(allowedCmds ...string) interp.ExecHandlerFunc {
73
	return func(ctx context.Context, args []string) error {
74
		if slices.Contains(allowedCmds, args[0]) {
75
			return interp.DefaultExecHandler(2*time.Second)(ctx, args)
76
		}
77

78
		return nil
79
	}
80
}
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.