useSSLVerificationWarningHandler.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { createMessage } from '@formkit/core'
  3. import { FormHandlerExecution } from '#shared/components/Form/types.ts'
  4. import type {
  5. ChangedField,
  6. FormHandler,
  7. FormHandlerFunction,
  8. ReactiveFormSchemData,
  9. } from '#shared/components/Form/types.ts'
  10. import { i18n } from '#shared/i18n.ts'
  11. import type { FormKitNode } from '@formkit/core'
  12. export const useSSLVerificationWarningHandler = (): FormHandler => {
  13. const addWarning = (formNode?: FormKitNode) => {
  14. formNode?.store.set(
  15. createMessage({
  16. blocking: false,
  17. key: 'sslVerificationWarning',
  18. type: 'warning',
  19. value: i18n.t(
  20. 'Turning off SSL verification is a security risk and should be used only temporary. Use this option at your own risk!',
  21. ),
  22. visible: true,
  23. }),
  24. )
  25. }
  26. const clearWarning = (formNode?: FormKitNode) => {
  27. formNode?.store.remove('sslVerificationWarning')
  28. }
  29. const initializeSSLVerifyDisabledNodeEvent = (
  30. sslFieldNode: FormKitNode,
  31. formNode: FormKitNode,
  32. ) => {
  33. sslFieldNode.on('prop:disabled', ({ origin }) => {
  34. const { props, value } = origin
  35. if (props.disabled) clearWarning(formNode)
  36. else if (value === false) addWarning(formNode)
  37. })
  38. }
  39. const initializeFormNodeEvents = (formNode: FormKitNode) => {
  40. formNode.on('child.deep', ({ payload }) => {
  41. const childNode = payload as FormKitNode
  42. if (childNode.name !== 'sslVerify') return
  43. initializeSSLVerifyDisabledNodeEvent(childNode, formNode)
  44. childNode.on('destroying', () => {
  45. clearWarning(formNode)
  46. })
  47. })
  48. }
  49. const initializeSSLVerifyNodeEvents = (
  50. execution: FormHandlerExecution,
  51. getNodeByName: (id: string) => FormKitNode | undefined,
  52. formNode?: FormKitNode,
  53. ) => {
  54. if (execution === FormHandlerExecution.InitialSettled && formNode) {
  55. const sslFieldNode = getNodeByName('sslVerify')
  56. if (sslFieldNode) {
  57. initializeSSLVerifyDisabledNodeEvent(sslFieldNode, formNode)
  58. sslFieldNode.on('destroying', () => {
  59. clearWarning(formNode)
  60. initializeFormNodeEvents(formNode)
  61. })
  62. } else {
  63. initializeFormNodeEvents(formNode)
  64. }
  65. }
  66. }
  67. const executeHandler = (
  68. execution: FormHandlerExecution,
  69. schemaData: ReactiveFormSchemData,
  70. changedField?: ChangedField,
  71. formNode?: FormKitNode,
  72. ) => {
  73. if (
  74. schemaData.fields.sslVerify === undefined ||
  75. schemaData.fields.sslVerify === null ||
  76. (execution === FormHandlerExecution.FieldChange &&
  77. (!changedField || changedField.name !== 'sslVerify')) ||
  78. (typeof formNode?.find === 'function' && !formNode?.find('sslVerify'))
  79. ) {
  80. return false
  81. }
  82. return true
  83. }
  84. const handleSSLVerificationWarning: FormHandlerFunction = (
  85. execution,
  86. reactivity,
  87. data,
  88. ) => {
  89. const { changedField, formNode, getNodeByName } = data
  90. const { schemaData } = reactivity
  91. initializeSSLVerifyNodeEvents(execution, getNodeByName, formNode)
  92. if (!executeHandler(execution, schemaData, changedField, formNode)) return
  93. if (
  94. !schemaData.fields.sslVerify.props.disabled &&
  95. ((execution === FormHandlerExecution.InitialSettled &&
  96. schemaData.fields.sslVerify.props.value === false) ||
  97. (execution === FormHandlerExecution.FieldChange &&
  98. changedField?.newValue === false))
  99. ) {
  100. addWarning(formNode)
  101. return
  102. }
  103. clearWarning(formNode)
  104. }
  105. return {
  106. execution: [
  107. FormHandlerExecution.InitialSettled,
  108. FormHandlerExecution.FieldChange,
  109. ],
  110. callback: handleSSLVerificationWarning,
  111. }
  112. }