mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
[FLASK] Fix crash when requesting unknown snap permission (#18447)
This commit is contained in:
parent
c52d2131d3
commit
40628da3b4
@ -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,
|
||||
};
|
||||
|
@ -5,12 +5,16 @@ 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) => {
|
||||
{getWeightedPermissions(t, permissions, targetSubjectMetadata).map(
|
||||
(permission, index) => {
|
||||
return (
|
||||
<PermissionCell
|
||||
title={permission.label}
|
||||
@ -21,11 +25,13 @@ export default function PermissionsConnectPermissionList({ permissions }) {
|
||||
key={`${permission.permissionName}-${index}`}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
},
|
||||
)}
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
PermissionsConnectPermissionList.propTypes = {
|
||||
permissions: PropTypes.object.isRequired,
|
||||
targetSubjectMetadata: PropTypes.object.isRequired,
|
||||
};
|
||||
|
@ -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,
|
||||
}),
|
||||
),
|
||||
[],
|
||||
)
|
||||
|
@ -123,6 +123,7 @@ export default function SnapInstall({
|
||||
</Text>
|
||||
<PermissionsConnectPermissionList
|
||||
permissions={requestState.permissions || {}}
|
||||
targetSubjectMetadata={targetSubjectMetadata}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
@ -135,6 +135,7 @@ export default function SnapUpdate({
|
||||
approvedPermissions={approvedPermissions}
|
||||
revokedPermissions={revokedPermissions}
|
||||
newPermissions={newPermissions}
|
||||
targetSubjectMetadata={targetSubjectMetadata}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
@ -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),
|
||||
);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user