<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>2018-02 on Funky Si's Test</title><link>https://blog-test.funkysi1701.com/2018/02/</link><description>Recent content in 2018-02 on Funky Si's Test</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><managingEditor>funkysi1701@gmail.com (Simon Foster)</managingEditor><webMaster>funkysi1701@gmail.com (Simon Foster)</webMaster><lastBuildDate>Mon, 26 Feb 2018 00:00:00 +0000</lastBuildDate><atom:link href="https://blog-test.funkysi1701.com/2018/02/index.xml" rel="self" type="application/rss+xml"/><item><title>Refactoring if statements</title><link>https://blog-test.funkysi1701.com/posts/2018/refactoringifstatements/</link><author>funkysi1701@gmail.com (funkysi1701)</author><pubDate>Mon, 26 Feb 2018 00:00:00 +0000</pubDate><guid>https://blog-test.funkysi1701.com/posts/2018/refactoringifstatements/</guid><category term="Design">Design</category><category term="Class">Class</category><category term="Interface">Interface</category><description>&lt;p&gt;The code base I am working on contains a huge if block. By huge I mean 77 if statements one after the other, each if checks to see what page id you are on and loads different content. This is not easy to maintain and I want to refactor it.&lt;/p&gt;
&lt;p&gt;One option would be to replace the if statements with a switch block. However this is just as unmanageable as the huge if block. Lets look at a better option.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Polymorphism&lt;/strong&gt; is where you create a base class and then create sub classes from it. In my case I created an interface IPage with a single method CreateContent.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;public&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;interface&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;IPage&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; CreatePageContent();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and then create 77 classes for each page which implemented this single method.&lt;/p&gt;
&lt;p&gt;Now comes the fun bit how do I call the correct page class from my original code?&lt;/p&gt;
&lt;p&gt;I created a dictionary than maps page ids to the class names.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;public&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;static&lt;/span&gt; Dictionary&amp;lt;PageIds, Type&amp;gt; PageIdToClass = &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; Dictionary&amp;lt;PageIds, Type&amp;gt;()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; PageIds.HomePage,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;typeof&lt;/span&gt;(HomePage)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; PageIds.ContactPage,
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;typeof&lt;/span&gt;(ContactPage)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; },
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#75715e"&gt;//etc &lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the one step I am not 100% happy with as I think it may be possible to remove or simplify this step.&lt;/p&gt;
&lt;p&gt;Now I have a way to map ids to classes I can write a class to do this.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;public&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;MyPage&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; IPage _repo;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;public&lt;/span&gt; MyPage(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; pageId)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; PageIds p = (PageIds)pageId;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Type t = PageIdToClass[p];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; ConstructorInfo constructor = t.GetConstructor(&lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; Type[] { });
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; _repo = (IPage)constructor.Invoke(&lt;span style="color:#66d9ef"&gt;null&lt;/span&gt;);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;public&lt;/span&gt; &lt;span style="color:#66d9ef"&gt;string&lt;/span&gt; Create()
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; _repo.CreatePageContent();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So in my constructor I take the pageId and pass it to my dictionary to get which subclass to load. I then get its Constructor and invoke it.&lt;/p&gt;
&lt;p&gt;Now I can remove the huge if block and replace it with a single line of code.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-csharp" data-lang="csharp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;var&lt;/span&gt; page = &lt;span style="color:#66d9ef"&gt;new&lt;/span&gt; MyPage(pageId);
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;On the face of it this change might look like a lot of work for not much gain as we started off with one file and now we have the original file, an interface, 77 subclasses and the MyPage class. However the original file is a lot more manageable and each sub class can be altered independently of each other.&lt;/p&gt;
&lt;p&gt;This is a big step towards making this code more maintainable, there is always more that can be done but that can wait for another day.&lt;/p&gt;</description></item><item><title>Chrome distrusts SSL Certificates</title><link>https://blog-test.funkysi1701.com/posts/2018/ssl-distrusts/</link><author>funkysi1701@gmail.com (funkysi1701)</author><pubDate>Mon, 19 Feb 2018 00:00:00 +0000</pubDate><guid>https://blog-test.funkysi1701.com/posts/2018/ssl-distrusts/</guid><category term="Certificates">Certificates</category><category term="SSL">SSL</category><category term="Security">Security</category><category term="DevOps">DevOps</category><description>&lt;p&gt;One of the websites I have been working on has been displaying an error in the console. The error reads as follows.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-txt" data-lang="txt"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;The SSL certificate used to load resources from https://example.com will be distrusted in M70. Once distrusted, users will be prevented from loading these resources. See https://g.co/chrome/symantecpkicerts for more information.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But what does this mean? Well let’s start by looking at the &lt;a href="https://security.googleblog.com/2017/09/chromes-plan-to-distrust-symantec.html" target="_blank" rel="noopener noreferrer"&gt;link&lt;/a&gt;
provided.&lt;/p&gt;
&lt;p&gt;In January 2017 it was revealed that Certificate Authorities run by Symantec which include Thawte, VeriSign, Equifax, GeoTrust, and RapidSSL had been issuing certificates that did not comply with baseline standards.&lt;/p&gt;
&lt;p&gt;Starting with Chrome 66, Google has decided to remove trust for these certificates. Chrome 66 is due for release around 17th April. My error mentions M70 so what does that refer to?&lt;/p&gt;
&lt;p&gt;Chrome 70 which is due to be released in October 2018 will removed the trust for another batch of Symantec certificates.&lt;/p&gt;
&lt;p&gt;If you are getting one of these errors because you are using a certificate that is going to be distrusted what will your site look like in Chrome 66 or Chrome 70?&lt;/p&gt;
&lt;p&gt;Well Chrome 66 is now in the dev channel so we can give it a try.
&lt;img class="img-fluid" alt="Chrome browser warning that a Symantec SSL certificate is distrusted" src="https://blog-test.funkysi1701.com/images/2018/tempsnip.png.jpg" loading="lazy"
width="1673" height="1119"
/&gt;
&lt;/p&gt;
&lt;p&gt;Not very nice for your users is it? Now is the time to order a new SSL certificate to avoid this happening to your site.&lt;/p&gt;
&lt;p&gt;I first saw this error a few months ago and have been reading up about it and waiting for Chrome 66 to reach the dev channel so I could test what it did to my site. However now that I have Chrome 66 installed I spotted the intranet for the company I work for is also affected. I do not directly work on the intranet so I notified the security team that they may want to look into this.&lt;/p&gt;
&lt;p&gt;Unfortunately the response I received has been that Google needs to fix this before Chrome 66 is released. I am not criticising my employer or the security team, however this isn’t something Google can just “ &lt;strong&gt;fix&lt;/strong&gt; “.&lt;/p&gt;
&lt;p&gt;The certificates issued were issued by a CA that had issues so in order to maintain the trustworthiness of all certificates Google had little choice but to distrust them. Google and security experts need to be making more of a fuss about this and I am joining in on making a fuss by writing this blog. &lt;a href="https://scotthelme.co.uk/are-you-ready-for-the-symantec-distrust/" target="_blank" rel="noopener noreferrer"&gt;Scott Helme&lt;/a&gt;
estimates that there are about 7000 websites which may be affected by the M66 and M70 distrusts.&lt;/p&gt;</description></item><item><title>Content Security Policies</title><link>https://blog-test.funkysi1701.com/posts/2018/content-security-policies/</link><author>funkysi1701@gmail.com (funkysi1701)</author><pubDate>Mon, 12 Feb 2018 00:00:00 +0000</pubDate><guid>https://blog-test.funkysi1701.com/posts/2018/content-security-policies/</guid><category term="CSP">CSP</category><category term="ReportURI">ReportURI</category><category term="Security">Security</category><description>&lt;p&gt;A content Security Policy or CSP is a HTTP response header that defines what sources of content can be loaded on a web page. It is a way to combat Cross Site Scripting (XSS) attacks.&lt;/p&gt;
&lt;h2 id="what-is-a-xss-attack-then"&gt;What is a XSS attack then?&lt;a class="anchor ms-1" href="#what-is-a-xss-attack-then" aria-label="Permalink: What is a XSS attack then?"&gt;&lt;i class="fas fa-link" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When you load a webpage it also loads various other resources like images, some css style sheets, various javascript files that you want to run and probably many other things.&lt;/p&gt;
&lt;p&gt;How do you know that you can trust all of these things? If you created them and they live under you control then the answer is probably yes. However these days you will probably want to use resources from across the internet, like youtube videos, google analytics, disqus comments, jquery libraries from a cdn etc and you can’t be sure exactly what they are doing.&lt;/p&gt;
&lt;p&gt;Imagine you had a page which you could add any text into a form which would then be displayed. A malicious user could add evil javascript or get the browser to load evil code from anywhere on the internet.&lt;/p&gt;
&lt;h2 id="csp-to-the-rescue"&gt;CSP to the rescue!&lt;a class="anchor ms-1" href="#csp-to-the-rescue" aria-label="Permalink: CSP to the rescue!"&gt;&lt;i class="fas fa-link" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A CSP allows the browser to only load from sources that you specify. You could specify that resources from your own site will load but the evil script will not.&lt;/p&gt;
&lt;p&gt;Let’s look at some examples&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Content-Security-Policy: script-src &amp;#39;self&amp;#39;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags to only load from the current webhost. script-src is not the only keyword you can use, let’s look at some of the others.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;script-src&lt;/strong&gt; – control what &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tags will load&lt;br&gt;
&lt;strong&gt;style-src&lt;/strong&gt; – control what css will load&lt;br&gt;
&lt;strong&gt;img-src&lt;/strong&gt; – control what images will load&lt;br&gt;
&lt;strong&gt;frame-src&lt;/strong&gt; – control what frames will load&lt;br&gt;
&lt;strong&gt;font-src&lt;/strong&gt; – control what fonts will load&lt;br&gt;
&lt;strong&gt;object-src&lt;/strong&gt; – control what object tags will load&lt;br&gt;
&lt;strong&gt;connect-src&lt;/strong&gt; – control what resources a script can connect to&lt;br&gt;
&lt;strong&gt;media-src&lt;/strong&gt; – controls what media (audio/video) will load&lt;br&gt;
&lt;strong&gt;default-src&lt;/strong&gt; – if no specific rule exists then the default directive will run&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Content-Security-Policy: default-src https
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows any content to be loaded from any site as long as it comes from a secure (https) site&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Content-Security-Policy: default-src https://example.com
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This allows any content to be loaded from &lt;a href="https://example.com" target="_blank" rel="noopener noreferrer"&gt;https://example.com&lt;/a&gt;
only.&lt;/p&gt;
&lt;h2 id="how-do-i-use-this-on-my-site"&gt;How do I use this on my site?&lt;a class="anchor ms-1" href="#how-do-i-use-this-on-my-site" aria-label="Permalink: How do I use this on my site?"&gt;&lt;i class="fas fa-link" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I have added CSPs into my web.config which works great for my .Net Framework code.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt; &amp;lt;system.webServer&amp;gt; &amp;lt;httpProtocol&amp;gt; &amp;lt;customHeaders&amp;gt; &amp;lt;add name=&amp;#34;Content-Security-Policy&amp;#34; value=&amp;#34;default-src https://example.com&amp;#34; /&amp;gt; &amp;lt;/customHeaders&amp;gt; &amp;lt;/httpProtocol&amp;gt; &amp;lt;/system.webServer&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For .net core it is a bit more complex as you don’t tend to use web.config files, however check out Anthony Chu’s &lt;a href="https://anthonychu.ca/post/aspnet-core-csp/" target="_blank" rel="noopener noreferrer"&gt;post&lt;/a&gt;
, which has a solution to that problem.&lt;/p&gt;
&lt;h2 id="report-only"&gt;Report Only&lt;a class="anchor ms-1" href="#report-only" aria-label="Permalink: Report Only"&gt;&lt;i class="fas fa-link" aria-hidden="true"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One last thing about CSPs to mention is the Report Only flag.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Content-Security-Policy-Report-Only
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This does the same as the above but doesn’t enforce anything, so you can fix any problems before you break anything.&lt;/p&gt;
&lt;p&gt;To view your issues just look in the developer tools in your favourite browser. Or you can configure all your reports to be collated in one place with a report-uri directive.&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Content-Security-Policy: default-src https://example.com; report-uri https://example.report-uri.com/r/d/csp/reportOnly;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Scott Helme and Troy Hunt have a site called &lt;a href="https://report-uri.com/" target="_blank" rel="noopener noreferrer"&gt;report-uri&lt;/a&gt;
which offer a service for collating and viewing all your CSP violations so check it out if you want to know more about CSPs.&lt;/p&gt;</description></item><item><title>Pluralsight</title><link>https://blog-test.funkysi1701.com/posts/2018/pluralsight/</link><author>funkysi1701@gmail.com (funkysi1701)</author><pubDate>Mon, 05 Feb 2018 00:00:00 +0000</pubDate><guid>https://blog-test.funkysi1701.com/posts/2018/pluralsight/</guid><category term="Learning">Learning</category><category term="Training">Training</category><category term="Pluralsight">Pluralsight</category><category term="C-Sharp">C-Sharp</category><description>&lt;p&gt;Earlier this year I signed up for pluralsight. If you want to sign up to pluralsight as well use this link &lt;a href="https://referral.pluralsight.com/mQd8BJ4" target="_blank" rel="noopener noreferrer"&gt;https://referral.pluralsight.com/mQd8BJ4&lt;/a&gt;
to get money off.&lt;/p&gt;
&lt;p&gt;Pluralsight is a website that sells training videos on a wide variety of technical topics. You sign up for a monthly or annual subscription and you can watch over 6000 courses whenever and where ever you like.&lt;/p&gt;
&lt;p&gt;I am not a big fan of videos as if I am sat in front of a screen I would rather be building something, however I do spend a lot of time listening to podcasts. So I have decided to convert this time to listening to pluralsight videos.&lt;/p&gt;
&lt;p&gt;If we assume that by not seeing the visual portion of the videos you lose out on 75% of the learning (This is just a guesstimate I am not sure what the exact figure is or how you would calculate it). However this is still 25% more learning than if I hadn’t signed up at all.&lt;/p&gt;
&lt;p&gt;Since I signed up I have listened to over 27 hours of technical videos. These have covered topics like C#, Security, Xamarin, MVVM, Interfaces, Getting Involved and Clean architecture. The number one thing I have learnt is that there is a lot that I still don’t understand.&lt;/p&gt;
&lt;p&gt;That said I would like to think that since starting to listen to pluralsight concepts have gone from being unknown unknowns (I don’t know about them and I don’t understand them) to known unknowns (I know about them but don’t yet fully understand them)&lt;/p&gt;
&lt;p&gt;To keep track of your learning pluralsight has some tests which you can complete. I am particularly proud of my score on the Azure test which ranks me as an expert.&lt;/p&gt;
&lt;p&gt;Why not check out what you can learn from pluralsight?&lt;/p&gt;</description></item></channel></rss>