sanitize.go 12 KB

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