1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

[FLASK] Fix crash when requesting unknown snap permission (#18447)

This commit is contained in:
Frederik Bolding 2023-04-05 15:34:33 +02:00 committed by GitHub
parent c52d2131d3
commit 40628da3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 68 additions and 48 deletions

View File

@ -9,24 +9,13 @@ export default function UpdateSnapPermissionList({
approvedPermissions,
revokedPermissions,
newPermissions,
targetSubjectMetadata,
}) {
const t = useI18nContext();
return (
<Box paddingTop={1}>
{getWeightedPermissions(t, newPermissions).map((permission, index) => {
return (
<PermissionCell
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
/>
);
})}
{getWeightedPermissions(t, approvedPermissions).map(
{getWeightedPermissions(t, newPermissions, targetSubjectMetadata).map(
(permission, index) => {
return (
<PermissionCell
@ -40,7 +29,23 @@ export default function UpdateSnapPermissionList({
);
},
)}
{getWeightedPermissions(t, revokedPermissions).map(
{getWeightedPermissions(
t,
approvedPermissions,
targetSubjectMetadata,
).map((permission, index) => {
return (
<PermissionCell
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
/>
);
})}
{getWeightedPermissions(t, revokedPermissions, targetSubjectMetadata).map(
(permission, index) => {
return (
<PermissionCell
@ -72,4 +77,5 @@ UpdateSnapPermissionList.propTypes = {
* New permissions that are being requested
*/
newPermissions: PropTypes.object.isRequired,
targetSubjectMetadata: PropTypes.object.isRequired,
};

View File

@ -5,27 +5,33 @@ import { useI18nContext } from '../../../hooks/useI18nContext';
import PermissionCell from '../permission-cell';
import Box from '../../ui/box';
export default function PermissionsConnectPermissionList({ permissions }) {
export default function PermissionsConnectPermissionList({
permissions,
targetSubjectMetadata,
}) {
const t = useI18nContext();
return (
<Box paddingTop={2} paddingBottom={2}>
{getWeightedPermissions(t, permissions).map((permission, index) => {
return (
<PermissionCell
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
/>
);
})}
{getWeightedPermissions(t, permissions, targetSubjectMetadata).map(
(permission, index) => {
return (
<PermissionCell
title={permission.label}
description={permission.description}
weight={permission.weight}
avatarIcon={permission.leftIcon}
dateApproved={permission?.permissionValue?.date}
key={`${permission.permissionName}-${index}`}
/>
);
},
)}
</Box>
);
}
PermissionsConnectPermissionList.propTypes = {
permissions: PropTypes.object.isRequired,
targetSubjectMetadata: PropTypes.object.isRequired,
};

View File

@ -409,6 +409,7 @@ export const getPermissionDescription = ({
t,
permissionName,
permissionValue,
targetSubjectMetadata,
}) => {
let value = PERMISSION_DESCRIPTIONS[UNKNOWN_PERMISSION];
@ -416,7 +417,12 @@ export const getPermissionDescription = ({
value = PERMISSION_DESCRIPTIONS[permissionName];
}
const result = value({ t, permissionName, permissionValue });
const result = value({
t,
permissionName,
permissionValue,
targetSubjectMetadata,
});
if (!Array.isArray(result)) {
return [{ ...result, permissionName, permissionValue }];
}
@ -434,14 +440,20 @@ export const getPermissionDescription = ({
*
* @param {Function} t - The translation function
* @param {object} permissions - The permissions object.
* @param {object} targetSubjectMetadata - The subject metadata.
* @returns {PermissionLabelObject[]}
*/
export function getWeightedPermissions(t, permissions) {
export function getWeightedPermissions(t, permissions, targetSubjectMetadata) {
return Object.entries(permissions)
.reduce(
(target, [permissionName, permissionValue]) =>
target.concat(
getPermissionDescription({ t, permissionName, permissionValue }),
getPermissionDescription({
t,
permissionName,
permissionValue,
targetSubjectMetadata,
}),
),
[],
)

View File

@ -123,6 +123,7 @@ export default function SnapInstall({
</Text>
<PermissionsConnectPermissionList
permissions={requestState.permissions || {}}
targetSubjectMetadata={targetSubjectMetadata}
/>
</>
)}

View File

@ -135,6 +135,7 @@ export default function SnapUpdate({
approvedPermissions={approvedPermissions}
revokedPermissions={revokedPermissions}
newPermissions={newPermissions}
targetSubjectMetadata={targetSubjectMetadata}
/>
</>
)}

View File

@ -1,29 +1,18 @@
import { isObject } from '@metamask/utils';
import { PERMISSION_DESCRIPTIONS } from '../../../helpers/utils/permission';
import { getPermissionDescription } from '../../../helpers/utils/permission';
export function getSnapInstallWarnings(permissions, targetSubjectMetadata, t) {
const weightOneWarnings = Object.entries(permissions).reduce(
(filteredWarnings, [permissionName, permissionValue]) => {
const permissionDescription = PERMISSION_DESCRIPTIONS[permissionName]({
const permissionDescription = getPermissionDescription({
t,
permissionName,
permissionValue,
targetSubjectMetadata,
});
if (Array.isArray(permissionDescription)) {
permissionDescription.forEach((description) => {
if (description.weight === 1) {
const { id, message } = description;
filteredWarnings.push({ id, message });
}
});
} else if (
isObject(permissionDescription) &&
permissionDescription.weight === 1
) {
const { id, message } = permissionDescription;
filteredWarnings.push({ id, message });
}
return filteredWarnings;
return filteredWarnings.concat(
permissionDescription.filter((description) => description.weight === 1),
);
},
[],
);

View File

@ -35,6 +35,7 @@ import {
getSubjectsWithSnapPermission,
getPermissions,
getPermissionSubjects,
getTargetSubjectMetadata,
} from '../../../../selectors';
import { formatDate } from '../../../../helpers/utils/util';
@ -65,6 +66,9 @@ function ViewSnap() {
(state) => snap && getPermissions(state, snap.id),
);
const subjects = useSelector((state) => getPermissionSubjects(state));
const targetSubjectMetadata = useSelector((state) =>
getTargetSubjectMetadata(state, snap?.id),
);
const dispatch = useDispatch();
const onToggle = () => {
@ -180,6 +184,7 @@ function ViewSnap() {
<Box width={BLOCK_SIZES.FULL}>
<PermissionsConnectPermissionList
permissions={permissions ?? {}}
targetSubjectMetadata={targetSubjectMetadata}
/>
</Box>
</div>