Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ export function parse(string: string): Credentials | undefined {
if (!match) return undefined;

// decode user pass
const userPass = base64.decode(match[1]);
let userPass: string;
try {
userPass = base64.decode(match[1]);
} catch {
return undefined;
}
const colonIndex = userPass.indexOf(':');
if (colonIndex === -1) return undefined;

Expand Down
32 changes: 32 additions & 0 deletions src/parse-browser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { afterAll, assert, beforeAll, describe, it, vi } from 'vitest';

describe.skipIf(!Uint8Array.fromBase64)('parse without Buffer', () => {
let parse: typeof import('./index.js').parse;

beforeAll(async () => {
vi.stubGlobal('Buffer', undefined);
vi.resetModules();
({ parse } = await import('./index.js'));
});

afterAll(() => {
vi.unstubAllGlobals();
vi.resetModules();
});

it('should return undefined for invalid base64 characters', () => {
assert.strictEqual(parse('Basic .'), undefined);
assert.strictEqual(parse('Basic ~'), undefined);
});

it('should return undefined for incomplete base64', () => {
assert.strictEqual(parse('Basic Y'), undefined);
});

it('should parse valid credentials', () => {
assert.deepEqual(parse('Basic Zm9vOmJhcg=='), {
name: 'foo',
pass: 'bar',
});
});
});