Skip to content

Commit 049836a

Browse files
fix: Remove Exposed Speech Service API Key from Network Calls (#1869)
1 parent 400d1f8 commit 049836a

File tree

5 files changed

+13
-17
lines changed

5 files changed

+13
-17
lines changed

code/create_app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ def speech_config():
526526
if response.status_code == 200:
527527
return {
528528
"token": response.text,
529-
"key": speech_key,
530529
"region": env_helper.AZURE_SPEECH_SERVICE_REGION,
531530
"languages": env_helper.AZURE_SPEECH_RECOGNIZER_LANGUAGES,
532531
}

code/frontend/src/components/Answer/Answer.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { conversationResponseWithCitations } from "../../../__mocks__/SampleData
1313
jest.mock('microsoft-cognitiveservices-speech-sdk', () => {
1414
return {
1515
SpeechConfig: {
16-
fromSubscription: jest.fn(),
16+
fromAuthorizationToken: jest.fn(),
1717
},
1818
AudioConfig: {
1919
fromDefaultSpeakerOutput: jest.fn(),
@@ -51,7 +51,7 @@ const speechMockData = {
5151
jest.mock("microsoft-cognitiveservices-speech-sdk", () => {
5252
return {
5353
SpeechConfig: {
54-
fromSubscription: jest.fn(),
54+
fromAuthorizationToken: jest.fn(),
5555
fromSpeakerOutput: jest.fn().mockReturnValue({}),
5656
},
5757
AudioConfig: {

code/frontend/src/components/Answer/Answer.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const Answer = ({
5151
const [audioContext, setAudioContext] = useState<AudioContext | null>(null); //Manully manage the audio context eg pausing resuming
5252

5353
const [synthesizerData, setSynthesizerData] = useState({
54-
key: "",
54+
token: "",
5555
region: "",
5656
});
5757
const [synthesizer, setSynthesizer] =
@@ -70,8 +70,8 @@ export const Answer = ({
7070
};
7171

7272
const initializeSynthesizer = () => {
73-
const speechConfig = sdk.SpeechConfig.fromSubscription(
74-
synthesizerData.key,
73+
const speechConfig = sdk.SpeechConfig.fromAuthorizationToken(
74+
synthesizerData.token,
7575
synthesizerData.region
7676
);
7777
const newAudioDestination = new SpeechSDK.SpeakerAudioDestination();
@@ -90,7 +90,7 @@ export const Answer = ({
9090
};
9191

9292
useEffect(() => {
93-
if (synthesizerData.key != "") {
93+
if (synthesizerData.token != "") {
9494
initializeSynthesizer();
9595

9696
return () => {
@@ -112,12 +112,12 @@ export const Answer = ({
112112
const response = await fetch("/api/speech");
113113
try {
114114
if (!response.ok) {
115-
throw new Error("Network response was not ok");
115+
throw new Error("Network response was not ok");
116116
}
117-
const data = await response.json();
118-
setSynthesizerData({ key: data.key, region: data.region });
119-
} catch(e) {
120-
console.log(e)
117+
const data = await response.json();
118+
setSynthesizerData({ token: data.token, region: data.region });
119+
} catch (e) {
120+
console.log(e);
121121
}
122122
};
123123
fetchSythesizerData();
@@ -334,7 +334,7 @@ export const Answer = ({
334334
}
335335
data-testid="toggle-citations-list"
336336
>
337-
<Text className={styles.accordionTitle}>
337+
<Text className={styles.accordionTitle}>
338338
<span data-testid="no-of-references">
339339
{parsedAnswer.citations.length > 1
340340
? parsedAnswer.citations.length + " references"
@@ -375,7 +375,7 @@ export const Answer = ({
375375
onKeyDown={(e) =>
376376
e.key === " " || e.key === "Enter"
377377
? onCitationClicked(citation)
378-
: () => {}
378+
: () => { }
379379
}
380380
tabIndex={0}
381381
title={createCitationFilepath(citation, ++idx)}

code/tests/functional/tests/backend_api/default/test_speech_token.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def test_speech_token_returned(app_url: str, app_config: AppConfig):
2020
"token": "speech-token",
2121
"region": app_config.get("AZURE_SPEECH_SERVICE_REGION"),
2222
"languages": app_config.get("AZURE_SPEECH_RECOGNIZER_LANGUAGES").split(","),
23-
"key": "some-azure-speech-service-key",
2423
}
2524
assert response.headers["Content-Type"] == "application/json"
2625

code/tests/test_app.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ def test_returns_speech_token_using_keys(
115115
"token": "speech-token",
116116
"region": AZURE_SPEECH_SERVICE_REGION,
117117
"languages": AZURE_SPEECH_RECOGNIZER_LANGUAGES,
118-
"key": "mock-speech-key",
119118
}
120119

121120
requests.post.assert_called_once_with(
@@ -159,7 +158,6 @@ def test_returns_speech_token_using_rbac(
159158
"token": "speech-token",
160159
"region": AZURE_SPEECH_SERVICE_REGION,
161160
"languages": AZURE_SPEECH_RECOGNIZER_LANGUAGES,
162-
"key": "mock-key1",
163161
}
164162

165163
requests.post.assert_called_once_with(

0 commit comments

Comments
 (0)