Skip to content

Commit

Permalink
fixup: validate imported form
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Oct 9, 2024
1 parent ec0c5f3 commit 000347f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/components/NewMessage/NewMessagePollEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import Close from 'vue-material-design-icons/Close.vue'
import IconFileUpload from 'vue-material-design-icons/FileUpload.vue'
import Plus from 'vue-material-design-icons/Plus.vue'

import { showError } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'

import NcActionButton from '@nextcloud/vue/dist/Components/NcActionButton.js'
Expand All @@ -106,6 +107,7 @@ import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import { POLL } from '../../constants.js'
import { usePollsStore } from '../../stores/polls.ts'
import type { createPollParams } from '../../types/index.ts'
import { validatePollForm } from '../../utils/validatePollForm.ts'

const props = defineProps<{
token: string,
Expand Down Expand Up @@ -204,14 +206,13 @@ function importPoll(event: Event) {
const reader = new FileReader()
reader.onload = (e: ProgressEvent) => {
try {
const jsonObject = JSON.parse((e.target as FileReader).result as string)
const parsedObject = validatePollForm((e.target as FileReader).result as string)
for (const key of Object.keys(pollForm)) {
if (jsonObject[key] !== undefined) {
pollForm[key] = jsonObject[key]
}
pollForm[key] = parsedObject[key]
}
} catch (error) {
console.error('Error while parsing JSON:', error)
showError(t('spreed', 'Error while importing poll'))
console.error('Error while importing poll:', error)
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/utils/validatePollForm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { createPollParams } from '../types/index.ts'

const pollFormExample: createPollParams = {
question: '',
options: ['', ''],
resultMode: 0,
maxVotes: 0,
}
const REQUIRED_KEYS: Array<keyof createPollParams> = Object.keys(pollFormExample) as Array<keyof createPollParams>

/**
* Parses a given JSON string and validates with required poll form object.
* Throws an error if parsed object doesn't match
* @param jsonString The string to evaluate
*/
function validatePollForm(jsonString: string): createPollParams {
const parsedObject = JSON.parse(jsonString)

if (REQUIRED_KEYS.some(key => parsedObject[key] === undefined)) {
throw new Error('Missing required key')
}

if (REQUIRED_KEYS.some(key => typeof pollFormExample[key] !== typeof parsedObject[key])
|| Object.values(parsedObject.options).some(opt => typeof opt !== 'string')) {
throw new Error('Invalid parsed value')
}

return parsedObject
}

export {
validatePollForm,
}

0 comments on commit 000347f

Please sign in to comment.