Skip to content

Commit

Permalink
Fix a bug if the headers array is null or undefined in exec auth.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns committed Oct 13, 2019
1 parent 9e10c20 commit 6bf2b75
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/exec_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export class ExecAuth implements Authenticator {
}
const token = this.getToken(credential);
if (token) {
if (!opts.headers) {
opts.headers = [];
}
opts.headers!.Authorization = `Bearer ${token}`;
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/exec_auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import * as shell from 'shelljs';

import execa = require('execa');
import request = require('request');
import https = require('https');

import { ExecAuth } from './exec_auth';
import { User } from './config_types';
import { fail } from 'assert';

describe('ExecAuth', () => {
it('should claim correctly', () => {
Expand Down Expand Up @@ -246,4 +248,37 @@ describe('ExecAuth', () => {
expect(optsOut.env.PATH).to.equal(process.env.PATH);
expect(optsOut.env.BLABBLE).to.equal(process.env.BLABBLE);
});

it('should handle empty headers array correctly', async () => {
const auth = new ExecAuth();
(auth as any).execFn = (
command: string,
args: string[],
opts: execa.SyncOptions,
): execa.ExecaSyncReturnValue => {
return {
code: 0,
stdout: JSON.stringify({ status: { token: 'foo' } }),
} as execa.ExecaSyncReturnValue;
};
const opts = {} as https.RequestOptions;
auth.applyAuthentication(
{
name: 'user',
authProvider: {
config: {
exec: {
command: 'echo',
},
},
},
},
opts,
);
if (!opts.headers) {
fail('unexpected null headers!');
} else {
expect(opts.headers.Authorization).to.equal('Bearer foo');
}
});
});

0 comments on commit 6bf2b75

Please sign in to comment.