From 558277cfff34e2de0776209a4df6021d3a692aa0 Mon Sep 17 00:00:00 2001 From: Prasanjeet-Microsoft Date: Fri, 25 Jul 2025 15:26:30 +0530 Subject: [PATCH 1/3] Removed exposed Speech Service API key from network calls --- code/create_app.py | 1 - .../frontend/src/components/Answer/Answer.tsx | 22 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/code/create_app.py b/code/create_app.py index 0c977415a..4822eba50 100644 --- a/code/create_app.py +++ b/code/create_app.py @@ -526,7 +526,6 @@ def speech_config(): if response.status_code == 200: return { "token": response.text, - "key": speech_key, "region": env_helper.AZURE_SPEECH_SERVICE_REGION, "languages": env_helper.AZURE_SPEECH_RECOGNIZER_LANGUAGES, } diff --git a/code/frontend/src/components/Answer/Answer.tsx b/code/frontend/src/components/Answer/Answer.tsx index bae26108e..d9cb70ec0 100644 --- a/code/frontend/src/components/Answer/Answer.tsx +++ b/code/frontend/src/components/Answer/Answer.tsx @@ -51,7 +51,7 @@ export const Answer = ({ const [audioContext, setAudioContext] = useState(null); //Manully manage the audio context eg pausing resuming const [synthesizerData, setSynthesizerData] = useState({ - key: "", + token: "", region: "", }); const [synthesizer, setSynthesizer] = @@ -70,8 +70,8 @@ export const Answer = ({ }; const initializeSynthesizer = () => { - const speechConfig = sdk.SpeechConfig.fromSubscription( - synthesizerData.key, + const speechConfig = sdk.SpeechConfig.fromAuthorizationToken( + synthesizerData.token, synthesizerData.region ); const newAudioDestination = new SpeechSDK.SpeakerAudioDestination(); @@ -90,7 +90,7 @@ export const Answer = ({ }; useEffect(() => { - if (synthesizerData.key != "") { + if (synthesizerData.token != "") { initializeSynthesizer(); return () => { @@ -112,12 +112,12 @@ export const Answer = ({ const response = await fetch("/api/speech"); try { if (!response.ok) { - throw new Error("Network response was not ok"); + throw new Error("Network response was not ok"); } - const data = await response.json(); - setSynthesizerData({ key: data.key, region: data.region }); - } catch(e) { - console.log(e) + const data = await response.json(); + setSynthesizerData({ token: data.token, region: data.region }); + } catch (e) { + console.log(e); } }; fetchSythesizerData(); @@ -334,7 +334,7 @@ export const Answer = ({ } data-testid="toggle-citations-list" > - + {parsedAnswer.citations.length > 1 ? parsedAnswer.citations.length + " references" @@ -375,7 +375,7 @@ export const Answer = ({ onKeyDown={(e) => e.key === " " || e.key === "Enter" ? onCitationClicked(citation) - : () => {} + : () => { } } tabIndex={0} title={createCitationFilepath(citation, ++idx)} From 13f913bcd4857a948671ef7e7a49039e88bc3bf6 Mon Sep 17 00:00:00 2001 From: Prasanjeet-Microsoft Date: Fri, 25 Jul 2025 16:06:16 +0530 Subject: [PATCH 2/3] update speech token test cases to remove key assertion --- .../functional/tests/backend_api/default/test_speech_token.py | 1 - code/tests/test_app.py | 2 -- 2 files changed, 3 deletions(-) diff --git a/code/tests/functional/tests/backend_api/default/test_speech_token.py b/code/tests/functional/tests/backend_api/default/test_speech_token.py index ba02365b2..1388b0cd5 100644 --- a/code/tests/functional/tests/backend_api/default/test_speech_token.py +++ b/code/tests/functional/tests/backend_api/default/test_speech_token.py @@ -20,7 +20,6 @@ def test_speech_token_returned(app_url: str, app_config: AppConfig): "token": "speech-token", "region": app_config.get("AZURE_SPEECH_SERVICE_REGION"), "languages": app_config.get("AZURE_SPEECH_RECOGNIZER_LANGUAGES").split(","), - "key": "some-azure-speech-service-key", } assert response.headers["Content-Type"] == "application/json" diff --git a/code/tests/test_app.py b/code/tests/test_app.py index 56865f185..1e2fe6240 100644 --- a/code/tests/test_app.py +++ b/code/tests/test_app.py @@ -115,7 +115,6 @@ def test_returns_speech_token_using_keys( "token": "speech-token", "region": AZURE_SPEECH_SERVICE_REGION, "languages": AZURE_SPEECH_RECOGNIZER_LANGUAGES, - "key": "mock-speech-key", } requests.post.assert_called_once_with( @@ -159,7 +158,6 @@ def test_returns_speech_token_using_rbac( "token": "speech-token", "region": AZURE_SPEECH_SERVICE_REGION, "languages": AZURE_SPEECH_RECOGNIZER_LANGUAGES, - "key": "mock-key1", } requests.post.assert_called_once_with( From 679f21d6f813aa2ea3a0ca864731e7b5fab261b8 Mon Sep 17 00:00:00 2001 From: Prasanjeet-Microsoft Date: Fri, 25 Jul 2025 17:39:54 +0530 Subject: [PATCH 3/3] modified frontend unit test case (Answer.test.tsx) --- code/frontend/src/components/Answer/Answer.test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/frontend/src/components/Answer/Answer.test.tsx b/code/frontend/src/components/Answer/Answer.test.tsx index 9be303f68..d32b1fcb7 100644 --- a/code/frontend/src/components/Answer/Answer.test.tsx +++ b/code/frontend/src/components/Answer/Answer.test.tsx @@ -13,7 +13,7 @@ import { conversationResponseWithCitations } from "../../../__mocks__/SampleData jest.mock('microsoft-cognitiveservices-speech-sdk', () => { return { SpeechConfig: { - fromSubscription: jest.fn(), + fromAuthorizationToken: jest.fn(), }, AudioConfig: { fromDefaultSpeakerOutput: jest.fn(), @@ -51,7 +51,7 @@ const speechMockData = { jest.mock("microsoft-cognitiveservices-speech-sdk", () => { return { SpeechConfig: { - fromSubscription: jest.fn(), + fromAuthorizationToken: jest.fn(), fromSpeakerOutput: jest.fn().mockReturnValue({}), }, AudioConfig: {