diff --git a/frontend/src/test/outbound-link-parser.test.ts b/frontend/src/test/outbound-link-parser.test.ts index ef1fe455b..3ab512b87 100644 --- a/frontend/src/test/outbound-link-parser.test.ts +++ b/frontend/src/test/outbound-link-parser.test.ts @@ -250,6 +250,19 @@ describe('parseShadowsocksLink', () => { expect(settings.servers[0].method).toBe('aes-256-gcm'); expect(settings.servers[0].password).toBe('legacypw'); }); + + it('decodes URL-safe base64 userinfo (as the emitter writes it)', () => { + // genShadowsocksLink emits Base64.encode(method:password, true) — URL-safe, + // so a password whose encoding contains - or _ must still round-trip. + const method = 'aes-256-gcm'; + const password = '>>>'; + const userinfo = Base64.encode(`${method}:${password}`, true); + expect(userinfo).toMatch(/[-_]/); + const out = parseShadowsocksLink(`ss://${userinfo}@1.2.3.4:8388#urlsafe`); + const settings = out?.settings as { servers: Array<{ method: string; password: string }> }; + expect(settings.servers[0].method).toBe(method); + expect(settings.servers[0].password).toBe(password); + }); }); describe('parseHysteria2Link', () => { diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index c58ce781c..845c3e109 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -641,8 +641,10 @@ export class Base64 { } static decode(content: string = ''): string { + const normalized = content.replace(/-/g, '+').replace(/_/g, '/'); + const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '='); return new TextDecoder().decode( - Uint8Array.from(window.atob(content), (c) => c.charCodeAt(0)), + Uint8Array.from(window.atob(padded), (c) => c.charCodeAt(0)), ); } }