MCPcopy Create free account
hub / github.com/bitwarden/clients / editCipher

Method editCipher

apps/cli/src/commands/edit.command.ts:106–208  ·  view source on GitHub ↗
(id: string, req: CipherExport)

Source from the content-addressed store, hash-verified

104 }
105
106 private async editCipher(id: string, req: CipherExport) {
107 const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId));
108 const cipher = await this.cipherService.get(id, activeUserId);
109 const hasPremium = await firstValueFrom(
110 this.billingAccountProfileStateService.hasPremiumFromAnySource$(activeUserId),
111 );
112
113 if (cipher == null) {
114 return Response.notFound();
115 }
116
117 const canEditCipher = await firstValueFrom(
118 this.cipherAuthorizationService.canEditCipher$(cipher),
119 );
120 if (!canEditCipher) {
121 return Response.noEditPermission();
122 }
123
124 let cipherView = await this.cipherService.decrypt(cipher, activeUserId);
125 if (cipherView.isDeleted) {
126 return Response.badRequest("You may not edit a deleted item. Use the restore command first.");
127 }
128
129 // Users without permission to view passwords never receive the real login password/TOTP or
130 // hidden field values from `bw get`/`bw list` (they're redacted to `null`, matching how the
131 // web/browser/desktop clients disable those fields for editing). If those redacted `null`s
132 // are round-tripped back through `bw edit`, preserve the real, already-decrypted values
133 // instead of letting them be overwritten and lost. A non-null value is a deliberate write
134 // and is left alone. See PM-16160/PM-33526.
135 const canViewPassword = cipherView.viewPassword;
136 const originalLoginPassword = cipherView.login?.password;
137 const originalLoginTotp = cipherView.login?.totp;
138 // Values are keyed by field name rather than position so that adding, removing, or
139 // reordering fields in the request can't shift a value onto the wrong field. Fields
140 // sharing a name keep their original relative order.
141 const originalHiddenFieldValues = new Map<string, string[]>();
142 for (const field of cipherView.fields ?? []) {
143 if (field.type === FieldType.Hidden) {
144 const values = originalHiddenFieldValues.get(field.name) ?? [];
145 values.push(field.value);
146 originalHiddenFieldValues.set(field.name, values);
147 }
148 }
149
150 cipherView = CipherExport.toView(req, cipherView);
151
152 if (!canViewPassword) {
153 if (cipherView.login != null) {
154 if (cipherView.login.password == null) {
155 cipherView.login.password = originalLoginPassword;
156 }
157 if (cipherView.login.totp == null) {
158 cipherView.login.totp = originalLoginTotp;
159 }
160 }
161 cipherView.fields?.forEach((field) => {
162 if (field.type !== FieldType.Hidden || field.value != null) {
163 return;

Callers 1

runMethod · 0.95

Calls 15

promptForArchiveEditMethod · 0.95
notFoundMethod · 0.80
canEditCipher$Method · 0.80
noEditPermissionMethod · 0.80
badRequestMethod · 0.80
policyAppliesToUser$Method · 0.80
successMethod · 0.80
errorMethod · 0.65
getMethod · 0.45
decryptMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected