Skip to content

Commit

Permalink
Implement security ban theme (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: DblK <[email protected]>
  • Loading branch information
DblK and DblK authored Dec 19, 2021
1 parent d2ca7b0 commit 7638137
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Here is the list of all main features so far:
- [X] Display a webpage for forbidden devices
- [X] Auto-refresh configuration on file change
- [X] Add the possibility to whitelist or blacklist a switch
- [X] Add the possibility to ban theme

# Dev or build from source

Expand Down
5 changes: 5 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ sources:

# All security information will be stored here
security:
# List of theme to be banned with security
# Be aware that this should be string (do not forget quotes)
# You can find the theme of a switch in the log upon access
bannedTheme:
- "0000000000000000000000000000000000000000000000000000000000000000"
# List of switch uid to whitelist
# If enabled then only switch in this area will be listed
# You can find the uid of a switch in the log upon access
Expand Down
15 changes: 13 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type debug struct {
}

type security struct {
Whitelist []string `mapstructure:"whitelist"`
Backlist []string `mapstructure:"backlist"`
Whitelist []string `mapstructure:"whitelist"`
Backlist []string `mapstructure:"backlist"`
BannedTheme []string `mapstructure:"bannedTheme"`
}

// File holds all config information
Expand Down Expand Up @@ -231,3 +232,13 @@ func (cfg *File) isInWhiteList(uid string) bool {
})
return idxWhiteList != -1
}

// IsBannedTheme tells if the theme is banned or not
func (cfg *File) IsBannedTheme(theme string) bool {
fmt.Println(theme)
fmt.Println(cfg.Security.BannedTheme)
idxBannedTheme := utils.Search(len(cfg.Security.BannedTheme), func(index int) bool {
return cfg.Security.BannedTheme[index] == theme
})
return idxBannedTheme != -1
}
20 changes: 20 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,24 @@ var _ = Describe("Config", func() {
})
})
})
Context("Security for theme", func() {
var myConfig = config.File{}
Describe("IsBannedTheme", func() {
It("should not be banned if empty config", func() {
Expect(myConfig.IsBannedTheme("myTheme")).To(BeFalse())
})
It("should not be banned if no corresponding config", func() {
var bannedThemes = make([]string, 0)
bannedThemes = append(bannedThemes, "banned")
myConfig.Security.BannedTheme = bannedThemes
Expect(myConfig.IsBannedTheme("myTheme")).To(BeFalse())
})
It("should not be banned if no corresponding config", func() {
var bannedThemes = make([]string, 0)
bannedThemes = append(bannedThemes, "myTheme")
myConfig.Security.BannedTheme = bannedThemes
Expect(myConfig.IsBannedTheme("myTheme")).To(BeTrue())
})
})
})
})
14 changes: 14 additions & 0 deletions mock_repository/mock_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions repository/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config interface {

IsBlacklisted(string) bool
IsWhitelisted(string) bool
IsBannedTheme(string) bool
}

// ShopTemplate contains all variables used for shop template
Expand Down
8 changes: 8 additions & 0 deletions security.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func tinfoilMiddleware(next http.Handler) http.Handler {
return
}

// Check for banned theme
var theme = strings.Join(headers["Theme"], "")
if config.GetConfig().IsBannedTheme(theme) {
log.Println("[Security] Banned theme detected...", uid, theme)
_ = shopTemplate.Execute(w, config.GetConfig().ShopTemplateData())
return
}

// No User-Agent for tinfoil app
if headers["User-Agent"] != nil {
log.Println("[Security] User-Agent detected...")
Expand Down

0 comments on commit 7638137

Please sign in to comment.