Skip to content

Commit a209082

Browse files
authored
fix(shared-ini-file-loader): ignore prohibited profile name (#1764)
* fix(shared-ini-file-loader): ignore prohibited profile name * fix: address feedbacks
1 parent 7e7feb1 commit a209082

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

Diff for: packages/shared-ini-file-loader/src/index.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,20 @@ aws_session_token = ${FOO_CREDS.sessionToken}`.trim()
289289
},
290290
});
291291
});
292+
293+
it("should ignore profile name in block list", async () => {
294+
__addMatcher(
295+
DEFAULT_PATH,
296+
`
297+
[__proto__]
298+
foo = not_exist`.trim()
299+
);
300+
301+
expect(await loadSharedConfigFiles()).toEqual({
302+
configFile: {},
303+
credentialsFile: {},
304+
});
305+
});
292306
});
293307

294308
describe("shared config file", () => {
@@ -527,5 +541,19 @@ aws_session_token = ${FOO_CREDS.sessionToken}`.trim()
527541
configFile: { default: parsed.default },
528542
});
529543
});
544+
545+
it("should ignore profile name in block list", async () => {
546+
__addMatcher(
547+
DEFAULT_PATH,
548+
`
549+
[profile __proto__]
550+
foo = not_exist`.trim()
551+
);
552+
553+
expect(await loadSharedConfigFiles()).toEqual({
554+
configFile: {},
555+
credentialsFile: {},
556+
});
557+
});
530558
});
531559
});

Diff for: packages/shared-ini-file-loader/src/index.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface SharedConfigFiles {
3636

3737
const swallowError = () => ({});
3838

39-
export function loadSharedConfigFiles(init: SharedConfigInit = {}): Promise<SharedConfigFiles> {
39+
export const loadSharedConfigFiles = (init: SharedConfigInit = {}): Promise<SharedConfigFiles> => {
4040
const {
4141
filepath = process.env[ENV_CREDENTIALS_PATH] || join(getHomeDir(), ".aws", "credentials"),
4242
configFilepath = process.env[ENV_CONFIG_PATH] || join(getHomeDir(), ".aws", "config"),
@@ -52,10 +52,10 @@ export function loadSharedConfigFiles(init: SharedConfigInit = {}): Promise<Shar
5252
credentialsFile,
5353
};
5454
});
55-
}
55+
};
5656

5757
const profileKeyRegex = /^profile\s(["'])?([^\1]+)\1$/;
58-
function normalizeConfigFile(data: ParsedIniData): ParsedIniData {
58+
const normalizeConfigFile = (data: ParsedIniData): ParsedIniData => {
5959
const map: ParsedIniData = {};
6060
for (const key of Object.keys(data)) {
6161
let matches: Array<string> | null;
@@ -71,16 +71,20 @@ function normalizeConfigFile(data: ParsedIniData): ParsedIniData {
7171
}
7272

7373
return map;
74-
}
74+
};
7575

76-
function parseIni(iniData: string): ParsedIniData {
76+
const profileNameBlockList = ["__proto__", "profile __proto__"];
77+
const parseIni = (iniData: string): ParsedIniData => {
7778
const map: ParsedIniData = {};
7879
let currentSection: string | undefined;
7980
for (let line of iniData.split(/\r?\n/)) {
8081
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
8182
const section = line.match(/^\s*\[([^\[\]]+)]\s*$/);
8283
if (section) {
8384
currentSection = section[1];
85+
if (profileNameBlockList.includes(currentSection)) {
86+
throw new Error(`Found invalid profile name "${currentSection}"`);
87+
}
8488
} else if (currentSection) {
8589
const item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
8690
if (item) {
@@ -91,10 +95,10 @@ function parseIni(iniData: string): ParsedIniData {
9195
}
9296

9397
return map;
94-
}
98+
};
9599

96-
function slurpFile(path: string): Promise<string> {
97-
return new Promise((resolve, reject) => {
100+
const slurpFile = (path: string): Promise<string> =>
101+
new Promise((resolve, reject) => {
98102
readFile(path, "utf8", (err, data) => {
99103
if (err) {
100104
reject(err);
@@ -103,14 +107,13 @@ function slurpFile(path: string): Promise<string> {
103107
}
104108
});
105109
});
106-
}
107110

108-
function getHomeDir(): string {
111+
const getHomeDir = (): string => {
109112
const { HOME, USERPROFILE, HOMEPATH, HOMEDRIVE = `C:${sep}` } = process.env;
110113

111114
if (HOME) return HOME;
112115
if (USERPROFILE) return USERPROFILE;
113116
if (HOMEPATH) return `${HOMEDRIVE}${HOMEPATH}`;
114117

115118
return homedir();
116-
}
119+
};

0 commit comments

Comments
 (0)