Friday, August 3, 2012

How to crash the page structure in edit mode

A quite interesting bug was reported recently on one of the CMS 6 R2 websites I’ve been working on. When expanding a certain node in the page structure in edit mode, the following error message appeared:

System.UriFormatException: Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed.

imageAs annoying as that error message is, the most annoying part was that the message appeared in the frame where the page structure usually is.

This resulted in us being unable to navigate the page structure and therefore not being able to find the page responsible for the error message. I tried finding the page by clicking around in View mode, but that’s just a waste of time fumbling in the dark. I also tried adding the page ID directly to the URL, but the error message appeared on all pages in this part of the structure (and that’s a lot of pages) so it was impossible to figure out exactly which page was responsible.

This leaves debugging the database as the last option, hooray! From the error message and poking around in Resharper I found that the LinkURL property of one of the pages probably was in the wrong format (containing a ‘:’ from the looks of the message). So I did a database query in order to find all pages with LinkURLs containing a ‘:’

SELECT * FROM [dbo].[tblWorkPage] WHERE [LinkURL] like '%:%'

This resulted in 200 results, too many for my taste so I wanted to narrow down the result. As I knew this problem only occurred in a certain part of the page structure I wanted to find out how many pages existed in that part. By looking at the pageID’s I could figure out the PagePath in the database and query all pages beginning with this PagePath:

SELECT * FROM [dbo].[tblPage] WHERE [PagePath] like '.1.91927.91932.43806.%'

This resulted in 188 results, still too many. But now I knew two things, I knew all the pages in the part on the structure that was causing a problem, and I knew all the pages containing a ‘:’ in their LinkURL property. By joining these two queries, I could now find all pages in the problematic part of the structure containing a ‘:’ in their LinkURLs:

SELECT W.pkID, W.fkPageID, W.Name, W.URLSegment, W.LinkURL, W.ExternalURL, P.PagePath FROM [dbo].[tblWorkPage] AS W INNER JOIN [dbo].[tblPage] AS P ON W.fkPageID = P.pkID WHERE P.PagePath like '.1.91927.91932.43806.%' AND W.LinkURL like '%:%'

Finally, this gave me 10 results and I could go through these results manually. Looking through all the LinkURL values I found the problem:

http:www.mydomain.com

I’m stunned that one simple typo like this can take down a part of the site structure! And guess what, the only way of fixing it is directly in the database, how’s that for a friday? First of all, I updated the LinkURL value in tblWorkPage:

UPDATE [dbo].[tblWorkPage] SET [LinkURL] = 'http://www.mydomain.com' WHERE [LinkURL] like 'http:www.mydomain.com' GO

Then, as globalization was enabled on my website, I needed to do the same thing in tblPageLanguage (Thanks to Tore Gjerdrum for pointing this out for me):

UPDATE [dbo].[tblPageLanguage] SET [LinkURL] = 'http://www.mydomain.com' WHERE [LinkURL] like 'http:www.mydomain.com' GO

Would you like to try this at home?
Note: This will crash a part of your website (or the whole thing if you’re really destructive and pick the start page in step #1). I take no responsibility for any of your actions or your angry boss/customer/colleagues!

Note 2: I’ve only tried this in CMS 6 R2

1) Choose a page you don’t like on your website
2) Set a shortcut on the page to for example: http:www.google.com
3) Watch as you look forward to some SQL fun!

Update!

1) Åge Reinås just gave me a hot tip! In order to find the page responsible for the problems, you can check out the page structure under Access Rights in Admin mode. If you hover over the page names in the structure, the page responsible will show a faulty url. For example: “http://www.mywebsite.com/EPiServer/CMS/Admin/www.mydomain.com”

2) Thanks to @stianvhagen for the hillarious meme he created after I published this post:

No comments:

Post a Comment