Skip to content

Commit 6b6e12e

Browse files
authored
fix: prevent dumping JSON files with keyword preserve markers in client grants (#1040)
* Prevent dumping JSON files with keyword preserve markers in client grants * Enhance client grants handling by adding keyword replacement for audience and client names, ensuring proper naming during JSON dump. * Refactor client grant name generation to streamline keyword replacement and ensure valid names
1 parent 0697bf6 commit 6b6e12e

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

src/context/directory/handlers/clientGrants.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path';
22
import fs from 'fs-extra';
33
import { Client, ResourceServer } from 'auth0';
4-
import { constants } from '../../../tools';
4+
import { constants, keywordReplace } from '../../../tools';
55

66
import {
77
getFiles,
@@ -16,6 +16,7 @@ import DirectoryContext from '..';
1616
import { ParsedAsset } from '../../../types';
1717
import { ClientGrant } from '../../../tools/auth0/handlers/clientGrants';
1818
import { paginate } from '../../../tools/auth0/client';
19+
import { doesHaveKeywordMarker } from '../../../keywordPreservation';
1920

2021
type ParsedClientGrants = ParsedAsset<'clientGrants', ClientGrant[]>;
2122

@@ -71,28 +72,41 @@ async function dump(context: DirectoryContext): Promise<void> {
7172
}
7273

7374
const clientName = (() => {
74-
const associatedClient = allClients.find((client) => {
75-
return client.client_id === grant.client_id;
76-
});
75+
const associatedClient = allClients.find((client) => client.client_id === grant.client_id);
7776

7877
if (associatedClient === undefined) return grant.client_id;
7978

8079
return associatedClient.name;
8180
})();
8281

83-
const apiName = (() => {
84-
const associatedAPI = allResourceServers.find((resourceServer) => {
85-
return resourceServer.identifier === grant.audience;
86-
});
82+
// Convert audience to the API name for readability
83+
const apiName = (grantAudience: string) => {
84+
const associatedAPI = allResourceServers.find(
85+
(resourceServer) => resourceServer.identifier === grantAudience
86+
);
8787

88-
if (associatedAPI === undefined) return grant.audience;
88+
if (associatedAPI === undefined) return grantAudience; // Use the audience if the API is not found
8989

90-
return associatedAPI.name;
91-
})();
90+
return associatedAPI.name; // Use the name of the API
91+
};
9292

93-
const name = sanitize(`${clientName}-${apiName}`);
94-
const grantFile = path.join(grantsFolder, `${name}.json`);
93+
// Replace keyword markers if necessary
94+
const clientNameNonMarker = doesHaveKeywordMarker(clientName, context.mappings)
95+
? keywordReplace(clientName, context.mappings)
96+
: clientName;
97+
const apiAudienceNonMarker = doesHaveKeywordMarker(grant.audience, context.mappings)
98+
? keywordReplace(grant.audience, context.mappings)
99+
: grant.audience;
100+
101+
// Construct the name using non-marker names
102+
const name = sanitize(`${clientNameNonMarker}-${apiName(apiAudienceNonMarker)}`);
95103

104+
// Ensure the name is not empty or invalid
105+
if (!name || name.trim().length === 0) {
106+
throw new Error(`Invalid name generated for client grant: ${JSON.stringify(grant)}`);
107+
}
108+
109+
const grantFile = path.join(grantsFolder, `${name}.json`);
96110
dumpJSON(grantFile, dumpGrant);
97111
});
98112
}

src/keywordPreservation.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ export const preserveKeywords = ({
297297
);
298298
}
299299

300+
// Update the clientGrants audience field if it exists
301+
if (updatedRemoteAssets && (updatedRemoteAssets as any).clientGrants) {
302+
for (let i = 0; i < (updatedRemoteAssets as any).clientGrants.length; i++) {
303+
const clientGrant = (updatedRemoteAssets as any).clientGrants[i];
304+
if (clientGrant.audience === remoteValue) {
305+
clientGrant.audience = localValue;
306+
}
307+
(updatedRemoteAssets as any).clientGrants[i] = clientGrant;
308+
}
309+
}
310+
300311
// Two address possibilities are provided to account for cases when there is a keyword
301312
// in the resources's identifier field. When the resource identifier's field is preserved
302313
// on the remote assets tree, it loses its identify, so we'll need to try two addresses:

0 commit comments

Comments
 (0)