sanitize.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package config
  2. import (
  3. "fmt"
  4. "strings"
  5. "text/template"
  6. "github.com/OliveTin/OliveTin/internal/env"
  7. "github.com/google/uuid"
  8. log "github.com/sirupsen/logrus"
  9. )
  10. // Sanitize will look for common configuration issues, and fix them. For example,
  11. // populating undefined fields - name -> title, etc.
  12. func (cfg *Config) Sanitize() {
  13. cfg.sanitizeLogLevel()
  14. cfg.sanitizeAuthRequireGuestsToLogin()
  15. cfg.sanitizeLogHistoryPageSize()
  16. cfg.sanitizeLocalUsers()
  17. cfg.sanitizeSecurityHeaders()
  18. cfg.sanitizeOnClickDefaults()
  19. // log.Infof("cfg %p", cfg)
  20. for idx := range cfg.Actions {
  21. cfg.Actions[idx].sanitize(cfg)
  22. }
  23. cfg.sanitizeDashboardsForInlineActions()
  24. cfg.sanitizeActionGroups()
  25. cfg.sanitizeActionGroupReferences()
  26. if err := cfg.validateReservedActionArgumentNames(); err != nil {
  27. log.Fatalf("%v", err)
  28. }
  29. }
  30. func (cfg *Config) validateReservedActionArgumentNames() error {
  31. for _, action := range cfg.Actions {
  32. if err := action.validateReservedArgumentNames(); err != nil {
  33. return err
  34. }
  35. }
  36. return nil
  37. }
  38. func (action *Action) validateReservedArgumentNames() error {
  39. if action == nil {
  40. return nil
  41. }
  42. for _, arg := range action.Arguments {
  43. if strings.HasPrefix(arg.Name, ReservedArgumentNamePrefix) {
  44. return fmt.Errorf("action %q argument %q uses reserved prefix %q", action.Title, arg.Name, ReservedArgumentNamePrefix)
  45. }
  46. }
  47. return nil
  48. }
  49. func (cfg *Config) sanitizeDashboardsForInlineActions() {
  50. for _, dashboard := range cfg.Dashboards {
  51. cfg.sanitizeDashboardComponentForInlineActions(dashboard)
  52. }
  53. }
  54. func (cfg *Config) sanitizeDashboardComponentForInlineActions(component *DashboardComponent) {
  55. visited := make(map[*DashboardComponent]bool)
  56. cfg.sanitizeDashboardComponentForInlineActionsHelper(component, visited)
  57. }
  58. func (cfg *Config) sanitizeDashboardComponentForInlineActionsHelper(component *DashboardComponent, visited map[*DashboardComponent]bool) {
  59. if component == nil {
  60. return
  61. }
  62. if visited[component] {
  63. return
  64. }
  65. visited[component] = true
  66. cfg.sanitizeInlineAction(component)
  67. cfg.sanitizeChildDashboardComponents(component, visited)
  68. }
  69. func (cfg *Config) sanitizeInlineAction(component *DashboardComponent) {
  70. if component.InlineAction == nil {
  71. return
  72. }
  73. sanitizeInlineActionTitles(component)
  74. if component.Entity != "" && component.InlineAction.Entity == "" {
  75. component.InlineAction.Entity = component.Entity
  76. }
  77. component.InlineAction.sanitize(cfg)
  78. cfg.addInlineActionIfNotExists(component.InlineAction)
  79. }
  80. func (cfg *Config) addInlineActionIfNotExists(action *Action) {
  81. if cfg.inlineActionExists(action) {
  82. return
  83. }
  84. cfg.Actions = append(cfg.Actions, action)
  85. }
  86. func sanitizeInlineActionTitles(component *DashboardComponent) {
  87. if component.InlineAction.Title == "" {
  88. component.InlineAction.Title = component.Title
  89. }
  90. if component.Title == "" {
  91. component.Title = component.InlineAction.Title
  92. }
  93. }
  94. func (cfg *Config) inlineActionExists(action *Action) bool {
  95. if cfg.inlineActionPointerExists(action) {
  96. return true
  97. }
  98. if cfg.inlineActionIDExists(action) {
  99. return true
  100. }
  101. return false
  102. }
  103. func (cfg *Config) inlineActionPointerExists(action *Action) bool {
  104. for _, existingAction := range cfg.Actions {
  105. if existingAction == action {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. func (cfg *Config) inlineActionIDExists(action *Action) bool {
  112. if action.ID == "" {
  113. return false
  114. }
  115. for _, existingAction := range cfg.Actions {
  116. if existingAction.ID == action.ID {
  117. return true
  118. }
  119. }
  120. return false
  121. }
  122. func (cfg *Config) sanitizeChildDashboardComponents(component *DashboardComponent, visited map[*DashboardComponent]bool) {
  123. for _, child := range component.Contents {
  124. if child.Entity == "" {
  125. child.Entity = component.Entity
  126. }
  127. cfg.sanitizeDashboardComponentForInlineActionsHelper(child, visited)
  128. }
  129. }
  130. func (cfg *Config) sanitizeLogLevel() {
  131. if logLevel, err := log.ParseLevel(cfg.LogLevel); err == nil {
  132. log.Info("Setting log level to ", logLevel)
  133. log.SetLevel(logLevel)
  134. }
  135. }
  136. func (action *Action) sanitize(cfg *Config) {
  137. if action.Timeout < 3 {
  138. action.Timeout = 3
  139. }
  140. action.ID = getActionID(action)
  141. action.Icon = lookupHTMLIcon(action.Icon, cfg.DefaultIconForActions)
  142. migrateActionOnClick(action)
  143. action.OnClick = sanitizeOnClick(action.OnClick, cfg)
  144. action.PopupOnStart = action.OnClick
  145. if action.MaxConcurrent < 1 {
  146. action.MaxConcurrent = 1
  147. }
  148. action.Groups = dedupeStrings(action.Groups)
  149. for idx := range action.Arguments {
  150. action.Arguments[idx].sanitize()
  151. }
  152. }
  153. func dedupeStrings(values []string) []string {
  154. seen := make(map[string]struct{}, len(values))
  155. out := make([]string, 0, len(values))
  156. for _, value := range values {
  157. out = appendUniqueString(out, seen, value)
  158. }
  159. return out
  160. }
  161. func appendUniqueString(out []string, seen map[string]struct{}, value string) []string {
  162. if value == "" {
  163. return out
  164. }
  165. if _, found := seen[value]; found {
  166. return out
  167. }
  168. seen[value] = struct{}{}
  169. return append(out, value)
  170. }
  171. const defaultActionGroupQueueSize = 5
  172. func (cfg *Config) sanitizeActionGroups() {
  173. for _, group := range cfg.ActionGroups {
  174. if group == nil {
  175. continue
  176. }
  177. if group.QueueSize <= 0 {
  178. group.QueueSize = defaultActionGroupQueueSize
  179. }
  180. group.Icon = lookupHTMLIcon(group.Icon, cfg.DefaultIconForActions)
  181. }
  182. }
  183. func (cfg *Config) sanitizeActionGroupReferences() {
  184. for _, action := range cfg.Actions {
  185. for _, groupName := range action.Groups {
  186. cfg.warnInvalidActionGroupReference(action, groupName)
  187. }
  188. }
  189. }
  190. func (cfg *Config) warnInvalidActionGroupReference(action *Action, groupName string) {
  191. group, found := cfg.ActionGroups[groupName]
  192. if !found {
  193. log.WithFields(log.Fields{
  194. "actionTitle": action.Title,
  195. "groupName": groupName,
  196. }).Warn("Action references unknown action group")
  197. return
  198. }
  199. if group == nil || group.MaxConcurrent < 1 {
  200. log.WithFields(log.Fields{
  201. "actionTitle": action.Title,
  202. "groupName": groupName,
  203. }).Warn("Action references action group that will not be enforced at runtime")
  204. }
  205. }
  206. func (cfg *Config) sanitizeAuthRequireGuestsToLogin() {
  207. if cfg.AuthRequireGuestsToLogin {
  208. log.Infof("AuthRequireGuestsToLogin is enabled. All defaultPermissions will be set to false")
  209. cfg.DefaultPermissions.View = false
  210. cfg.DefaultPermissions.Exec = false
  211. cfg.DefaultPermissions.Logs = false
  212. cfg.DefaultPermissions.Kill = false
  213. }
  214. }
  215. func (cfg *Config) sanitizeLogHistoryPageSize() {
  216. if cfg.LogHistoryPageSize < 10 {
  217. log.Warnf("LogsHistoryLimit is too low, setting it to 10")
  218. cfg.LogHistoryPageSize = 10
  219. } else if cfg.LogHistoryPageSize > 100 {
  220. log.Warnf("LogsHistoryLimit is high, you can do this, but expect browser lag.")
  221. }
  222. }
  223. func (cfg *Config) sanitizeLocalUsers() {
  224. for _, user := range cfg.AuthLocalUsers.Users {
  225. expandLocalUserEnvTemplates(user)
  226. }
  227. if err := validateUniqueLocalUserAPIKeys(cfg.AuthLocalUsers.Users); err != nil {
  228. log.Fatalf("%v", err)
  229. }
  230. }
  231. func expandLocalUserEnvTemplates(user *LocalUser) {
  232. if user == nil {
  233. return
  234. }
  235. if user.Password != "" {
  236. user.Password = expandEnvTemplate(user.Password)
  237. }
  238. if user.ApiKey != "" {
  239. user.ApiKey = expandEnvTemplate(user.ApiKey)
  240. }
  241. }
  242. // validateUniqueLocalUserAPIKeys returns an error when two local users share the same non-empty apiKey.
  243. func validateUniqueLocalUserAPIKeys(users []*LocalUser) error {
  244. seen := make(map[string]string)
  245. for _, user := range users {
  246. if err := recordUniqueLocalUserAPIKey(seen, user); err != nil {
  247. return err
  248. }
  249. }
  250. return nil
  251. }
  252. func recordUniqueLocalUserAPIKey(seen map[string]string, user *LocalUser) error {
  253. if user == nil || user.ApiKey == "" {
  254. return nil
  255. }
  256. if prior, ok := seen[user.ApiKey]; ok {
  257. return fmt.Errorf("duplicate authLocalUsers apiKey for users %q and %q", prior, user.Username)
  258. }
  259. seen[user.ApiKey] = user.Username
  260. return nil
  261. }
  262. func (cfg *Config) sanitizeSecurityHeaders() {
  263. cfg.sanitizeSecurityHeadersCSP()
  264. cfg.sanitizeSecurityHeadersXFrameOptions()
  265. }
  266. func (cfg *Config) sanitizeSecurityHeadersCSP() {
  267. if !cfg.Security.HeaderContentSecurityPolicy || cfg.Security.ContentSecurityPolicy != "" {
  268. return
  269. }
  270. cfg.Security.ContentSecurityPolicy = ContentSecurityPolicyDefault
  271. }
  272. func (cfg *Config) sanitizeSecurityHeadersXFrameOptions() {
  273. if !cfg.Security.HeaderXFrameOptions || cfg.Security.XFrameOptions != "" {
  274. return
  275. }
  276. cfg.Security.XFrameOptions = "DENY"
  277. }
  278. // expandEnvTemplate expands {{ .Env.VAR }} in config strings using the process environment.
  279. func expandEnvTemplate(source string) string {
  280. t, err := template.New("envTemplate").Option("missingkey=error").Parse(source)
  281. if err != nil {
  282. log.WithFields(log.Fields{"error": err}).Debug("Env template parse failed, using literal")
  283. return source
  284. }
  285. var b strings.Builder
  286. if err := t.Execute(&b, map[string]interface{}{"Env": env.BuildEnvMap()}); err != nil {
  287. log.WithFields(log.Fields{"error": err}).Debug("Env template execute failed, using literal")
  288. return source
  289. }
  290. return b.String()
  291. }
  292. func getActionID(action *Action) string {
  293. if action.ID == "" {
  294. return uuid.NewString()
  295. }
  296. if strings.Contains(action.ID, "{{") {
  297. log.Fatalf("Action IDs cannot contain variables")
  298. }
  299. return action.ID
  300. }
  301. //gocyclo:ignore
  302. func sanitizeOnClick(raw string, cfg *Config) string {
  303. switch raw {
  304. case "execution-dialog":
  305. return raw
  306. case "execution-dialog-output-html":
  307. return raw
  308. case "execution-dialog-stdout-only":
  309. return raw
  310. case "execution-button":
  311. return raw
  312. case "history":
  313. return raw
  314. default:
  315. return cfg.DefaultOnClick
  316. }
  317. }
  318. func migrateActionOnClick(action *Action) {
  319. if action.OnClick == "" && action.PopupOnStart != "" {
  320. action.OnClick = action.PopupOnStart
  321. }
  322. }
  323. func shouldMigrateDefaultOnClickFromPopup(onClick, popupOnStart string) bool {
  324. if popupOnStart == "" {
  325. return false
  326. }
  327. if onClick == "" {
  328. return true
  329. }
  330. return onClick == "nothing" && popupOnStart != "nothing"
  331. }
  332. func (cfg *Config) migrateDefaultOnClickFromLegacyPopup() {
  333. if !shouldMigrateDefaultOnClickFromPopup(cfg.DefaultOnClick, cfg.DefaultPopupOnStart) {
  334. return
  335. }
  336. cfg.DefaultOnClick = cfg.DefaultPopupOnStart
  337. }
  338. func (cfg *Config) sanitizeOnClickDefaults() {
  339. cfg.migrateDefaultOnClickFromLegacyPopup()
  340. if cfg.DefaultOnClick == "" {
  341. cfg.DefaultOnClick = "nothing"
  342. }
  343. cfg.DefaultPopupOnStart = cfg.DefaultOnClick
  344. }
  345. func (arg *ActionArgument) sanitize() {
  346. if arg.Title == "" {
  347. arg.Title = arg.Name
  348. }
  349. for idx, choice := range arg.Choices {
  350. if choice.Title == "" {
  351. arg.Choices[idx].Title = choice.Value
  352. }
  353. }
  354. arg.sanitizeNoType()
  355. // Default value validation runs in executor at config load (validateArgumentDefaults).
  356. }
  357. func (arg *ActionArgument) sanitizeNoType() {
  358. if len(arg.Choices) == 0 && arg.Type == "" {
  359. log.WithFields(log.Fields{
  360. "arg": arg.Name,
  361. }).Warn("Argument type isn't set, will default to 'ascii' but this may not be safe. You should set a type specifically.")
  362. arg.Type = "ascii"
  363. }
  364. }