-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
feat(stt): support custom Azure Speech transcription endpoint URL #6333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,19 @@ const SpeechToTextType = { | |
| GROQ_WHISPER: 'groqWhisper' | ||
| } | ||
|
|
||
| export const buildAzureSpeechToTextUrl = (serviceRegion: string, apiVersion: string, baseUrl?: string) => { | ||
| const trimmedBaseUrl = baseUrl?.trim() | ||
| const base = trimmedBaseUrl | ||
| ? trimmedBaseUrl.replace(/\/+$/, '') | ||
| : `https://${serviceRegion}.cognitiveservices.azure.com/speechtotext/transcriptions:transcribe` | ||
|
|
||
| if (/[?&]api-version=/.test(base)) { | ||
| return base | ||
| } | ||
|
|
||
| return `${base}${base.includes('?') ? '&' : '?'}api-version=${encodeURIComponent(apiVersion)}` | ||
| } | ||
|
|
||
| export const convertSpeechToText = async (upload: IFileUpload, speechToTextConfig: ICommonObject, options: ICommonObject) => { | ||
| if (speechToTextConfig) { | ||
| const credentialId = speechToTextConfig.credentialId as string | ||
|
|
@@ -76,8 +89,12 @@ export const convertSpeechToText = async (upload: IFileUpload, speechToTextConfi | |
| } | ||
| case SpeechToTextType.AZURE_COGNITIVE: { | ||
| try { | ||
| const baseUrl = `https://${credentialData.serviceRegion}.cognitiveservices.azure.com/speechtotext/transcriptions:transcribe` | ||
| const apiVersion = credentialData.apiVersion || '2024-05-15-preview' | ||
| const azureSpeechToTextUrl = buildAzureSpeechToTextUrl( | ||
| credentialData.serviceRegion, | ||
| apiVersion, | ||
| speechToTextConfig?.baseUrl | ||
| ) | ||
|
|
||
| const formData = new FormData() | ||
| const audioBlob = new Blob([new Uint8Array(audio_file)], { type: upload.type }) | ||
|
|
@@ -93,7 +110,7 @@ export const convertSpeechToText = async (upload: IFileUpload, speechToTextConfi | |
| } | ||
| formData.append('definition', JSON.stringify(definition)) | ||
|
|
||
| const response = await axios.post(`${baseUrl}?api-version=${apiVersion}`, formData, { | ||
| const response = await axios.post(azureSpeechToTextUrl, formData, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the baseUrl is now user-configurable via the UI, using the raw axios client poses a security risk as it bypasses Server-Side Request Forgery (SSRF) protections. It is highly recommended to use the secureAxiosRequest wrapper from ./httpSecurity, which validates the target URL against the repository's deny list (e.g., internal network ranges). Note that you will need to import secureAxiosRequest from ./httpSecurity and adjust the call to use the configuration object syntax. |
||
| headers: { | ||
| 'Ocp-Apim-Subscription-Key': credentialData.azureSubscriptionKey, | ||
| Accept: 'application/json' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the current string-based URL construction works for standard cases, it is susceptible to issues with trailing separators (e.g., ?&) or fragments (e.g., #frag?api-version=...). Using the native URL API would make this helper more robust and maintainable by automatically handling query parameter merging and encoding.