PSKeePass / PoShKeePass

PowerShell module for KeePass

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception when trying to update an entry with Update-KeePassEntry

Wavelandian opened this issue · comments

In some cases (most of cases, unable to identify a pattern), when trying to update an entry, by using this command:
Update-KeePassEntry -DatabaseProfileName $KPDBProfileName -MasterKey $KeePassDBPassword -KeePassEntry $entry -Title $title -KeePassEntryGroupPath $groupFullPath -force

I get the below exception:
image

---------------------------
Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
---------------------------
 
 
 
 
   at KeePassLib.Serialization.KdbxFile.WriteEntry(PwEntry pe, Boolean bIsHistory)
 
   at KeePassLib.Serialization.KdbxFile.WriteList(String name, PwObjectList`1 value, Boolean bIsHistory)
 
   at KeePassLib.Serialization.KdbxFile.WriteEntry(PwEntry pe, Boolean bIsHistory)
 
   at KeePassLib.Serialization.KdbxFile.<>c__DisplayClass204_0.<WriteDocument>b__1(PwEntry pe)
 
   at KeePassLib.PwGroup.PreOrderTraverseTree(GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.PwGroup.PreOrderTraverseTree(GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.PwGroup.PreOrderTraverseTree(GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.PwGroup.PreOrderTraverseTree(GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.PwGroup.PreOrderTraverseTree(GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.PwGroup.TraverseTree(TraversalMethod tm, GroupHandler groupHandler, EntryHandler entryHandler)
 
   at KeePassLib.Serializatio......
 
<truncated>
---------------------------
Abort   Retry   Ignore   
---------------------------

Details of the machine I'm using:

Machine: VUKT10000085 (Microsoft Windows NT 6.1.7601 Service Pack 1)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 7576
PSVersion: 5.1.14409.1005
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005
BuildVersion: 10.0.14409.1005
CLRVersion: 4.0.30319.34209
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1

@Wavelandian thanks for reporting.

I'll take a look, think I have seen this before on the group level should be easy to fix, will see if I can release a fix by Monday.

commented

In my case I can see a clear pattern: this exception occurs whenever I update an entry that has been updated before.

I was getting the same error this morning, but I added -Confirm:$false and no longer get the error message.

Hi @jkdba , any update on this?

In my case the problem is related with the history collection.

If I comment this line: https://github.com/PSKeePass/PoShKeePass/blob/master/internal/Set-KPEntry.ps1#L128 it saves without the error message.

Interesting that despite the error it saves the history anyway. (if I select ignore).

@wi-fr - with me is consistent. It happens every time
@kshire - for me the -Confirm:$false doesn't work, still give the error.

I'm seeing the same assertion.
As the scripts are run non-interactively, this prompt isn't seen by the user and the KeePass database which is stored on a network share was left partially written and completely corrupted.
So we had to pull the database back from a recent backup.
Could the KeePass database saving method be altered to write to a temporary file first, then replace it, in case these kind of assertions occur midway through serialization?

PSES module version: 1.10.0
PSVersion: 5.1.15063.1387
PSEdition: Desktop
PSBuildVersion: 10.0.15063.1387
CLRVersion: 4.0.30319.42000
Operating system: Windows 64-bit 10.0.15063

Think I found a fix for this.

I found that if you insert a $OldEntry.History.Clear() right before the $KeePassEntry.History.Add($OldEntry) referenced by @ClaudioESSilva the exception is no longer thrown on an update.

Just opened another issue with the same problem

Is this still being investigated ?

Think I found a fix for this.

I found that if you insert a $OldEntry.History.Clear() right before the $KeePassEntry.History.Add($OldEntry) referenced by @ClaudioESSilva the exception is no longer thrown on an update.

Hi

New to all of this so please bare with me,

If i download the files and make this change, how then do i load the module into powershell to be used, instead of the original?

Seems the project owner hasn't been here is quite some time....the way I did the update:
installed PoshKeePass, then found where the module files (%programfiles%\windowspowershell\modules\poshkeepass\2.1.1.8), make a copy just in case then open the PoshKeePass.psm1 file add the line - somewhere around line 2896, after that had no issues with updating entries.

commented

Thanks, kdmhorn, your fix solved my problem

Performing the fix above caused the following exception:
Exception calling "Open" with "3" argument(s): "The file header is corrupted. Data is missing at the end of the file, i.e. the file is incomplete."
I imagine that this is because I have installed the module from the PowerShell Gallery. Can anyone update the module in the absence of the maintainer, or are we out of luck?

@DamionD - The above fix wouldn't have caused that - running the command before the fix does, as when the error message appears, if you click abort it leaves your .kdbx file in a incomplete state

The fix stops this from happening

Hi @kdmhorn @SJHarrison1992 can anyone confirm if the history is maintained in the updated entry with the suggested fix?

I believe it does, but let me check Monday morning when I have access to my test database. What the process is doing is copying the record current state to a new object, it clears the history from that copied object then adds it to the history of the original record before committing the changes made. I'm not great with code (C++ or C#) but had found the fix by tracing what KeePASS source code does with an update.

Just tested and commited fix, released and pushed to psgallery.

This seems to still be occurring, even after updating to 2.1.2.0.
This seems to occur when an entry has a history inside it.

I could also be doing something wrong though. Any suggestions?

@xtremedew lets clarify the issue we are seeing. From my testing this is what I found:

  1. create new entry - no issue
  2. update entry - no issue
  3. update entry again - issue occurs

After testing with fix this behavior was resolved for me, Can you provide a step by step of how your issue occurs so I can reproduce it.

@jkdba - I am pretty sure I resolved this on my end. I think I was running old code somewhere. I deleted everything and started again from scratch - No issues now. Sorry for causing any panic or confusion. Thanks for the help! Keep up the good work.

@xtremedew no worries, glad to know you found it! Just tested again as sanity and still didn't have the issue. Let me know if anything comes up.

Can confirm history is maintained with the suggested fix

Great to see you are maintaining this great library

@SJHarrison1992 great! Yes not always as frequently as I would like.