Skip to content

Commit

Permalink
fix(shared-ini-file-loader): ignore prohibited profile name (#1764)
Browse files Browse the repository at this point in the history
* fix(shared-ini-file-loader): ignore prohibited profile name

* fix: address feedbacks
  • Loading branch information
AllanZhengYP authored Dec 11, 2020
1 parent 7e7feb1 commit a209082
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
28 changes: 28 additions & 0 deletions packages/shared-ini-file-loader/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,20 @@ aws_session_token = ${FOO_CREDS.sessionToken}`.trim()
},
});
});

it("should ignore profile name in block list", async () => {
__addMatcher(
DEFAULT_PATH,
`
[__proto__]
foo = not_exist`.trim()
);

expect(await loadSharedConfigFiles()).toEqual({
configFile: {},
credentialsFile: {},
});
});
});

describe("shared config file", () => {
Expand Down Expand Up @@ -527,5 +541,19 @@ aws_session_token = ${FOO_CREDS.sessionToken}`.trim()
configFile: { default: parsed.default },
});
});

it("should ignore profile name in block list", async () => {
__addMatcher(
DEFAULT_PATH,
`
[profile __proto__]
foo = not_exist`.trim()
);

expect(await loadSharedConfigFiles()).toEqual({
configFile: {},
credentialsFile: {},
});
});
});
});
25 changes: 14 additions & 11 deletions packages/shared-ini-file-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface SharedConfigFiles {

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

export function loadSharedConfigFiles(init: SharedConfigInit = {}): Promise<SharedConfigFiles> {
export const loadSharedConfigFiles = (init: SharedConfigInit = {}): Promise<SharedConfigFiles> => {
const {
filepath = process.env[ENV_CREDENTIALS_PATH] || join(getHomeDir(), ".aws", "credentials"),
configFilepath = process.env[ENV_CONFIG_PATH] || join(getHomeDir(), ".aws", "config"),
Expand All @@ -52,10 +52,10 @@ export function loadSharedConfigFiles(init: SharedConfigInit = {}): Promise<Shar
credentialsFile,
};
});
}
};

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

return map;
}
};

function parseIni(iniData: string): ParsedIniData {
const profileNameBlockList = ["__proto__", "profile __proto__"];
const parseIni = (iniData: string): ParsedIniData => {
const map: ParsedIniData = {};
let currentSection: string | undefined;
for (let line of iniData.split(/\r?\n/)) {
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
const section = line.match(/^\s*\[([^\[\]]+)]\s*$/);
if (section) {
currentSection = section[1];
if (profileNameBlockList.includes(currentSection)) {
throw new Error(`Found invalid profile name "${currentSection}"`);
}
} else if (currentSection) {
const item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
if (item) {
Expand All @@ -91,10 +95,10 @@ function parseIni(iniData: string): ParsedIniData {
}

return map;
}
};

function slurpFile(path: string): Promise<string> {
return new Promise((resolve, reject) => {
const slurpFile = (path: string): Promise<string> =>
new Promise((resolve, reject) => {
readFile(path, "utf8", (err, data) => {
if (err) {
reject(err);
Expand All @@ -103,14 +107,13 @@ function slurpFile(path: string): Promise<string> {
}
});
});
}

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

if (HOME) return HOME;
if (USERPROFILE) return USERPROFILE;
if (HOMEPATH) return `${HOMEDRIVE}${HOMEPATH}`;

return homedir();
}
};

0 comments on commit a209082

Please sign in to comment.