Skip to content

Commit 2e11654

Browse files
Fix WAF API ingress and harden web auth configuration
1 parent a6e0b38 commit 2e11654

5 files changed

Lines changed: 51 additions & 10 deletions

File tree

infra/main.bicep

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ module avmContainerApp_API 'br/public:avm/res/app/container-app:0.19.0' = {
11191119
}
11201120
]
11211121
}
1122-
ingressExternal: true
1122+
ingressExternal: !enablePrivateNetworking
11231123
activeRevisionsMode: 'Single'
11241124
ingressTransport: 'auto'
11251125
corsPolicy: {
@@ -1771,7 +1771,7 @@ module avmContainerApp_API_update 'br/public:avm/res/app/container-app:0.19.0' =
17711771
}
17721772
]
17731773
}
1774-
ingressExternal: true
1774+
ingressExternal: !enablePrivateNetworking
17751775
activeRevisionsMode: 'Single'
17761776
ingressTransport: 'auto'
17771777
corsPolicy: {

infra/main_custom.bicep

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ module avmContainerApp_API 'br/public:avm/res/app/container-app:0.19.0' = {
11321132
}
11331133
]
11341134
}
1135-
ingressExternal: true
1135+
ingressExternal: !enablePrivateNetworking
11361136
activeRevisionsMode: 'Single'
11371137
ingressTransport: 'auto'
11381138
corsPolicy: {
@@ -1804,7 +1804,7 @@ module avmContainerApp_API_update 'br/public:avm/res/app/container-app:0.19.0' =
18041804
}
18051805
]
18061806
}
1807-
ingressExternal: true
1807+
ingressExternal: !enablePrivateNetworking
18081808
activeRevisionsMode: 'Single'
18091809
ingressTransport: 'auto'
18101810
corsPolicy: {

src/ContentProcessorWeb/azure_cicd.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ steps:
9191
--image $(acrLoginServer)/$(imageRepository):$(Build.BuildId) \
9292
--cpu 4.0 \
9393
--memory 8.0Gi \
94-
--set-env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP__WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
94+
--set-env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP_WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
9595
else
9696
# Create the container app with the new image and registry settings
9797
az containerapp create \
@@ -104,7 +104,7 @@ steps:
104104
--registry-server $(acrLoginServer) \
105105
--registry-identity $managedIdentityResourceId \
106106
--ingress external \
107-
--env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP__WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
107+
--env-vars APP_API_BASE_URL=$cpsApiBaseUrl APP_WEB_CLIENT_ID=$(appWebClientId) APP_WEB_AUTHORITY=$(appWebAuthority) APP_WEB_SCOPE=$(appWebScope) APP_API_SCOPE=$(appApiScope) APP_AUTH_ENABLED=false
108108
109109
fi
110110
displayName: 'Deploy container to Azure Container App'

src/ContentProcessorWeb/src/Services/httpUtility.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111
const api: string = process.env.REACT_APP_API_BASE_URL as string;
1212

13+
const isAuthEnabled = (): boolean =>
14+
process.env.REACT_APP_AUTH_ENABLED?.toLowerCase() !== 'false';
15+
16+
const isUsableToken = (token: string | null): token is string => {
17+
if (!token) return false;
18+
const value = token.trim();
19+
if (!value) return false;
20+
if (value.toLowerCase() === 'null' || value.toLowerCase() === 'undefined') {
21+
return false;
22+
}
23+
if (value.startsWith('APP_')) return false;
24+
return true;
25+
};
26+
1327
interface FetchResponse<T> {
1428
data: T | null;
1529
status: number;
@@ -73,11 +87,14 @@ const fetchWithAuth = async <T>(
7387
const token = localStorage.getItem('token');
7488

7589
const headers: Record<string, string> = {
76-
'Authorization': `Bearer ${token}`,
7790
'Accept': 'application/json',
7891
'Cache-Control': 'no-cache',
7992
};
8093

94+
if (isAuthEnabled() && isUsableToken(token)) {
95+
headers['Authorization'] = `Bearer ${token}`;
96+
}
97+
8198
let processedBody: BodyInit | null = null;
8299
if (body instanceof FormData) {
83100
processedBody = body;
@@ -132,12 +149,15 @@ const fetchHeadersWithAuth = async <T>(
132149
const token = localStorage.getItem('token');
133150

134151
const headers: Record<string, string> = {
135-
'Authorization': `Bearer ${token}`,
136152
'Content-Type': 'application/json',
137153
'Accept': 'application/json',
138154
'Cache-Control': 'no-cache',
139155
};
140156

157+
if (isAuthEnabled() && isUsableToken(token)) {
158+
headers['Authorization'] = `Bearer ${token}`;
159+
}
160+
141161
if (body instanceof FormData) {
142162
delete headers['Content-Type'];
143163
} else {

src/ContentProcessorWeb/src/msal-auth/msaConfig.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
*/
88
import { Configuration, LogLevel } from '@azure/msal-browser';
99

10+
const isUsableScope = (scope?: string): scope is string => {
11+
if (!scope) return false;
12+
const value = scope.trim();
13+
if (!value) return false;
14+
// Guard against unresolved placeholders from env substitution.
15+
if (value.startsWith('APP_')) return false;
16+
if (value.startsWith('<') && value.endsWith('>')) return false;
17+
return true;
18+
};
19+
1020
export const msalConfig: Configuration = {
1121
auth: {
1222
clientId: process.env.REACT_APP_WEB_CLIENT_ID as string,
@@ -31,14 +41,25 @@ export const msalConfig: Configuration = {
3141
const loginScope = process.env.REACT_APP_WEB_SCOPE as string;
3242
const tokenScope = process.env.REACT_APP_API_SCOPE as string;
3343

44+
const loginScopes = ['user.read'];
45+
if (isUsableScope(loginScope)) {
46+
loginScopes.push(loginScope);
47+
}
48+
49+
const tokenScopes = isUsableScope(tokenScope)
50+
? [tokenScope]
51+
: isUsableScope(loginScope)
52+
? [loginScope]
53+
: ['user.read'];
54+
3455
export const loginRequest = {
35-
scopes: ["user.read", loginScope],
56+
scopes: loginScopes,
3657
};
3758

3859
export const graphConfig = {
3960
graphMeEndpoint: "https://graph.microsoft.com/v1.0/me",
4061
};
4162

4263
export const tokenRequest = {
43-
scopes: [tokenScope],
64+
scopes: tokenScopes,
4465
};

0 commit comments

Comments
 (0)