Validating $INSTDIR before uninstall: Difference between revisions
No edit summary |
|||
Line 17: | Line 17: | ||
StrCmp $INSTDIR "" 0 +2 | StrCmp $INSTDIR "" 0 +2 | ||
StrCpy $INSTDIR $EXEDIR | StrCpy $INSTDIR $EXEDIR | ||
# Check that the uninstall isn't dangerous. | |||
!insertmacro BadPathsCheck | |||
# Does path end with "\MyApp"? Change -6 accordingly. | # Does path end with "\MyApp"? Change -6 accordingly. | ||
StrCpy $R0 $INSTDIR "" -6 | StrCpy $R0 $INSTDIR "" -6 | ||
StrCmp $R0 "\MyApp" +3 | StrCmp $R0 "\MyApp" +3 | ||
MessageBox MB_YESNO|MB_ICONQUESTION ". | MessageBox MB_YESNO|MB_ICONQUESTION "Unrecognised uninstall path. Continue anyway?" IDYES +2 | ||
Abort | Abort | ||
Line 28: | Line 31: | ||
MessageBox MB_OK|MB_ICONSTOP "Install path invalid!" | MessageBox MB_OK|MB_ICONSTOP "Install path invalid!" | ||
Abort | Abort | ||
</highlight-nsis> | </highlight-nsis> | ||
Line 38: | Line 39: | ||
Another check at the end is for MyApp.exe. If that file does not exist then the uninstall path could possibly be bad. | Another check at the end is for MyApp.exe. If that file does not exist then the uninstall path could possibly be bad. | ||
== Bad paths detection == | |||
<highlight-nsis> | |||
!macro BadPathsCheck | |||
StrCpy $R0 $INSTDIR "" -2 | |||
StrCmp $R0 ":\" bad | |||
StrCpy $R0 $INSTDIR "" -14 | |||
StrCmp $R0 "\Program Files" bad | |||
StrCpy $R0 $INSTDIR "" -8 | |||
StrCmp $R0 "\Windows" bad | |||
StrCpy $R0 $INSTDIR "" -6 | |||
StrCmp $R0 "\WinNT" bad | |||
StrCpy $R0 $INSTDIR "" -9 | |||
StrCmp $R0 "\system32" bad | |||
StrCpy $R0 $INSTDIR "" -8 | |||
StrCmp $R0 "\Desktop" bad | |||
StrCpy $R0 $INSTDIR "" -22 | |||
StrCmp $R0 "\Documents and Settings" bad | |||
StrCpy $R0 $INSTDIR "" -13 | |||
StrCmp $R0 "\My Documents" bad done | |||
bad: | |||
MessageBox MB_OK|MB_ICONSTOP "Install path invalid!" | |||
Abort | |||
done: | |||
!macroend | |||
</highlight-nsis> | |||
[[Category:Code_Examples]] | [[Category:Code_Examples]] | ||
Stu | Stu |
Revision as of 22:36, 10 July 2007
Author: Afrow UK (talk, contrib) |
Description
The install directory ($INSTDIR) is saved in the registry after a successful install and then fetched again in the uninstaller. If not fetched from the registry manually (via a ReadRegStr) then it is set to $EXEDIR (the location of the uninstaller executable). This can be problematic, especially if a lazy NSIS script writer uses RMDir /r "$INSTDIR".
What if $INSTDIR is empty because the registry key is missing? "$INSTDIR" will become "" which means that everything from the system root could be deleted INCLUDING THE OPERATING SYSTEM. This would also happen if the user ran the uninstaller from C:\ and if that uninstaller did not set $INSTDIR to anything. Also, what if the user has installed to his desktop, or perhaps to My Documents. Need I say more?
So, what can we do about this? There is only so much validation that we can do on $INSTDIR, but some is better than none at all!
The Code
ClearErrors ReadRegStr $INSTDIR HKLM "Software\MyApp" "" IfErrors +2 StrCmp $INSTDIR "" 0 +2 StrCpy $INSTDIR $EXEDIR # Check that the uninstall isn't dangerous. !insertmacro BadPathsCheck # Does path end with "\MyApp"? Change -6 accordingly. StrCpy $R0 $INSTDIR "" -6 StrCmp $R0 "\MyApp" +3 MessageBox MB_YESNO|MB_ICONQUESTION "Unrecognised uninstall path. Continue anyway?" IDYES +2 Abort IfFileExists "$INSTDIR\*.*" 0 +2 IfFileExists "$INSTDIR\MyApp.exe" +3 MessageBox MB_OK|MB_ICONSTOP "Install path invalid!" Abort
Reflection
There are some bits to change here, such as the part that checks if the path ends with "\MyApp". Obviously, if it does not, then your user may be uninstalling from somewhere else and in which case it will prompt the user with a Yes/No message box.
You may or may not want to read $INSTDIR from the registry. If not then $INSTDIR will be set to $EXEDIR and in which case you can remove the first 3 lines of the script.
Another check at the end is for MyApp.exe. If that file does not exist then the uninstall path could possibly be bad.
Bad paths detection
!macro BadPathsCheck StrCpy $R0 $INSTDIR "" -2 StrCmp $R0 ":\" bad StrCpy $R0 $INSTDIR "" -14 StrCmp $R0 "\Program Files" bad StrCpy $R0 $INSTDIR "" -8 StrCmp $R0 "\Windows" bad StrCpy $R0 $INSTDIR "" -6 StrCmp $R0 "\WinNT" bad StrCpy $R0 $INSTDIR "" -9 StrCmp $R0 "\system32" bad StrCpy $R0 $INSTDIR "" -8 StrCmp $R0 "\Desktop" bad StrCpy $R0 $INSTDIR "" -22 StrCmp $R0 "\Documents and Settings" bad StrCpy $R0 $INSTDIR "" -13 StrCmp $R0 "\My Documents" bad done bad: MessageBox MB_OK|MB_ICONSTOP "Install path invalid!" Abort done: !macroend
Stu