summaryrefslogtreecommitdiff
path: root/plugins/static/plugin.go
blob: f6d9a0f2bbdffa7047c43eee79dfe4c7e8e1b164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package static

import (
	"net/http"
	"path"
	"strings"

	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

// PluginName contains default service name.
const PluginName = "static"

const RootPluginName = "http"

// Plugin serves static files. Potentially convert into middleware?
type Plugin struct {
	// server configuration (location, forbidden files and etc)
	cfg *Config

	log logger.Logger

	// root is initiated http directory
	root http.Dir

	// file extensions which are allowed to be served
	allowedExtensions map[string]struct{}

	// file extensions which are forbidden to be served
	forbiddenExtensions map[string]struct{}
}

// Init must return configure service and return true if service hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
	const op = errors.Op("static_plugin_init")
	if !cfg.Has(RootPluginName) {
		return errors.E(op, errors.Disabled)
	}

	err := cfg.UnmarshalKey(RootPluginName, &s.cfg)
	if err != nil {
		return errors.E(op, errors.Disabled, err)
	}

	if s.cfg.Static == nil {
		return errors.E(op, errors.Disabled)
	}

	s.log = log
	s.root = http.Dir(s.cfg.Static.Dir)

	err = s.cfg.Valid()
	if err != nil {
		return errors.E(op, err)
	}

	// create 2 hashmaps with the allowed and forbidden file extensions
	s.allowedExtensions = make(map[string]struct{}, len(s.cfg.Static.Allow))
	s.forbiddenExtensions = make(map[string]struct{}, len(s.cfg.Static.Forbid))

	// init forbidden
	for i := 0; i < len(s.cfg.Static.Forbid); i++ {
		// skip empty lines
		if s.cfg.Static.Forbid[i] == "" {
			continue
		}
		s.forbiddenExtensions[s.cfg.Static.Forbid[i]] = struct{}{}
	}

	// init allowed
	for i := 0; i < len(s.cfg.Static.Allow); i++ {
		// skip empty lines
		if s.cfg.Static.Allow[i] == "" {
			continue
		}
		s.allowedExtensions[s.cfg.Static.Allow[i]] = struct{}{}
	}

	// check if any forbidden items presented in the allowed
	// if presented, delete such items from allowed
	for k := range s.forbiddenExtensions {
		delete(s.allowedExtensions, k)
	}

	// at this point we have distinct allowed and forbidden hashmaps, also with alwaysServed
	return nil
}

func (s *Plugin) Name() string {
	return PluginName
}

// Middleware must return true if request/response pair is handled within the middleware.
func (s *Plugin) Middleware(next http.Handler) http.Handler {
	// Define the http.HandlerFunc
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// do not allow paths like ../../resource
		// only specified folder and resources in it
		// https://lgtm.com/rules/1510366186013/
		if strings.Contains(r.URL.Path, "..") {
			w.WriteHeader(http.StatusForbidden)
			return
		}

		if s.cfg.Static.Request != nil {
			for k, v := range s.cfg.Static.Request {
				r.Header.Add(k, v)
			}
		}

		if s.cfg.Static.Response != nil {
			for k, v := range s.cfg.Static.Response {
				w.Header().Set(k, v)
			}
		}

		// first - create a proper file path
		fPath := path.Clean(r.URL.Path)
		ext := strings.ToLower(path.Ext(fPath))

		// check that file extension in the forbidden list
		if _, ok := s.forbiddenExtensions[ext]; ok {
			s.log.Debug("file extension is forbidden", "ext", ext)
			next.ServeHTTP(w, r)
			return
		}

		// if we have some allowed extensions, we should check them
		// if not - all extensions allowed except forbidden
		if len(s.allowedExtensions) > 0 {
			// not found in allowed
			if _, ok := s.allowedExtensions[ext]; !ok {
				next.ServeHTTP(w, r)
				return
			}

			// file extension allowed
		}

		// ok, file is not in the forbidden list
		// Stat it and get file info
		f, err := s.root.Open(fPath)
		if err != nil {
			// else no such file, show error in logs only in debug mode
			s.log.Debug("no such file or directory", "error", err)
			// pass request to the worker
			next.ServeHTTP(w, r)
			return
		}

		// at high confidence there is should not be an error
		// because we stat-ed the path previously and know, that that is file (not a dir), and it exists
		finfo, err := f.Stat()
		if err != nil {
			// else no such file, show error in logs only in debug mode
			s.log.Debug("no such file or directory", "error", err)
			// pass request to the worker
			next.ServeHTTP(w, r)
			return
		}

		defer func() {
			err = f.Close()
			if err != nil {
				s.log.Error("file close error", "error", err)
			}
		}()

		// if provided path to the dir, do not serve the dir, but pass the request to the worker
		if finfo.IsDir() {
			s.log.Debug("possible path to dir provided")
			// pass request to the worker
			next.ServeHTTP(w, r)
			return
		}

		// set etag
		if s.cfg.Static.CalculateEtag {
			SetEtag(s.cfg.Static.Weak, f, finfo.Name(), w)
		}

		// we passed all checks - serve the file
		http.ServeContent(w, r, finfo.Name(), finfo.ModTime(), f)
	})
}