Line data Source code
1 : import { useI18n } from 'vue-i18n'
2 : import { useRoute, useRouter } from 'vue-router'
3 :
4 : import { useToast } from '@/components/ui/toast/toastStore'
5 : import { clearPreservedQuery } from '@/platform/navigation/preservedQueryManager'
6 : import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
7 : import { useTelemetry } from '@comfyorg/telemetry'
8 : // oxlint-disable-next-line comfy/no-restricted-paths
9 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
10 :
11 : import { useTemplateWorkflows } from './useTemplateWorkflows'
12 :
13 : /**
14 : * Composable for loading templates from URL query parameters
15 : *
16 : * Supports URLs like:
17 : * - /?template=flux_simple (loads with default source)
18 : * - /?template=flux_simple&source=custom (loads from custom source)
19 : * - /?template=flux_simple&mode=linear (loads template in linear mode)
20 : *
21 : * Input validation:
22 : * - Template, source, and mode parameters must match: ^[a-zA-Z0-9_-]+$
23 : * - Invalid formats are rejected with console warnings
24 : */
25 2355 : export function useTemplateUrlLoader() {
26 2355 : const route = useRoute()
27 2355 : const router = useRouter()
28 2355 : const { t } = useI18n()
29 2355 : const toast = useToast()
30 2355 : const templateWorkflows = useTemplateWorkflows()
31 2355 : const canvasStore = useCanvasStore()
32 2355 : const TEMPLATE_NAMESPACE = PRESERVED_QUERY_NAMESPACES.TEMPLATE
33 2355 : const SUPPORTED_MODES = ['linear'] as const
34 : type SupportedMode = (typeof SUPPORTED_MODES)[number]
35 :
36 : /**
37 : * Validates parameter format to prevent path traversal and injection attacks
38 : * Allows: letters, numbers, underscores, hyphens, and dots (for version numbers)
39 : * Blocks: path separators (/, \), special chars that could enable injection
40 : */
41 2355 : const isValidParameter = (param: string): boolean => {
42 4 : return /^[a-zA-Z0-9_.-]+$/.test(param)
43 : }
44 :
45 : /**
46 : * Type guard to check if a value is a supported mode
47 : */
48 2355 : const isSupportedMode = (mode: string): mode is SupportedMode => {
49 0 : return SUPPORTED_MODES.includes(mode as SupportedMode)
50 : }
51 :
52 : /**
53 : * Removes template, source, and mode parameters from URL
54 : */
55 2355 : const cleanupUrlParams = () => {
56 2 : const newQuery = { ...route.query }
57 2 : delete newQuery.template
58 2 : delete newQuery.source
59 2 : delete newQuery.mode
60 2 : void router.replace({ query: newQuery })
61 : }
62 :
63 : /**
64 : * Loads template from URL query parameters if present
65 : * Handles errors internally and shows appropriate user feedback
66 : * @returns the id of the template that loaded, if one did
67 : */
68 2355 : const loadTemplateFromUrl = async (): Promise<string | undefined> => {
69 2 : const templateParam = route.query.template
70 :
71 2 : if (!templateParam || typeof templateParam !== 'string') {
72 0 : return
73 : }
74 :
75 2 : if (!isValidParameter(templateParam)) {
76 0 : console.warn(
77 : `[useTemplateUrlLoader] Invalid template parameter format: ${templateParam}`
78 : )
79 0 : return
80 : }
81 :
82 2 : const sourceParam = (route.query.source as string | undefined) || 'default'
83 :
84 2 : if (!isValidParameter(sourceParam)) {
85 0 : console.warn(
86 : `[useTemplateUrlLoader] Invalid source parameter format: ${sourceParam}`
87 : )
88 0 : return
89 : }
90 :
91 2 : const modeParam = route.query.mode as string | undefined
92 :
93 2 : if (
94 : modeParam &&
95 : (typeof modeParam !== 'string' || !isValidParameter(modeParam))
96 : ) {
97 0 : console.warn(
98 : `[useTemplateUrlLoader] Invalid mode parameter format: ${modeParam}`
99 : )
100 0 : return
101 : }
102 :
103 2 : if (modeParam && !isSupportedMode(modeParam)) {
104 0 : console.warn(
105 : `[useTemplateUrlLoader] Unsupported mode parameter: ${modeParam}. Supported modes: ${SUPPORTED_MODES.join(', ')}`
106 : )
107 : }
108 :
109 2 : try {
110 2 : await templateWorkflows.loadTemplates()
111 :
112 : const result = await templateWorkflows.loadWorkflowTemplate(
113 : templateParam,
114 : sourceParam
115 : )
116 :
117 2 : if (result !== 'loaded') return
118 :
119 1 : if (modeParam === 'linear') {
120 : // Set linear mode after successful template load
121 0 : useTelemetry()?.trackEnterLinear({ source: 'template_url' })
122 0 : canvasStore.linearMode = true
123 : }
124 2 : return sourceParam === 'default' ? templateParam : undefined
125 : } catch (error) {
126 0 : console.error(
127 : '[useTemplateUrlLoader] Failed to load template from URL:',
128 : error
129 : )
130 0 : toast.error(t('g.error'), {
131 : description: t('templateWorkflows.error.loading')
132 : })
133 : } finally {
134 2 : cleanupUrlParams()
135 2 : clearPreservedQuery(TEMPLATE_NAMESPACE)
136 : }
137 : }
138 :
139 2355 : return {
140 : loadTemplateFromUrl
141 : }
142 : }
|