Skip to content

Commit 2f9dbc5

Browse files
gitri-msdehoward
andauthored
Chat Copilot Security Improvements (#126)
### Motivation and Context This PR addresses microsoft/semantic-kernel#1639. It is a combination of PRs #92 and #110 ### Description #### Backend changes - Remove API key authorization - Use "AzureAD" as default authentication configuration for deployments, "None" for running locally (Note: UI changes to disable sign in flow for the latter case are still forthcoming) - Enable auth policy on controllers that checks if the user is part of the conversation they are trying to access This PR changes the contract between the frontend and backend around how user IDs are communicated. Users who have been signing into the frontend with AAD will now only see their chats if the backend is also gated by AAD authentication, which was not the case previously. #### Frontend changes - adds `REACT_APP_AUTH_TYPE` and changes AAD variables in `.env` to be optional - adds `AuthHelper.IsAuthAAD` to conditionally render different elements throughout the app - changes user settings menu popup to instead just show as a settings button: ![image](https://github.com/microsoft/chat-copilot/assets/52973358/342f977d-d011-464d-b122-5eff5f8222ac) Existing users will need to uncomment `REACT_APP_AUTH_TYPE=AzureAd` in `webapp/.env` to continue using AAD as their authorization type. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [Contribution Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Desmond Howard <[email protected]>
1 parent 8bc548f commit 2f9dbc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1103
-652
lines changed

.github/workflows/copilot-deploy-environment.yml

-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@ on:
1616
required: true
1717
AZURE_SUBSCRIPTION_ID:
1818
required: true
19-
WEB_API_KEY:
20-
required: true
2119
AZURE_OPENAI_ENDPOINT:
2220
required: true
23-
APPLICATION_AUTHORITY:
24-
required: true
2521

2622
permissions:
2723
contents: read
@@ -36,7 +32,6 @@ jobs:
3632
AZURE_CLIENT_ID: ${{secrets.AZURE_CLIENT_ID}}
3733
AZURE_TENANT_ID: ${{secrets.AZURE_TENANT_ID}}
3834
AZURE_SUBSCRIPTION_ID: ${{secrets.AZURE_SUBSCRIPTION_ID}}
39-
WEB_API_KEY: ${{secrets.WEB_API_KEY}}
4035
AZURE_OPENAI_ENDPOINT: ${{secrets.AZURE_OPENAI_ENDPOINT}}
4136

4237
deploy-backend:
@@ -61,4 +56,3 @@ jobs:
6156
AZURE_CLIENT_ID: ${{secrets.AZURE_CLIENT_ID}}
6257
AZURE_TENANT_ID: ${{secrets.AZURE_TENANT_ID}}
6358
AZURE_SUBSCRIPTION_ID: ${{secrets.AZURE_SUBSCRIPTION_ID}}
64-
APPLICATION_AUTHORITY: ${{secrets.APPLICATION_AUTHORITY}}

.github/workflows/copilot-deploy-frontend.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ on:
1616
required: true
1717
AZURE_SUBSCRIPTION_ID:
1818
required: true
19-
APPLICATION_AUTHORITY:
20-
required: true
2119

2220
permissions:
2321
contents: read
@@ -90,4 +88,4 @@ jobs:
9088
9189
- name: Deploy SWA
9290
run: |
93-
scripts/deploy/deploy-webapp.sh --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --resource-group ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --deployment-name ${{inputs.DEPLOYMENT_NAME}} --application-id ${{vars.APPLICATION_CLIENT_ID}} --authority ${{secrets.APPLICATION_AUTHORITY}} --no-redirect --version ${{ steps.versiontag.outputs.versiontag }} --version-info "Built from commit ${{ steps.gitversion.outputs.ShortSha }} on $(date +"%Y-%m-%d")"
91+
scripts/deploy/deploy-webapp.sh --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --resource-group ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --deployment-name ${{inputs.DEPLOYMENT_NAME}} --client-id ${{vars.FRONTEND_CLIENT_ID}} --no-redirect --version ${{ steps.versiontag.outputs.versiontag }} --version-info "Built from commit ${{ steps.gitversion.outputs.ShortSha }} on $(date +"%Y-%m-%d")"

.github/workflows/copilot-deploy-infra.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ on:
1313
required: true
1414
AZURE_SUBSCRIPTION_ID:
1515
required: true
16-
WEB_API_KEY:
17-
required: true
1816
AZURE_OPENAI_ENDPOINT:
1917
required: true
2018
outputs:
@@ -61,4 +59,4 @@ jobs:
6159
inlineScript: |
6260
AI_SERVICE_KEY=$(az cognitiveservices account keys list --name ${{vars.AZUREOPENAI__NAME}} --resource-group ${{vars.AZUREOPENAI_DEPLOYMENT_GROUP_NAME}} | jq -r '.key1')
6361
echo "::add-mask::$AI_SERVICE_KEY"
64-
scripts/deploy/deploy-azure.sh --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --web-api-key ${{secrets.WEB_API_KEY}} --resource-group ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --deployment-name ${{steps.deployment-id.outputs.deployment_name}} --region ${{vars.CC_DEPLOYMENT_REGION}} --ai-service AzureOpenAI --ai-endpoint ${{secrets.AZURE_OPENAI_ENDPOINT}} --ai-service-key $AI_SERVICE_KEY --app-service-sku ${{vars.WEBAPP_API_SKU}} --no-deploy-package --debug-deployment
62+
scripts/deploy/deploy-azure.sh --subscription ${{secrets.AZURE_SUBSCRIPTION_ID}} --resource-group ${{vars.CC_DEPLOYMENT_GROUP_NAME}} --deployment-name ${{steps.deployment-id.outputs.deployment_name}} --region ${{vars.CC_DEPLOYMENT_REGION}} --client-id ${{vars.BACKEND_CLIENT_ID}} --tenant-id ${{secrets.AZURE_TENANT_ID}} --instance ${{vars.AZURE_INSTANCE}} --ai-service AzureOpenAI --ai-endpoint ${{secrets.AZURE_OPENAI_ENDPOINT}} --ai-service-key $AI_SERVICE_KEY --app-service-sku ${{vars.WEBAPP_API_SKU}} --no-deploy-package --debug-deployment

.github/workflows/copilot-deploy-pipeline.yml

-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ jobs:
2727
AZURE_CLIENT_ID: ${{secrets.AZURE_CLIENT_ID}}
2828
AZURE_TENANT_ID: ${{secrets.AZURE_TENANT_ID}}
2929
AZURE_SUBSCRIPTION_ID: ${{secrets.AZURE_SUBSCRIPTION_ID}}
30-
WEB_API_KEY: ${{secrets.WEB_API_KEY}}
3130
AZURE_OPENAI_ENDPOINT: ${{secrets.AZURE_OPENAI_ENDPOINT}}
32-
APPLICATION_AUTHORITY: ${{secrets.APPLICATION_AUTHORITY}}
3331

3432
stable:
3533
uses: ./.github/workflows/copilot-deploy-environment.yml
@@ -41,6 +39,4 @@ jobs:
4139
AZURE_CLIENT_ID: ${{secrets.AZURE_CLIENT_ID}}
4240
AZURE_TENANT_ID: ${{secrets.AZURE_TENANT_ID}}
4341
AZURE_SUBSCRIPTION_ID: ${{secrets.AZURE_SUBSCRIPTION_ID}}
44-
WEB_API_KEY: ${{secrets.WEB_API_KEY}}
4542
AZURE_OPENAI_ENDPOINT: ${{secrets.AZURE_OPENAI_ENDPOINT}}
46-
APPLICATION_AUTHORITY: ${{secrets.APPLICATION_AUTHORITY}}

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"**/.hg": true,
4747
"**/CVS": true,
4848
"**/.DS_Store": true,
49-
"**/Thumbs.db": true
49+
"**/Thumbs.db": true,
50+
"**.lock": true,
5051
},
5152
}

README.md

+48-38
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ You will need the following items to run the sample:
1717
- [.NET 7.0 SDK](https://dotnet.microsoft.com/download/dotnet/7.0) _(via Setup script)_
1818
- [Node.js](https://nodejs.org/en/download) _(via Setup script)_
1919
- [Yarn](https://classic.yarnpkg.com/docs/install) _(via Setup script)_
20-
- [Azure account](https://azure.microsoft.com/free)
21-
- [Azure AD Tenant](https://learn.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant)
2220
- AI Service
2321

2422
| AI Service | Requirement |
@@ -28,13 +26,6 @@ You will need the following items to run the sample:
2826

2927
# Instructions
3028

31-
## Register an application
32-
33-
1. Follow [these instructions](https://learn.microsoft.com/azure/active-directory/develop/quickstart-register-app) and use the values below:
34-
- `Supported account types`: "_Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)_"
35-
- `Redirect URI (optional)`: _Single-page application (SPA)_ and use _http://localhost:3000_.
36-
2. Take note of the `Application (client) ID`. Chat Copilot will use this ID for authentication.
37-
3829
## Windows
3930

4031
1. Open PowerShell as an administrator.
@@ -52,19 +43,12 @@ You will need the following items to run the sample:
5243
3. Configure Chat Copilot.
5344

5445
```powershell
55-
.\Configure.ps1 -AIService {AI_SERVICE} -APIKey {API_KEY} -Endpoint {AZURE_OPENAI_ENDPOINT} -ClientId {AZURE_APPLICATION_ID}
46+
.\Configure.ps1 -AIService {AI_SERVICE} -APIKey {API_KEY} -Endpoint {AZURE_OPENAI_ENDPOINT}
5647
```
5748

5849
- `AI_SERVICE`: `AzureOpenAI` or `OpenAI`.
5950
- `API_KEY`: The `API key` for Azure OpenAI or for OpenAI.
6051
- `AZURE_OPENAI_ENDPOINT`: The Azure OpenAI resource `Endpoint` address. Omit `-Endpoint` if using OpenAI.
61-
- `AZURE_APPLICATION_ID`: The `Application (client) ID` associated with the registered application.
62-
63-
- (Optional): To set a specific Tenant Id, use the parameter:
64-
65-
```powershell
66-
-TenantId {TENANT_ID}
67-
```
6852

6953
- > **IMPORTANT:** For `AzureOpenAI`, if you deployed models `gpt-35-turbo` and `text-embedding-ada-002` with custom names (instead of each own's given name), also use the parameters:
7054
@@ -111,19 +95,13 @@ You will need the following items to run the sample:
11195
3. Configure Chat Copilot.
11296

11397
```bash
114-
./Configure.sh --aiservice {AI_SERVICE} --apikey {API_KEY} --endpoint {AZURE_OPENAI_ENDPOINT} --clientid {AZURE_APPLICATION_ID}
98+
./Configure.sh --aiservice {AI_SERVICE} --apikey {API_KEY} --endpoint {AZURE_OPENAI_ENDPOINT}
11599
```
116100

117101
- `AI_SERVICE`: `AzureOpenAI` or `OpenAI`.
118102
- `API_KEY`: The `API key` for Azure OpenAI or for OpenAI.
119103
- `AZURE_OPENAI_ENDPOINT`: The Azure OpenAI resource `Endpoint` address. Omit `--endpoint` if using OpenAI.
120-
- `AZURE_APPLICATION_ID`: The `Application (client) ID` associated with the registered application.
121104

122-
- (Optional): To set a specific Tenant Id, use the parameter:
123-
124-
```bash
125-
--tenantid {TENANT_ID}
126-
```
127105

128106
- > **IMPORTANT:** For `AzureOpenAI`, if you deployed models `gpt-35-turbo` and `text-embedding-ada-002` with custom names (instead of each own's given name), also use the parameters:
129107
@@ -141,27 +119,36 @@ You will need the following items to run the sample:
141119

142120
> NOTE: Confirm pop-ups are not blocked and you are logged in with the same account used to register the application.
143121

144-
## (Optional) Enable backend authorization via Azure AD
122+
## (Optional) Enable backend authentication via Azure AD
123+
124+
By default, Chat Copilot runs locally without authentication, using a guest user profile. If you want to enable authentication with Azure Active Directory, follow the steps below.
125+
126+
### Requirements
145127

146-
1. Ensure you created the required application registration mentioned in [Register an application](#register-an-application)
128+
- [Azure account](https://azure.microsoft.com/free)
129+
- [Azure AD Tenant](https://learn.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant)
147130

148-
2. Create a second application registration to represent the web api
131+
### Instructions
149132

150-
> For more details on creating an application registration, go [here](https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-app).
133+
1. Create an [application registration](https://learn.microsoft.com/azure/active-directory/develop/quickstart-register-app) for the frontend web app, using the values below
134+
- `Supported account types`: "_Accounts in this organizational directory only ({YOUR TENANT} only - Single tenant)_"
135+
- `Redirect URI (optional)`: _Single-page application (SPA)_ and use _http://localhost:3000_.
151136

152-
1. Give the app registration a name
137+
2. Create a second [application registration](https://learn.microsoft.com/azure/active-directory/develop/quickstart-register-app) for the backend web api, using the values below:
138+
- `Supported account types`: "_Accounts in this organizational directory only ({YOUR TENANT} only - Single tenant)_"
139+
- Do **not** configure a `Redirect URI (optional)`
153140

154-
2. As _Supported account type_ choose `Accounts in any organizational directory and personal Microsoft Accounts`
141+
> NOTE: Other account types can be used to allow multitenant and personal Microsoft accounts to use your application if you desire. Doing so may result in more users and therefore higher costs.
155142

156-
3. Do not configure a _Redirect Uri_
143+
> Take note of the `Application (client) ID` for both app registrations as you will need them in future steps.
157144

158145
3. Expose an API within the second app registration
159146

160147
1. Select _Expose an API_ from the menu
161148

162149
2. Add an _Application ID URI_
163150

164-
1. This will generate an `api://` URI with a generated for you
151+
1. This will generate an `api://` URI
165152

166153
2. Click _Save_ to store the generated URI
167154

@@ -193,17 +180,40 @@ You will need the following items to run the sample:
193180

194181
7. Click _Add permissions_
195182

196-
5. Update frontend web app configuration
183+
5. Run the Configure script with additional parameters to set up authentication.
197184

198-
1. Open _.env_ file
185+
**Powershell**
199186

200-
2. Set the value of `REACT_APP_AAD_API_SCOPE` to your application ID URI followed by the scope `access_as_user`, e.g. `api://12341234-1234-1234-1234-123412341234/access_as_user`
187+
```powershell
188+
.\Configure.ps1 -AiService {AI_SERVICE} -APIKey {API_KEY} -Endpoint {AZURE_OPENAI_ENDPOINT} -FrontendClientId {FRONTEND_APPLICATION_ID} -BackendClientId {BACKEND_APPLICATION_ID} -TenantId {TENANT_ID} -Instance {AZURE_AD_INSTANCE}
189+
```
201190

202-
6. Update backend web api configuration
191+
**Bash**
192+
```bash
193+
./Configure.sh --aiservice {AI_SERVICE} --apikey {API_KEY} --endpoint {AZURE_OPENAI_ENDPOINT} --frontend-clientid {FRONTEND_APPLICATION_ID} --backend-clientid {BACKEND_APPLICATION_ID} --tenantid {TENANT_ID} --instance {AZURE_AD_INSTANCE}
194+
```
203195

204-
1. Open _appsettings.json_
196+
- `AI_SERVICE`: `AzureOpenAI` or `OpenAI`.
197+
- `API_KEY`: The `API key` for Azure OpenAI or for OpenAI.
198+
- `AZURE_OPENAI_ENDPOINT`: The Azure OpenAI resource `Endpoint` address. Omit `-Endpoint` if using OpenAI.
199+
- `FRONTEND_APPLICATION_ID`: The `Application (client) ID` associated with the application registration for the frontend.
200+
- `BACKEND_APPLICATION_ID`: The `Application (client) ID` associated with the application registration for the backend.
201+
- `TENANT_ID` : Your Azure AD tenant ID
202+
- `AZURE_AD_INSTANCE` _(optional)_: The Azure AD cloud instance for the authenticating users. Defaults to `https://login.microsoftonline.com`.
205203

206-
2. Set the value of `Authorization:AzureAd:Audience` to your application ID URI
204+
6. Run Chat Copilot locally. This step starts both the backend API and frontend application.
205+
206+
**Powershell**
207+
208+
```powershell
209+
.\Start.ps1
210+
```
211+
212+
**Bash**
213+
214+
```bash
215+
./Start.sh
216+
```
207217

208218
# Troubleshooting
209219

scripts/.env

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ ENV_PLANNER_MODEL_OPEN_AI="gpt-3.5-turbo"
66
ENV_COMPLETION_MODEL_AZURE_OPEN_AI="gpt-35-turbo"
77
ENV_PLANNER_MODEL_AZURE_OPEN_AI="gpt-35-turbo"
88
ENV_EMBEDDING_MODEL="text-embedding-ada-002"
9-
ENV_TENANT_ID="common"
109
ENV_ASPNETCORE="Development"
10+
ENV_INSTANCE="https://login.microsoftonline.com"
1111

1212
# Constants
1313
ENV_AZURE_OPEN_AI="AzureOpenAI"
1414
ENV_OPEN_AI="OpenAI"
15+
ENV_AZURE_AD="AzureAd"
16+
ENV_NONE="None"
17+
ENV_SCOPES="access_as_user"

0 commit comments

Comments
 (0)