Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions ui/src/views/application-workflow/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
>
</div>
<el-text type="info" class="ml-16 color-secondary" v-else-if="saveTime"
>{{ $t('workflow.info.saveTime')
}}{{ datetimeFormat(saveTime) }}</el-text
>{{ $t('workflow.info.saveTime') }}{{ datetimeFormat(saveTime) }}</el-text
>
</div>
<div v-if="showHistory && disablePublic">
Expand Down Expand Up @@ -207,7 +206,10 @@ const urlParams = computed(() =>
mapToUrlParams(apiInputParams.value) ? '?' + mapToUrlParams(apiInputParams.value) : '',
)
const shareUrl = computed(
() => `${window.location.origin}/chat/` + detail.value?.access_token + urlParams.value,
() =>
`${window.location.origin}${window.MaxKB.chatPrefix}/` +
detail.value?.access_token +
urlParams.value,
)

function back() {
Expand Down Expand Up @@ -368,9 +370,7 @@ const publish = () => {
const node = res.node
const err_message = res.errMessage
if (typeof err_message == 'string') {
MsgError(
res.node.properties?.stepName + ` ${t('workflow.node')},` + err_message,
)
MsgError(res.node.properties?.stepName + ` ${t('workflow.node')},` + err_message)
} else {
const keys = Object.keys(err_message)
MsgError(
Expand Down Expand Up @@ -406,9 +406,7 @@ const clickShowDebug = () => {
const node = res.node
const err_message = res.errMessage
if (typeof err_message == 'string') {
MsgError(
res.node.properties?.stepName + ` ${t('workflow.node')},` + err_message,
)
MsgError(res.node.properties?.stepName + ` ${t('workflow.node')},` + err_message)
} else {
const keys = Object.keys(err_message)
MsgError(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code contains several issues and areas for improvement:

  1. Variable Reassignment: The variable urlParams is redefined within a conditional block inside the computed definition, which can lead to unexpected behavior.

  2. URL Concatenation: There are multiple instances where URLs are concatenated using string operators directly, without ensuring proper escaping or handling of special characters. This could potentially introduce security vulnerabilities if not properly managed.

  3. Empty Strings in Conditional Statements: In some places, conditions involve checking empty strings using !value, which might not be intuitive. Consider using Boolean(value) instead for clearer logic.

  4. Error Message Handling: Error messages should be displayed correctly, especially when dealing with complex error objects like those returned from API calls.

  5. Unused Variables: Some variables (detail.value) appear unused and could be removed to simplify the code.

Here's a revised version of the code addressing these points:

const detail = ref(null);
const saveTime = ref(new Date());

// Ensure URL parameters are safe before concatenating them
function safeStringify(input) {
  return JSON.stringify(input).replace(/"/g, '\\"').replace(/\n/gi, "\\n");
}

const urlParams = computed(() => {
  const apiUrlParams = mapToUrlParams(apiInputParams.value) || {}
  const queryPart = Object.entries(apiUrlParams)
    .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
    .join('&');
  return queryPart.length > 0 ? `?${queryPart}` : '';
});

const shareUrl = computed(() => {
  let baseURL;
  if (window.MaxKB) {
    baseURL = window.MaxKB.chatPrefix;
  } else {
    // Provide default base URL if MaxKB is undefined or null
    baseURL = '/chat/';
  }
  return baseURL + detail.value?.access_token + urlParams.value;
});

function back() {
  history.back();
}

async function publish() {
  try {
    const res = await fetch("https://api.maxkb.com/publish-workflow", {
      method: "POST",
      headers: {
        ContentType: "application/json; charset=utf-8",
      },
      body: JSON.stringify({
        workflowId: detail.value.id,
        input: apiInputParams.value,
      }),
    });

    if (!res.ok) {
      throw new Error(`Request failed with status ${res.status}`);
    }

    const data = await res.json();
    // Handle success response
  } catch (error) {
    console.error('Failed to publish:', error);
    MsgError(t('workflow.common.networkError'));
  }
}

Key Changes:

  • Safe Stringification: Added safeStringify helper function to safely stringify objects into query parameters.
  • URL Safe Encoding: Ensured parameter values are safely encoded for use in URLs.
  • Base URL Logic Check: Implemented a simple check to ensure a valid base URL is used, providing a fallback if necessary.
  • Removed Unnecessary Variable Assignment: Removed unnecessary reassignments of urlParams.

These changes improve the robustness and maintainability of the code.

Expand Down
Loading