Skip to content

Commit

Permalink
fix: correctly encode and decode base64 on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Sep 18, 2023
1 parent 9bea6a0 commit cde34d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
27 changes: 19 additions & 8 deletions frontend/dashboard/src/api/registry/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ import { useMutation } from '@tanstack/vue-query';

import { protectedApiClient } from '@/api/twirp';

function fromBinary(binary: string) {
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return String.fromCharCode(...new Uint16Array(bytes.buffer));
function b64EncodeUnicode(str: string) {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function toSolidBytes(match, p1) {
return String.fromCharCode(parseInt('0x' + p1));
}),
);
}

function b64DecodeUnicode(str: string) {
return decodeURIComponent(
atob(str)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(''),
);
}

export const useOverlaysParseHtml = () => useMutation({
Expand All @@ -16,9 +27,9 @@ export const useOverlaysParseHtml = () => useMutation({
return '';
}
const req = await protectedApiClient.overlaysParseHtml({
html: btoa(htmlString),
html: b64EncodeUnicode(htmlString),
});

return fromBinary(req.response.html);
return b64DecodeUnicode(req.response.html);
},
});
32 changes: 22 additions & 10 deletions frontend/overlays/src/pages/overlaysRegistry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ export interface LayerSettings {
htmlOverlayJs: string
}

function fromBase64(binary: string) {
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
return String.fromCharCode(...new Uint16Array(bytes.buffer));
}

export const OverlaysRegistry: React.FC = () => {
const [url, setUrl] = useState<string | null>(null);
const { apiKey, overlayId } = useParams();
Expand Down Expand Up @@ -142,7 +134,7 @@ export const OverlaysRegistry: React.FC = () => {
return <Fragment key={layer.id}>
<style>
{`.layer-${layer.id} {
${fromBase64(layer.settings.htmlOverlayCss)}
${b64DecodeUnicode(layer.settings.htmlOverlayCss)}
}`}
</style>
<div
Expand All @@ -159,9 +151,29 @@ export const OverlaysRegistry: React.FC = () => {
textWrap: 'nowrap',
}}
className={'layer-' + layer.id}
dangerouslySetInnerHTML={{ __html: layer.htmlContent ? fromBase64(layer.htmlContent) : '' }}
dangerouslySetInnerHTML={{ __html: layer.htmlContent ? b64DecodeUnicode(layer.htmlContent) : '' }}
/>
</Fragment>;
})}
</div>;
};


function b64EncodeUnicode(str: string) {
return btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function toSolidBytes(match, p1) {
return String.fromCharCode(parseInt('0x' + p1));
}),
);
}

function b64DecodeUnicode(str: string) {
return decodeURIComponent(
atob(str)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(''),
);
}

0 comments on commit cde34d0

Please sign in to comment.