PSKeePass / PoShKeePass

PowerShell module for KeePass
MIT License
256 stars 58 forks source link

Exception when trying to update an entry with Update-KeePassEntry #148

Closed Wavelandian closed 5 years ago

Wavelandian commented 6 years ago

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

jkdba commented 6 years ago

@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.

Wavelandian commented 6 years ago

Many thanks for taking a look into this issue. Today I'm starting my holidays and will be on leave for the next two weeks, therefore unable to test new updates, if any.

On my return I'll spend more time to deep look into the occurrences and the differences with the correct entry updates, to see if I can identify a pattern.

2018-08-03 16:04 GMT+02:00 John Klann notifications@github.com:

@Wavelandian https://github.com/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.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/PSKeePass/PoShKeePass/issues/148#issuecomment-410258984, or mute the thread https://github.com/notifications/unsubscribe-auth/ARq-vOevd5sQwZ3Ok42XpUnt_FawtJdnks5uNFhXgaJpZM4VtuiD .

wi-fr commented 6 years ago

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

kshire commented 6 years ago

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

ClaudioESSilva commented 6 years ago

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.

SimonBrangwin commented 5 years ago

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

kdmhorn commented 5 years ago

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.

Shaun-Harrison commented 5 years ago

Just opened another issue with the same problem

Is this still being investigated ?

Shaun-Harrison commented 5 years ago

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?

kdmhorn commented 5 years ago

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.

wi-fr commented 5 years ago

Thanks, kdmhorn, your fix solved my problem

DamionD commented 5 years ago

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?

Shaun-Harrison commented 5 years ago

@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

jkdba commented 5 years ago

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

kdmhorn commented 5 years ago

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.

jkdba commented 5 years ago

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

xtremedew commented 5 years ago

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?

jkdba commented 5 years ago

@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.

xtremedew commented 5 years ago

@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.

jkdba commented 5 years ago

@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.

Shaun-Harrison commented 5 years ago

Can confirm history is maintained with the suggested fix

Great to see you are maintaining this great library

jkdba commented 5 years ago

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