Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions apps/sim/app/api/tools/hubspot/lists/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type NextRequest, NextResponse } from 'next/server'
import { hubspotListsSelectorContract } from '@/lib/api/contracts/selectors/hubspot'
import { parseRequest } from '@/lib/api/server'
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
import { validateAlphanumericId } from '@/lib/core/security/input-validation'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
Expand All @@ -27,6 +28,12 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
if (!parsed.success) return parsed.response
const { credentialId, objectTypeId, query } = parsed.data.query

const credentialIdValidation = validateAlphanumericId(credentialId, 'credentialId', 255)
if (!credentialIdValidation.isValid) {
logger.warn(`[${requestId}] Invalid credential ID: ${credentialIdValidation.error}`)
return NextResponse.json({ error: credentialIdValidation.error }, { status: 400 })
}

const authz = await authorizeCredentialUse(request, {
credentialId,
requireWorkflowIdForInternal: false,
Expand Down
7 changes: 7 additions & 0 deletions apps/sim/app/api/tools/hubspot/owners/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type NextRequest, NextResponse } from 'next/server'
import { hubspotOwnersSelectorContract } from '@/lib/api/contracts/selectors/hubspot'
import { parseRequest } from '@/lib/api/server'
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
import { validateAlphanumericId } from '@/lib/core/security/input-validation'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
Expand All @@ -27,6 +28,12 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
if (!parsed.success) return parsed.response
const { credentialId, query } = parsed.data.query

const credentialIdValidation = validateAlphanumericId(credentialId, 'credentialId', 255)
if (!credentialIdValidation.isValid) {
logger.warn(`[${requestId}] Invalid credential ID: ${credentialIdValidation.error}`)
return NextResponse.json({ error: credentialIdValidation.error }, { status: 400 })
}

const authz = await authorizeCredentialUse(request, {
credentialId,
requireWorkflowIdForInternal: false,
Expand Down
7 changes: 7 additions & 0 deletions apps/sim/app/api/tools/hubspot/pipelines/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type NextRequest, NextResponse } from 'next/server'
import { hubspotPipelinesSelectorContract } from '@/lib/api/contracts/selectors/hubspot'
import { parseRequest } from '@/lib/api/server'
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
import { validateAlphanumericId } from '@/lib/core/security/input-validation'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
Expand Down Expand Up @@ -33,6 +34,12 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
if (!parsed.success) return parsed.response
const { credentialId, objectType } = parsed.data.query

const credentialIdValidation = validateAlphanumericId(credentialId, 'credentialId', 255)
if (!credentialIdValidation.isValid) {
logger.warn(`[${requestId}] Invalid credential ID: ${credentialIdValidation.error}`)
return NextResponse.json({ error: credentialIdValidation.error }, { status: 400 })
}

const authz = await authorizeCredentialUse(request, {
credentialId,
requireWorkflowIdForInternal: false,
Expand Down
7 changes: 7 additions & 0 deletions apps/sim/app/api/tools/hubspot/properties/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type NextRequest, NextResponse } from 'next/server'
import { hubspotPropertiesSelectorContract } from '@/lib/api/contracts/selectors/hubspot'
import { parseRequest } from '@/lib/api/server'
import { authorizeCredentialUse } from '@/lib/auth/credential-access'
import { validateAlphanumericId } from '@/lib/core/security/input-validation'
import { generateRequestId } from '@/lib/core/utils/request'
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
import { refreshAccessTokenIfNeeded } from '@/app/api/auth/oauth/utils'
Expand Down Expand Up @@ -36,6 +37,12 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
if (!parsed.success) return parsed.response
const { credentialId, objectType, query } = parsed.data.query

const credentialIdValidation = validateAlphanumericId(credentialId, 'credentialId', 255)
if (!credentialIdValidation.isValid) {
logger.warn(`[${requestId}] Invalid credential ID: ${credentialIdValidation.error}`)
return NextResponse.json({ error: credentialIdValidation.error }, { status: 400 })
}

const authz = await authorizeCredentialUse(request, {
credentialId,
requireWorkflowIdForInternal: false,
Expand Down
50 changes: 27 additions & 23 deletions apps/sim/triggers/hubspot/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ import type { TriggerConfig } from '@/triggers/types'

const logger = createLogger('HubSpotPollingTrigger')

/**
* Resolves the effective object type from the subblock store. `getValue` returns `null`
* for fields the user hasn't interacted with yet, so we fall back to the dropdown's
* default ('contact') — otherwise the cascading property selectors render empty on
* first render even when the dropdown visibly shows "contact".
*/
function resolveSelectedObjectType(blockId: string): string | null {
const objectType = useSubBlockStore.getState().getValue(blockId, 'objectType') as string | null
const customId = useSubBlockStore.getState().getValue(blockId, 'customObjectTypeId') as
| string
| null
const selected = objectType ?? 'contact'
if (selected === 'custom') {
const trimmed = customId?.trim()
return trimmed ? trimmed : null
}
return selected
}

async function fetchHubSpotProperties(blockId: string, objectType: string) {
const credentialId = useSubBlockStore.getState().getValue(blockId, 'triggerCredentials') as
| string
Expand Down Expand Up @@ -128,13 +147,7 @@ export const hubspotPollingTrigger: TriggerConfig = {
placeholder: 'Select a property',
options: [],
fetchOptions: async (blockId: string) => {
const objectType = useSubBlockStore.getState().getValue(blockId, 'objectType') as
| string
| null
const customId = useSubBlockStore.getState().getValue(blockId, 'customObjectTypeId') as
| string
| null
const resolved = objectType === 'custom' ? customId : objectType
const resolved = resolveSelectedObjectType(blockId)
if (!resolved) throw new Error('Select an object type first')
try {
return await fetchHubSpotProperties(blockId, resolved)
Expand Down Expand Up @@ -162,13 +175,7 @@ export const hubspotPollingTrigger: TriggerConfig = {
placeholder: 'Select properties (optional)',
options: [],
fetchOptions: async (blockId: string) => {
const objectType = useSubBlockStore.getState().getValue(blockId, 'objectType') as
| string
| null
const customId = useSubBlockStore.getState().getValue(blockId, 'customObjectTypeId') as
| string
| null
const resolved = objectType === 'custom' ? customId : objectType
const resolved = resolveSelectedObjectType(blockId)
Comment thread
waleedlatif1 marked this conversation as resolved.
if (!resolved) return []
try {
return await fetchHubSpotProperties(blockId, resolved)
Expand All @@ -193,10 +200,8 @@ export const hubspotPollingTrigger: TriggerConfig = {
const credentialId = useSubBlockStore.getState().getValue(blockId, 'triggerCredentials') as
| string
| null
const objectType = useSubBlockStore.getState().getValue(blockId, 'objectType') as
| string
| null
if (!credentialId || !objectType) return []
const objectType = resolveSelectedObjectType(blockId) ?? 'contact'
if (!credentialId) throw new Error('No HubSpot credential selected')
if (isCredentialSetValue(credentialId)) return []
try {
const data = await requestJson(hubspotPipelinesSelectorContract, {
Expand Down Expand Up @@ -224,14 +229,13 @@ export const hubspotPollingTrigger: TriggerConfig = {
const credentialId = useSubBlockStore.getState().getValue(blockId, 'triggerCredentials') as
| string
| null
const objectType = useSubBlockStore.getState().getValue(blockId, 'objectType') as
| string
| null
const objectType = resolveSelectedObjectType(blockId) ?? 'contact'
const pipelineId = useSubBlockStore.getState().getValue(blockId, 'pipelineId') as
| string
| null
if (!credentialId || !objectType || !pipelineId) return []
if (!credentialId) throw new Error('No HubSpot credential selected')
if (isCredentialSetValue(credentialId)) return []
if (!pipelineId) return []
try {
const data = await requestJson(hubspotPipelinesSelectorContract, {
query: { credentialId, objectType },
Expand Down Expand Up @@ -259,7 +263,7 @@ export const hubspotPollingTrigger: TriggerConfig = {
const credentialId = useSubBlockStore.getState().getValue(blockId, 'triggerCredentials') as
| string
| null
if (!credentialId) return []
if (!credentialId) throw new Error('No HubSpot credential selected')
if (isCredentialSetValue(credentialId)) return []
try {
const data = await requestJson(hubspotOwnersSelectorContract, {
Expand Down
Loading