all: Add untrusted folders behind feature flag (ref #62) (#7055)

This commit is contained in:
Simon Frei
2020-11-09 15:33:32 +01:00
committed by GitHub
parent 4db5ea5893
commit 31559e908b
59 changed files with 6952 additions and 1111 deletions
+7 -1
View File
@@ -65,6 +65,7 @@ const (
EventSubBufferSize = 1000
defaultEventTimeout = time.Minute
httpsCertLifetimeDays = 820
featureFlagUntrusted = "untrusted"
)
type service struct {
@@ -110,7 +111,7 @@ func New(id protocol.DeviceID, cfg config.Wrapper, assetDir, tlsDefaultCommonNam
s := &service{
id: id,
cfg: cfg,
statics: newStaticsServer(cfg.GUI().Theme, assetDir),
statics: newStaticsServer(cfg.GUI().Theme, assetDir, cfg.Options().FeatureFlag(featureFlagUntrusted)),
model: m,
eventSubs: map[events.EventType]events.BufferedSubscription{
DefaultEventMask: defaultSub,
@@ -449,7 +450,12 @@ func (s *service) CommitConfiguration(from, to config.Configuration) bool {
// No action required when this changes, so mask the fact that it changed at all.
from.GUI.Debugging = to.GUI.Debugging
if untrusted := to.Options.FeatureFlag(featureFlagUntrusted); untrusted != from.Options.FeatureFlag(featureFlagUntrusted) {
s.statics.setUntrusted(untrusted)
}
if to.GUI == from.GUI {
// No GUI changes, we're done here.
return true
}
+70 -32
View File
@@ -30,15 +30,17 @@ type staticsServer struct {
mut sync.RWMutex
theme string
lastThemeChange time.Time
untrusted bool
}
func newStaticsServer(theme, assetDir string) *staticsServer {
func newStaticsServer(theme, assetDir string, untrusted bool) *staticsServer {
s := &staticsServer{
assetDir: assetDir,
assets: auto.Assets(),
mut: sync.NewRWMutex(),
theme: theme,
lastThemeChange: time.Now().UTC(),
untrusted: untrusted,
}
seen := make(map[string]struct{})
@@ -60,6 +62,10 @@ func newStaticsServer(theme, assetDir string) *staticsServer {
}
}
if untrusted {
l.Infoln(`Feature flag "untrusted":`, untrusted)
}
return s
}
@@ -88,6 +94,7 @@ func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
s.mut.RLock()
theme := s.theme
modificationTime := s.lastThemeChange
untrusted := s.untrusted
s.mut.RUnlock()
// If path starts with special prefix, get theme and file from path
@@ -105,44 +112,68 @@ func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
}
// Check for an override for the current theme.
if s.assetDir != "" {
p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
if _, err := os.Stat(p); err == nil {
mtype := assets.MimeTypeForFile(file)
if len(mtype) != 0 {
w.Header().Set("Content-Type", mtype)
}
http.ServeFile(w, r, p)
return
}
if untrusted && s.serveFromAssetDir(file, theme+"/untrusted", w, r) {
l.Debugln("serving", file, "from override untrusted")
return
}
if s.serveFromAssetDir(file, theme, w, r) {
return
}
// Check for a compiled in asset for the current theme.
as, ok := s.assets[theme+"/"+file]
if !ok {
// Check for an overridden default asset.
if s.assetDir != "" {
p := filepath.Join(s.assetDir, config.DefaultTheme, filepath.FromSlash(file))
if _, err := os.Stat(p); err == nil {
mtype := assets.MimeTypeForFile(file)
if len(mtype) != 0 {
w.Header().Set("Content-Type", mtype)
}
http.ServeFile(w, r, p)
return
}
}
// Check for a compiled in default asset.
as, ok = s.assets[config.DefaultTheme+"/"+file]
if !ok {
http.NotFound(w, r)
return
}
if untrusted && s.serveFromAssets(file, theme+"/untrusted", modificationTime, w, r) {
l.Debugln("serving", file, "from compiled untrusted")
return
}
if s.serveFromAssets(file, theme, modificationTime, w, r) {
return
}
// Check for an overridden default asset.
if untrusted && s.serveFromAssetDir(file, config.DefaultTheme+"/untrusted", w, r) {
l.Debugln("serving", file, "from override untrusted")
return
}
if s.serveFromAssetDir(file, config.DefaultTheme, w, r) {
return
}
// Check for a compiled in default asset.
if untrusted && s.serveFromAssets(file, config.DefaultTheme+"/untrusted", modificationTime, w, r) {
l.Debugln("serving", file, "from compiled untrusted")
return
}
if s.serveFromAssets(file, config.DefaultTheme, modificationTime, w, r) {
return
}
http.NotFound(w, r)
}
func (s *staticsServer) serveFromAssetDir(file, theme string, w http.ResponseWriter, r *http.Request) bool {
if s.assetDir == "" {
return false
}
p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
if _, err := os.Stat(p); err != nil {
return false
}
mtype := assets.MimeTypeForFile(file)
if len(mtype) != 0 {
w.Header().Set("Content-Type", mtype)
}
http.ServeFile(w, r, p)
return true
}
func (s *staticsServer) serveFromAssets(file, theme string, modificationTime time.Time, w http.ResponseWriter, r *http.Request) bool {
as, ok := s.assets[theme+"/"+file]
if !ok {
return false
}
as.Modified = modificationTime
assets.Serve(w, r, as)
return true
}
func (s *staticsServer) serveThemes(w http.ResponseWriter, r *http.Request) {
@@ -158,6 +189,13 @@ func (s *staticsServer) setTheme(theme string) {
s.mut.Unlock()
}
func (s *staticsServer) setUntrusted(enabled bool) {
s.mut.Lock()
l.Infoln(`Feature flag "untrusted":`, enabled)
s.untrusted = enabled
s.mut.Unlock()
}
func (s *staticsServer) String() string {
return fmt.Sprintf("staticsServer@%p", s)
}
+4
View File
@@ -114,6 +114,10 @@ func (c *mockedConfig) RemoveFolder(id string) (config.Waiter, error) {
return noopWaiter{}, nil
}
func (c *mockedConfig) FolderPasswords(device protocol.DeviceID) map[string]string {
return nil
}
func (c *mockedConfig) Device(id protocol.DeviceID) (config.DeviceConfiguration, bool) {
return config.DeviceConfiguration{}, false
}
+1 -1
View File
@@ -135,7 +135,7 @@ func (m *mockedModel) IndexUpdate(deviceID protocol.DeviceID, folder string, fil
return nil
}
func (m *mockedModel) Request(deviceID protocol.DeviceID, folder, name string, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (protocol.RequestResponse, error) {
func (m *mockedModel) Request(deviceID protocol.DeviceID, folder, name string, blockNo, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (protocol.RequestResponse, error) {
return nil, nil
}