1
0
Fork 0
mirror of synced 2026-06-05 11:08:19 +00:00

feat: add github_action to ua string

This commit is contained in:
Tom Keller 2026-05-12 16:04:31 -07:00
commit 15f02495fa
3 changed files with 42 additions and 42 deletions

26
dist/index.js generated vendored
View file

@ -71011,24 +71011,20 @@ var MAX_TAG_VALUE_LENGTH = 256;
var SANITIZATION_CHARACTER = "_";
var SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
var USER_AGENT_PREFIX = "configure-aws-credentials-for-github-actions";
var RUN_ID_PATTERN = /^[0-9]{1,20}$/;
var ATTEMPT_PATTERN = /^[0-9]{1,10}$/;
var UA_FIELDS = [
{ env: "GITHUB_ACTION", label: "action", pattern: /^[A-Za-z0-9_-]{1,128}$/ },
{ env: "GITHUB_RUN_ID", label: "run_id", pattern: /^[0-9]{1,20}$/ },
{ env: "GITHUB_RUN_ATTEMPT", label: "attempt", pattern: /^[0-9]{1,10}$/ }
];
function buildCustomUserAgent() {
const tokens = [[USER_AGENT_PREFIX]];
const runId = process.env.GITHUB_RUN_ID;
const attempt = process.env.GITHUB_RUN_ATTEMPT;
if (runId !== void 0) {
if (RUN_ID_PATTERN.test(runId)) {
tokens.push(["md", `run_id#${runId}`]);
for (const { env, label, pattern } of UA_FIELDS) {
const value = process.env[env];
if (value === void 0) continue;
if (pattern.test(value)) {
tokens.push(["md", `${label}#${value}`]);
} else {
warning("GITHUB_RUN_ID has unexpected format; omitting from User-Agent");
}
}
if (attempt !== void 0) {
if (ATTEMPT_PATTERN.test(attempt)) {
tokens.push(["md", `attempt#${attempt}`]);
} else {
warning("GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent");
warning(`${env} has unexpected format; omitting from User-Agent`);
}
}
return tokens;

View file

@ -8,25 +8,21 @@ const MAX_TAG_VALUE_LENGTH = 256;
const SANITIZATION_CHARACTER = '_';
const SPECIAL_CHARS_REGEX = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]+/;
const USER_AGENT_PREFIX = 'configure-aws-credentials-for-github-actions';
const RUN_ID_PATTERN = /^[0-9]{1,20}$/;
const ATTEMPT_PATTERN = /^[0-9]{1,10}$/;
const UA_FIELDS: ReadonlyArray<{ env: string; label: string; pattern: RegExp }> = [
{ env: 'GITHUB_ACTION', label: 'action', pattern: /^[A-Za-z0-9_-]{1,128}$/ },
{ env: 'GITHUB_RUN_ID', label: 'run_id', pattern: /^[0-9]{1,20}$/ },
{ env: 'GITHUB_RUN_ATTEMPT', label: 'attempt', pattern: /^[0-9]{1,10}$/ },
];
export function buildCustomUserAgent(): UserAgent {
const tokens: UserAgent = [[USER_AGENT_PREFIX]];
const runId = process.env.GITHUB_RUN_ID;
const attempt = process.env.GITHUB_RUN_ATTEMPT;
if (runId !== undefined) {
if (RUN_ID_PATTERN.test(runId)) {
tokens.push(['md', `run_id#${runId}`]);
for (const { env, label, pattern } of UA_FIELDS) {
const value = process.env[env];
if (value === undefined) continue;
if (pattern.test(value)) {
tokens.push(['md', `${label}#${value}`]);
} else {
core.warning('GITHUB_RUN_ID has unexpected format; omitting from User-Agent');
}
}
if (attempt !== undefined) {
if (ATTEMPT_PATTERN.test(attempt)) {
tokens.push(['md', `attempt#${attempt}`]);
} else {
core.warning('GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent');
core.warning(`${env} has unexpected format; omitting from User-Agent`);
}
}
return tokens;

View file

@ -1218,9 +1218,7 @@ describe('Configure AWS Credentials', {}, () => {
}),
);
// biome-ignore lint/suspicious/noExplicitAny: any required to mock private method
vi.spyOn(CredentialsClient.prototype as any, 'loadCredentials').mockRejectedValue(
new Error('network glitch'),
);
vi.spyOn(CredentialsClient.prototype as any, 'loadCredentials').mockRejectedValue(new Error('network glitch'));
await run();
expect(core.setFailed).toHaveBeenCalled();
expect(core.info).not.toHaveBeenCalledWith(expect.stringContaining('Retry'));
@ -1267,13 +1265,15 @@ describe('Configure AWS Credentials', {}, () => {
return (client.stsClient.config as any).customUserAgent;
}
it('includes run_id and attempt tokens when env vars are valid', async () => {
it('includes action, run_id and attempt tokens when env vars are valid', async () => {
vi.resetModules();
process.env.GITHUB_ACTION = '__run_2';
process.env.GITHUB_RUN_ID = '16412345678';
process.env.GITHUB_RUN_ATTEMPT = '1';
const ua = await getCustomUserAgent();
expect(ua).toEqual([
['configure-aws-credentials-for-github-actions'],
['md', 'action#__run_2'],
['md', 'run_id#16412345678'],
['md', 'attempt#1'],
]);
@ -1282,6 +1282,7 @@ describe('Configure AWS Credentials', {}, () => {
it('omits tokens when env vars are unset, with no warning', async () => {
vi.resetModules();
delete process.env.GITHUB_ACTION;
const ua = await getCustomUserAgent();
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
expect(core.warning).not.toHaveBeenCalled();
@ -1289,26 +1290,33 @@ describe('Configure AWS Credentials', {}, () => {
it('warns and skips when env vars are malformed', async () => {
vi.resetModules();
process.env.GITHUB_ACTION = '$(curl evil)';
process.env.GITHUB_RUN_ID = '$(curl evil)';
process.env.GITHUB_RUN_ATTEMPT = '1; rm -rf /';
const ua = await getCustomUserAgent();
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
expect(core.warning).toHaveBeenCalledWith(
'GITHUB_RUN_ID has unexpected format; omitting from User-Agent',
);
expect(core.warning).toHaveBeenCalledWith(
'GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent',
);
expect(core.warning).toHaveBeenCalledTimes(2);
expect(core.warning).toHaveBeenCalledWith('GITHUB_ACTION has unexpected format; omitting from User-Agent');
expect(core.warning).toHaveBeenCalledWith('GITHUB_RUN_ID has unexpected format; omitting from User-Agent');
expect(core.warning).toHaveBeenCalledWith('GITHUB_RUN_ATTEMPT has unexpected format; omitting from User-Agent');
expect(core.warning).toHaveBeenCalledTimes(3);
});
it('warns and skips when env vars exceed the length bound', async () => {
vi.resetModules();
process.env.GITHUB_ACTION = 'a'.repeat(200);
process.env.GITHUB_RUN_ID = '1'.repeat(50);
process.env.GITHUB_RUN_ATTEMPT = '1'.repeat(50);
const ua = await getCustomUserAgent();
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
expect(core.warning).toHaveBeenCalledTimes(2);
expect(core.warning).toHaveBeenCalledTimes(3);
});
it('rejects GITHUB_ACTION containing whitespace or other characters', async () => {
vi.resetModules();
process.env.GITHUB_ACTION = 'has space';
const ua = await getCustomUserAgent();
expect(ua).toEqual([['configure-aws-credentials-for-github-actions']]);
expect(core.warning).toHaveBeenCalledWith('GITHUB_ACTION has unexpected format; omitting from User-Agent');
});
it('sets AWS_EXECUTION_ENV to GitHubActions when unset', async () => {