<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>VN&#x27;s Blog</title>
    <link rel="self" type="application/atom+xml" href="https://blog.vnaik.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://blog.vnaik.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-03-20T00:00:00+00:00</updated>
    <id>https://blog.vnaik.com/atom.xml</id>
    <entry xml:lang="en">
        <title>About Me</title>
        <published>2025-03-20T00:00:00+00:00</published>
        <updated>2025-03-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/about/"/>
        <id>https://blog.vnaik.com/about/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/about/">&lt;h1 id=&quot;about&quot;&gt;About&lt;&#x2F;h1&gt;
&lt;p&gt;I&#x27;m Varun - programmer, travel enthusiast, collector of adventures, and serial comma lover. Living in sunny Sydney, Australia.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;contact-me&quot;&gt;Contact me&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;Linkedin - &lt;a href=&quot;https:&#x2F;&#x2F;www.linkedin.com&#x2F;in&#x2F;vnaik&#x2F;&quot;&gt;@vnaik&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Github - &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;varunnaik&quot;&gt;@varunnaik&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;E-mail - me [AT] vnaik.com&lt;&#x2F;li&gt;
&lt;li&gt;Instagram - &lt;a href=&quot;https:&#x2F;&#x2F;instagram.com&#x2F;varun_naik_&quot;&gt;@varun_naik_&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;about-this-website&quot;&gt;About this website&lt;&#x2F;h3&gt;
&lt;p&gt;This website is built with &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt; using a customised &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;carpetscheme&#x2F;lightspeed&quot;&gt;Lightspeed theme&lt;&#x2F;a&gt; and Edward Tufte&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;edwardtufte.github.io&#x2F;et-book&#x2F;&quot;&gt;ET-Book&lt;&#x2F;a&gt; font. This site uses no Javascript (except on some interactive pages), no cookies, and no tracking. This is hosted on &lt;a href=&quot;https:&#x2F;&#x2F;sr.ht&#x2F;&quot;&gt;Sourcehut pages&lt;&#x2F;a&gt;, and is built using &lt;a href=&quot;https:&#x2F;&#x2F;man.sr.ht&#x2F;builds.sr.ht&#x2F;&quot;&gt;sr.ht builds&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: Perfume</title>
        <published>2019-10-07T00:00:00+00:00</published>
        <updated>2019-10-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-perfume/"/>
        <id>https://blog.vnaik.com/posts/book-review-perfume/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-perfume/">&lt;p&gt;Patrick Suskind, 1985&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;perfume.jpg&quot; alt=&quot;Perfume book cover&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This is a gripping book that thoroughly captivates from beginning to end. It concerns Jean-Baptise Grenouille, an orphan in 1700&#x27;s France. Grenouille is a genius and psycopath possessing a superhuman ability to smell, to dissect scents and intuitively combine scents into new and irresistible combinations.&lt;&#x2F;p&gt;
&lt;p&gt; An often underrated and overlooked sense, this book underscores just how much influence it can have on people. Of the senses, smell is perhaps the least glamorous and most misunderstood sense - yet the author weaves it into an incredible tale, with Grenouille elevating mastery of this sense to superhuman levels. If nothing else, this book will leave you with a newfound appreciation for this sense and the ways it can affect our day to day life without us even realising it.&lt;&#x2F;p&gt;
&lt;p&gt;After a series of events, his keen sense of smell and his social isolation, combined with a relentless pursuit of perfection to the exclusion of everything else - even material comforts or a good meal - causes Grenouille to grow up to become a serial killer. Unlike the typical Hollywood serial killer protagonist who only targets people who deserve it, Grenouille targets exclusively young girls.&lt;&#x2F;p&gt;
&lt;p&gt;Perhaps the greatest feat of this book is that even as you&#x27;re horrified by the acts Grenouille commits, you can&#x27;t help but empathise with him - a testament to the skill of the author.&lt;&#x2F;p&gt;
&lt;p&gt;This is a very unique book, highly recommended.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: The Case Against Fragrance</title>
        <published>2019-10-06T00:00:00+00:00</published>
        <updated>2019-10-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-the-case-against-fragrance/"/>
        <id>https://blog.vnaik.com/posts/book-review-the-case-against-fragrance/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-the-case-against-fragrance/">&lt;p&gt;Kate Grenville, 2017&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;case-against-fragrance.jpg&quot; alt=&quot;Book cover of The Case Against Fragrance&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;In this book, Australian author Kate Grenville looks at fragrances - present in everything from perfumes to deodorants to dish soap to scented garbage bags, and examines the evidence - or lack thereof - for their safety.&lt;&#x2F;p&gt;
&lt;p&gt;Kate points out that nearly 17% of the population has some fragrance sensitivity that results in breathing difficulties and other health problems. And yet modern fragrances are pervasive - in everything from toilet cleaner to dish soap to fragrance dispensers to actual perfumes. &lt;&#x2F;p&gt;
&lt;p&gt;The perfume industry is self regulated so there is no reqeuirement to test or find the consequence of these compounds - which are trade secrets so their composition is never revealed to the world. The composition of fragrances also change frequently based on market demand and other factors, yet because they are never tested it is hard to know their health effects. For instance, the famous Chanel No. 5 had to have its formula changed when the original ingredients were found to be carcinogenic. Yet most brands rarely come under any kind of scrutiny for their health effects.&lt;&#x2F;p&gt;
&lt;p&gt;A fragrance contains many hundreds to thousands of compounds - and the effect of these individually, let alone in combination with each other, is not tested.&lt;&#x2F;p&gt;
&lt;p&gt;When you smell something, actual molecules of that thing go up your nose. &lt;&#x2F;p&gt;
&lt;p&gt;The self-regulated perfume industry publishes (non-binding) guidelines on how much carcinogenic compounds a product can contain. They are not banned outright, rather they can be used in concentrations low enough that if you follow the recommended maximum dose you should be fine. The problem is that if you use different items - say, a shampoo, conditioner, moisturuser, deordorant - each of which individually is not harmful when used in the correct dose - could still in aggregate exceed the recommended dose. A lot of beauty products in particular release formaldehyde in contact with air which is extremely harmful.&lt;&#x2F;p&gt;
&lt;p&gt;Modern fragrances are not made in a natural way. Once the fragrant compounds are identified, they can be isolated and used in concentrations that could never occur in nature, and used without all the accompanying chemicals that would accompany a natural fragrance. For instance, the essential oils of rose made artificially smells like a rose but does not contain the hundreds of chemicals in natural rose essential oils and the effect of the missing chemicals are not known - for instance some of them may have neutralising effects that eliminate the harmful effects of essential oils. And the perfume industry uses almost exclusively artificial oils which are hundreds of times tlcheaper than natural ones. &lt;&#x2F;p&gt;
&lt;p&gt;There is no evidence essential oils are good for you but there is evidence that some essential oils are bad for you, since essential oils are not inherently natural though they are made from natural ingredients. And a lot of artificial essential oils musk for example - are modified to remove undesirable properties and enhance desirable ones.&lt;&#x2F;p&gt;
&lt;p&gt;The brain desensitises to smells with exposure, it prefers novel smells to existing ones, this is likely a survival mechanism and is called olfactory fatigue. However, this means you need to use more and more scent to get the same effect as your nose sensitises.&lt;&#x2F;p&gt;
&lt;p&gt; It is not just high profile illnesses like cancer that fragrance causes. They also cause hormonal changes and some deosprays cause a spike in estrogen production. This is particularly disastrous for babies as their bodies are very sensitive to hormones, and these compounds could easily be absorbed - for example, when mommy uses a moisturiser and bavy sucks mommy&#x27;s finger.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, she suggests using fragrance-free products (as opposed to unscented products, which have odor neutralising products added to mask any added fragrances) for better health - and certainly making an audit of how many scented products I use in my daily life did produce a very long list.&lt;&#x2F;p&gt;
&lt;p&gt;All-in-all, a very interesting and esay to read book. Highly recommended.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Securing Docker Containers</title>
        <published>2019-09-07T00:00:00+00:00</published>
        <updated>2019-09-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/securing-docker-containers/"/>
        <id>https://blog.vnaik.com/posts/securing-docker-containers/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/securing-docker-containers/">&lt;p&gt;As Docker becomes more and more popular, the number of security exploits found is only likely to go up. It is quite important to configure a Docker container properly to prevent major security problems, yet this is not often done in practice. The following rules of thumb should help harden a Docker container and make it more secure in production.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Do not run Docker as root. If you are in the habit of prefixing docker commands with sudo, don&#x27;t - especially avoid in production. Instead run containers as a regular user, this way any Docker exploits will at least run only with user privileges.&lt;&#x2F;p&gt;
&lt;p&gt;Create a Docker group and add your user to it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sudo groupadd docker
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sudo usermod -aG docker $USER
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And generally &lt;a href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;install&#x2F;linux&#x2F;linux-postinstall&#x2F;&quot;&gt;follow the steps on the Docker website&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Processes inside the Docker should not be run as root, which is the default behaviour. The following lines at the top of your Dockerfile will ensure the application does not run as root:
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;RUN groupadd --gid 9999 appuser &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;\
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;useradd --system --uid 9999 -g appuser --create-home appuser
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;WORKDIR &#x2F;home&#x2F;appuser
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;USER appuser
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is a good idea to assign a group and user id, otherwise subsequent builds will have ids assigned non-deterministically and the image is not fully reproducible.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Reproducible DOCKERFILE: It is crucial for the purposes of creating reproducible builds of the Docker image, to specify exact versions - eg.,
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;FROM python:3.7.3-alpine3.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;&#x2F;pre&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt; as opposed to
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;FROM python:3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The latter will get the latest Python docker at the time of build depending on the machine the command is run on, and could result in developers, qa, prod having sightly different versions of the Docker image.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Reproducible Packages:This is a general rule that applies also outside the context of Docker. But individual packages inside the Docker - npm, pip, maven, and others alike - also need to be reproducible, and this means specifying exact versions. &lt;a href=&quot;&#x2F;posts&#x2F;package-managers-and-security&#x2F;&quot;&gt;Read more on why this is a good idea.&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Do not inadvertently expose &#x2F;var&#x2F;run&#x2F;docker.sock. This is the Docker Daemon socket and should be hidden from everything and especially anything inside the container. This socket file is the main API to control any of the docker containers running on that host and having write access to this is the same as having root access to the host. Many guides tell you to expose this - eg. to launch a docker container from within docker. &lt;a href=&quot;https:&#x2F;&#x2F;www.lvh.io&#x2F;posts&#x2F;dont-expose-the-docker-socket-not-even-to-a-container&#x2F;&quot;&gt;This should be avoided.&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;man7.org&#x2F;linux&#x2F;man-pages&#x2F;man7&#x2F;capabilities.7.html&quot;&gt;Linux kernel capabilities&lt;&#x2F;a&gt; determine what a process can do - for example, to chown files or kill processes. These are selectively granted to a Docker container by the host operating system. &lt;a href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;v17.12&#x2F;engine&#x2F;security&#x2F;security&#x2F;#linux-kernel-capabilities&quot;&gt;By default only a subset of commands are available&lt;&#x2F;a&gt;. The defaults are good for most use-cases, but for added security it is a good idea to drop all capabilities and add the ones you need back:
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;--cap-drop all --cap-add CHOWN
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;engine&#x2F;reference&#x2F;run&#x2F;#runtime-privilege-and-linux-capabilities&quot;&gt;Docker capabilities reference for --cap-add&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Going off the above, the --privileged flag grants All capabilities. This is best avoided entirely.&lt;&#x2F;li&gt;
&lt;li&gt;Disable default inter-container communication with --icc=false and use --link=CONTAINER_NAME_or_ID:ALIAS to explicitly link containers together.
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;--icc&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;--link&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;CONTAINER_NAME_or_ID:ALIAS
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Consider limits on memory, cpu, number of restarts (--restart=on-failure:&lt;number_of_restarts&gt;), maximum number of file descriptors (--ulimit nofile=&lt;number&gt;) and maximum number of processes (--ulimit nproc=&lt;number&gt;). &lt;a href=&quot;https:&#x2F;&#x2F;docs.docker.com&#x2F;engine&#x2F;reference&#x2F;commandline&#x2F;run&#x2F;#set-ulimits-in-container---ulimit&quot;&gt;Ulimits reference&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;If the application permits it, always use read-only filesystems with the --read-only to prevent modifications. This can be combined with tmpfs if the container needs to persist files temporarily.
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;--read-only --tmpfs &#x2F;tmp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Run Docker containers with --security-opt=no-new-privileges in order to prevent privilege escalation if a vulnerability does get exploited.
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;--security-opt&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;no-new-privileges
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;Keep the host and Docker up to date. This should be part of the regular deploy process but bears repeating - up-to-date systems help thwart container escape vulnerabilities.&lt;&#x2F;li&gt;
&lt;li&gt;Lastly, regularly audit the containers with tools such as &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;coreos&#x2F;clair&quot;&gt;Clair&lt;&#x2F;a&gt;, Synk. These can check Docker images for vulnreabilities.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;This is a pretty long list, but most of the advice boils down to this: Think carefully about exactly what you need your containers to do and make sure they are not capable of doing anything more. Careful application design can save you from many problems down the line.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Handling Time When Programming</title>
        <published>2019-07-26T00:00:00+00:00</published>
        <updated>2019-07-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/handling-time-when-programming/"/>
        <id>https://blog.vnaik.com/posts/handling-time-when-programming/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/handling-time-when-programming/">&lt;p&gt;Handling time when developing can be tricky. Time calculations depend on imprecise and variable physical features (eg. leap seconds to account for the rotation of the earth) and socio-political features (eg. time zones) and this means there could be several pitfalls between expectation and reality when working with time.&lt;&#x2F;p&gt;
&lt;p&gt;First, an introduction to certain time-related concepts.&lt;&#x2F;p&gt;
&lt;h3&gt;UTC&lt;&#x2F;h3&gt;
UTC provides a uniform framework with which to look at time, abstracted away from the vagaries of local times. Interpreting UTC dates is mostly straightforward; issues only arise when converting UTC to local times. This is less of a problem for dates in the past (unless you look at historic dates, maybe older than a few decades or centuries i.e., before the epoch) and more of a problem for dates in the future.
&lt;p&gt;&lt;span class=&quot;sidenote&quot;&gt;Flavours of time: &lt;strong&gt;UT&lt;&#x2F;strong&gt; is a time standard based on the Earth&#x27;s rotation; and &lt;strong&gt;UT1&lt;&#x2F;strong&gt; is conceptually the solar time at 0° longitude but is calculated based on distant quasars because precise measurements of the sun are difficult. It is an accurate reflection (or as close as our technology allows) to the actual rotation of the earth. &lt;strong&gt;TAI&lt;&#x2F;strong&gt; is a time standard where every day is exactly 86000 seconds long, and is measured as the weighted average of about 400 atomic clocks around the world. &lt;strong&gt;GMT&lt;&#x2F;strong&gt; is mean solar time and is correlated to UT1, and may differ from UTC by up to 0.9 s. &lt;strong&gt;GPS time&lt;&#x2F;strong&gt; is TAI offset by 19 seconds and no leap second correction is applied. &lt;&#x2F;span&gt;TAI is a reference timekeeping standard maintained by atomic clocks and is an abstract scale disconnected from physical reality. UT1 is solar time and is correlated to actual physical reality, to the rotation of the Earth which is variable from day to day. UT1 is correlated to reality but is inconvenient and irregular, while TAI is very convenient and regular - and thus UTC is based on TAI, but because TAI can go out of sync with UT1 from time to time, and because we cannot speed up the earth to make UT1 and UTC agree with each other, we instead use &lt;strong&gt;leap seconds&lt;&#x2F;strong&gt; to ensure UTC and UT1 are always within 0.9 seconds of each other. This means UTC is a discontinuous (because of leap seconds), abstract (because it is not perfectly correlated to the Earth&#x27;s rotation), and reliable time-scale for almost all programming needs. Leap seconds are added about every year and a half on average.&lt;&#x2F;p&gt;
&lt;h3&gt;Unix time&lt;&#x2F;h3&gt;
&lt;span class=&quot;sidenote&quot;&gt;However Unix time is affected by leap seconds and it handles these by &#x27;replaying&#x27; the previous second - thus, each day has 86400 seconds and time goes backwards for a second. UTC does not replay seconds and instead has an extra second on days with a leap second for 86401 seconds. &lt;&#x2F;span&gt;Unix time is measured as the number of seconds since the epoch (January 1 1970 in UTC) and is not affected by time zones or daylight savings. The start of the epoch is very close to the start of modern UTC timekeeping and representing and using precise dates from before this time can be challenging. Luckily that is rarely a problem for most projects.32 bit &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Year_2038_problem&quot;&gt;Unix time will overflow in 2038&lt;&#x2F;a&gt; so a 64 bit integer will need to be used to represent times beyond this.
&lt;h3&gt;Time zones and DST&lt;&#x2F;h3&gt;
Given the reliability of UTC, the most natural way of handling time when programming is to store times in UTC (either as a time stamp or an ISO-8601 string) and convert to local times applying current time zone rules as needed.
&lt;p&gt;Just about the only thing that can go wrong is the time zone rules changing between when the time was recorded and the time the date is interpreted. In this context, time zone rule changing means that the government in question has either removed or introduced daylight savings time, or moved the country to another time zone altogether. Since time zones are a political definition and not a technological one, such changes can and will happen arbitrarily for reasons that have nothing to do with technology.&lt;sup&gt;&lt;a href=&quot;#link_1&quot;&gt;[1]&lt;&#x2F;a&gt;&lt;a href=&quot;#link_2&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;a href=&quot;#link_3&quot;&gt;[3]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;span class=&quot;sidenote&quot;&gt;This also applies to dates in the past, because interpreting old dates precisely may require the use of historic time zone rules instead of the current ones. However, this is a very niche problem and by and large current time zone rules will suffice. &lt;&#x2F;span&gt;Thus, our time zone rules (as long as they are up-to-date) tell us how UTC maps to local time right now, but we don&#x27;t know for sure today how UTC time will map to local time in the future because the time zone rules could change between now and then.&lt;&#x2F;p&gt;
&lt;p&gt;Fortunately, the most common use-case for time is also the easiest - recording times of events, usually as they happen
(e.g., when an email is received, when a user logs in, etc.) - these dates are always in the past and are best stored as UTC. They can be converted into local time when read using the latest time zone rules.&lt;&#x2F;p&gt;
&lt;p&gt;Things are a bit more complicated when dealing with dates in the future, for example, to implement a scheduling or reminder system.&lt;&#x2F;p&gt;
&lt;h3&gt;Handling Future Times&lt;&#x2F;h3&gt;
&lt;span class=&quot;sidenote&quot;&gt;On the other hand, if you are interested in a specific &lt;em&gt;universal&lt;&#x2F;em&gt; instant in time in the future then UTC is a good fit. For example, if a task needs to run at 24 hour intervals irrespective of calendar time then use UTC. &lt;&#x2F;span&gt;Future times are usually about human expectations. For example, &quot;10:00 AM next Tuesday&quot; is a human time, not UTC or a Unix time stamp. This future time can be converted to UTC right now for storage, but the converted value could be affected by:
&lt;ul&gt;&lt;li&gt;Leap Seconds&lt;&#x2F;li&gt;
    &lt;li&gt;Daylight Savings Time changeovers&lt;&#x2F;li&gt;
    &lt;li&gt;Time zone rule changes (either UTC offset or daylight savings time changes)&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;span class=&quot;sidenote&quot;&gt;Local time is about human convention. For example, &quot;the same time next week&quot; does not translate to a precise physical duration. It is usually 604,800 seconds, but sometimes it&#x27;s 604,801 seconds (if there is a leap second), it could theoretically be 604,799 seconds (though this has never happened), sometimes it&#x27;s 601,200 seconds or 608,400 seconds (due to daylight savings time changes), and if a locality decides to change which time zone it follows, it could be a completely  different duration entirely. Computing the seconds between now and a future date is fraught with peril. &lt;&#x2F;span&gt;In your time zone, 10:00AM may be 15:00 UTC or 14:00 UTC depending on whether Daylight Savings is in effect - but a meeting scheduled for 10:00 AM happens at that time irrespective of daylight savings. Since UTC does not account for this or for the other problems mentioned above, storing times in a &#x27;human&#x27; format and converting to UTC when needed is the way to go. The more in the future the date is the less certain we can be ahead of time what the precise mapping of a human time (such as 10:00 AM next Tuesday) to UTC is because the underlying rules themselves can change in the meantime. If computed ahead of time, the UTC date could be incorrect by the time the actual time rolls around.
&lt;p&gt;This might seem like nit-picking, and it might indeed be unlikely to cause problems during development, but over the course of a couple of years these errors are just about guaranteed occur, especially if you look at the number of countries and time zones in the world - and not all countries have only one time zone and sometimes even regions that share the same time zone implement daylight savings differently (for example, New South Wales and Queensland in Australia). And daylight savings has the potential to bite you right from the get go.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore the best way to handle this is to store the time as a string, eg. &quot;10:00&quot; along with a date and the IANA time zone identifier (eg. &#x27;Australia&#x2F;Sydney) and let the system make the decision about the precise  UTC time only when this value is read.&lt;&#x2F;p&gt;
&lt;p&gt;If future dates are stored in UTC then every time the time zone rules are updated all the dates will have to be recalculated, so the original &#x27;human&#x27; time will need to be stored anyway. This also applies to leap seconds but can be ignored if your application can be off by a second.&lt;&#x2F;p&gt;
&lt;p&gt;Handling events scheduled to fall in the window of a daylight savings time switch over is trickier. For example, if Daylight Savings Time change happens at 1AM, then an event scheduled to run at 1:30AM every day will fail twice every year - not executing at all on the day of the first DST transition and executing twice on the day of the second DST transition. The best way to avoid this problem is to specifically check for this condition if necessary. Also different countries change to daylight savings time at different times of the day - some do it at midnight - 1AM others at 1AM - 2AM, and still others do it 2AM - 3AM.&lt;&#x2F;p&gt;
&lt;p&gt;Developing with these considerations can be tricky because things may seem to work fine during development and testing, and only fail months or years later when some unexpected time zone change occurs. Automated tests will potentially miss this kind of error unless tests were written with shifting time zones in mind. At the very least automatic tests should exercise the project through some daylight savings spring forwards and fall back.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3&gt;Conclusions&lt;&#x2F;h3&gt;
If the application needs universal time and is not intended to be mapped back to local time then UTC is a very good fit. An example will be a task scheduler that schedules tasks every 24 hours irrespective of conventions such as holidays, time of day, etc. On the other hand, if the application needs to use local times (eg. for a calendar application, or to schedule future work) then it is best to store time strings and IANA time zone names and not UTC.
&lt;ul&gt;
    &lt;li&gt;Store dates in the past as UTC - either as ISO 8601 or a time stamp&lt;&#x2F;li&gt;
    &lt;li&gt;Store dates in the future as a time string with the IANA time zone name&lt;&#x2F;li&gt;
    &lt;li&gt;Time zone rules (offset from UTC) can and do change&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
It is also worth remembering the following points, this is mostly a concern for applications where sub-second accuracy is important.
&lt;ul&gt;
    &lt;li&gt;The system clock can jump forwards and backwards in time, both when adjusting for leap seconds and more commonly when the hardware clock goes out of sync (as it does over time) and gets corrected by ntpd&lt;&#x2F;li&gt;
    &lt;li&gt;If sub-second accuracy is required TAI may be a better fit than UTC as it avoids the leap second jumps of UTC&lt;&#x2F;li&gt;
    &lt;li&gt;Daylight Savings rules also change, countries sometimes postpone or cancel DST changes as one-offs&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3&gt;References&lt;&#x2F;h3&gt;
&lt;p id=&quot;link_1&quot;&gt;1: &lt;a href=&quot;https:&#x2F;&#x2F;www.washingtonpost.com&#x2F;news&#x2F;worldviews&#x2F;wp&#x2F;2015&#x2F;08&#x2F;07&#x2F;north-koreas-new-time-zone-is-perfectly-bizarre&#x2F;?noredirect=on&quot;&gt;North Korea&#x27;s new time zone is perfectly bizarre&lt;&#x2F;a&gt;
&lt;p id=&quot;link_2&quot;&gt;2: &lt;a href=&quot;https:&#x2F;&#x2F;codeofmatt.com&#x2F;time-zone-chaos-inevitable-in-egypt&#x2F;&quot;&gt;Time Zone Chaos Inevitable in Egypt&lt;&#x2F;a&gt;
&lt;p id=&quot;link_3&quot;&gt;3: &lt;a href=&quot;https:&#x2F;&#x2F;codeofmatt.com&#x2F;on-the-timing-of-time-zone-changes&#x2F;&quot;&gt;On the Timing of Time Zone Changes&lt;&#x2F;a&gt;
&lt;p id=&quot;link_4&quot;&gt;4: &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;47104967&#x2F;are-windows-timezone-written-in-registry-reliable&#x2F;47106207#47106207&quot;&gt;Windows time zone change handling&lt;&#x2F;a&gt;
&lt;p id=&quot;link_5&quot;&gt;5: &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;moment&#x2F;moment-timezone&#x2F;issues&#x2F;363&quot;&gt;MomentJs time zone handling&lt;&#x2F;a&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>CSRF, CORS, and HTTP Security headers Demystified</title>
        <published>2019-07-12T00:00:00+00:00</published>
        <updated>2019-07-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/csrf-cors-web-attacks/"/>
        <id>https://blog.vnaik.com/posts/csrf-cors-web-attacks/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/csrf-cors-web-attacks/">&lt;p&gt;With an increasing number of breaches, intrusions, and data thefts, securing a web application is extremely
			important.&lt;&#x2F;p&gt;
		&lt;p&gt;On the other hand, programmers often do not have a strong grasp of how attacks work and how to mitigate them.
			This post attempts to close that gap a little.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;csrf&quot;&gt;CSRF&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;Cross-Site Request Forgery&lt;&#x2F;em&gt; is an attack where a third party forces a user to execute actions against
			a site where they are currently logged in.&lt;&#x2F;p&gt;
		&lt;p&gt;Here is an example illustrating how this works:&lt;&#x2F;p&gt;
		&lt;ol&gt;
			&lt;li&gt;You visit &lt;em&gt;evil.com&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;evil.com&lt;&#x2F;em&gt; has a hidden form that submits on page load to &lt;em&gt;mybank.com&#x2F;transfer-funds&lt;&#x2F;em&gt;.
				Since you are logged in to &lt;em&gt;mybank.com&lt;&#x2F;em&gt;, this request is made with your &lt;em&gt;mybank.com&lt;&#x2F;em&gt;
				cookies and will silently initiate a money transfer out of your account.&lt;&#x2F;li&gt;
			&lt;li&gt;Since &amp;#39;&lt;em&gt;evil.com&lt;&#x2F;em&gt;&amp;#39; and &amp;#39;&lt;em&gt;mybank.com&lt;&#x2F;em&gt;&amp;#39; are different origins, the browser
				refuses to provide the response to &lt;em&gt;evil.com&lt;&#x2F;em&gt; (because of CORS), but the attacker doesn&amp;#39;t
				care, the money&#x27;s already been transferred.&lt;&#x2F;li&gt;
		&lt;&#x2F;ol&gt;
		&lt;p&gt;Now if &lt;em&gt;mybank.com&lt;&#x2F;em&gt; implements CSRF protection correctly:&lt;&#x2F;p&gt;
		&lt;ol&gt;
			&lt;li&gt;Each time &lt;em&gt;mybank.com&lt;&#x2F;em&gt; serves a form to a user, it generates a CSRF token and inserts it into a
				hidden field in the form&lt;&#x2F;li&gt;
			&lt;li&gt;If a POST request is received, it checks the CSRF token against its database - if this is present and
				valid then the request goes through. If the CSRF token is missing or incorrect it is rejected.&lt;&#x2F;li&gt;
		&lt;&#x2F;ol&gt;
		&lt;p&gt;CSRF attacks target state-changing requests and not the direct theft of data because the attacker does not
			see the response of the forged request. The CSRF token provides security by being a stateless token that is
			never stored in the cookies or permanent storage - doing so would defeat the purpose of the token.&lt;&#x2F;p&gt;
		&lt;p&gt;CSRF protection support needs to be added to your application code and cannot be added at the proxy server
			layer (eg. in Nginx).
			&lt;a
				href=&quot;https:&#x2F;&#x2F;github.com&#x2F;OWASP&#x2F;CheatSheetSeries&#x2F;blob&#x2F;master&#x2F;cheatsheets&#x2F;Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.md&quot;&gt;A
				detailed look at CSRF from OWASP&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
		&lt;p&gt;It is good practice to always use the &lt;a href=&quot;https:&#x2F;&#x2F;owasp.org&#x2F;www-community&#x2F;SameSite&quot;&gt;SameSite
				directive&lt;&#x2F;a&gt; with cookies as this provides protection against CSRF attacks.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;cors&quot;&gt;CORS&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;Cross-Origin Resource Sharing&lt;&#x2F;em&gt; only applies in a browser context and is a security mechanism to allow
			one origin to make a request to another origin. All browsers follow the Single Origin Policy, meaning by
			default scripts cannot make requests to other origins - but if the server provides properly configured CORS
			headers this policy can be selectively relaxed. Thus CORS is a way of selectively loosening security and not
			of tightening it.&lt;&#x2F;p&gt;
		&lt;p&gt;When a website makes an XHR request to another origin, the browser initiates a preflight OPTIONS request
			first - and the original request is only made if the server responds to this preflight with a list of
			allowed origins, and this list contains the origin of the current page.&lt;&#x2F;p&gt;
		&lt;p&gt;Note that CORS preflight requests are not made for GET HEAD POST requests with default headers.&lt;&#x2F;p&gt;
		&lt;p&gt;Some key headers sent as a response to an OPTIONS request:&lt;&#x2F;p&gt;
		&lt;ul&gt;
			&lt;li&gt;&lt;em&gt;access-control-allow-credentials&lt;&#x2F;em&gt;: If set, cookies are sent by the browser&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;access-control-allow-origin&lt;&#x2F;em&gt;: The list of origins allowed to make requests, or &amp;#39;*&amp;#39; to
				allow anyone to make requests. If access-control-allow-credentials is set then this cannot be set to
				&amp;#39;*&amp;#39; or the browser will reject the request anyway&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;access-control-allow-methods&lt;&#x2F;em&gt;: The list of HTTP methods allowed to communicate - POST, PUT, etc.
			&lt;&#x2F;li&gt;
		&lt;&#x2F;ul&gt;
		&lt;p&gt;The reason &lt;em&gt;access-control-allow-origin&lt;&#x2F;em&gt; cannot be &#x27;*&#x27; when &lt;em&gt;access-control-allow-credentials&lt;&#x2F;em&gt;
			is set is to prevent developers taking the shortcut of adding a * and then forgetting about it altogether -
			this behaviour forces developers to think about how their API is going to be consumed.&lt;&#x2F;p&gt;
		&lt;p&gt;CORS is often a code smell of sorts - for example, a common use of CORS is to allow multiple properties
			access to one API, like &lt;em&gt;mybank.com&lt;&#x2F;em&gt; being accessed by &lt;em&gt;mybankpersonal.com&lt;&#x2F;em&gt; and
			&lt;em&gt;mybankbusiness.com&lt;&#x2F;em&gt; - and in these cases, CORS can be avoided altogether with the use of an API
			gateway. If all web pages are served from the same domain (&lt;em&gt;mybank.com&lt;&#x2F;em&gt;), no CORS headers need to be
			set and the browser can apply the full Same Origin Policy and provide maximum security. After all, CORS only
			serves to selectively decrease security not increase it.&lt;&#x2F;p&gt;
		&lt;p&gt;On the other hand, using CORS is unavoidable if you are providing an API for third parties to consume from a
			browser (eg. after going through an Oauth process).&lt;&#x2F;p&gt;
		&lt;p&gt;Of course, CORS is irrelevant for requests made outside the web browser, eg. with Curl or with
			server-to-server
			communication.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;xss&quot;&gt;XSS&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;Cross-Site Scripting&lt;&#x2F;em&gt; is an attack where an attacker injects a client-side script into a web page.
			XSS can be used to bypass both the same-origin policy and CSRF protections.&lt;&#x2F;p&gt;
		&lt;p&gt;This is done either by compromising the server (eg. by using a known exploit) or by leveraging
			poorly-sanitised user inputs to add a script to the website, which is triggered whenever a user visits the
			website.&lt;&#x2F;p&gt;
		&lt;p&gt;This is the most commonly exploited vulnerability and can be fixed with careful sanitisation and encoding for
			display of all user input - even things like email addresses could be vectors for script injection. For
			instance., &lt;&#x2F;p&gt;
		&lt;pre&gt;&lt;code&gt;&quot;&amp;gt;&amp;lt;script&amp;gt;alert(1);&amp;lt;&#x2F;script&amp;gt;&quot;@example.org&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
		&lt;p&gt;is a valid email address by RFC 5321!&lt;&#x2F;p&gt;
		&lt;p&gt;XSS is best sanitised on the output side, this way raw data is stored in the database as input by the user
			and is then escaped to an appropriate format at the time it is sent out again - for example, if the result
			is to be sent as part of HTML page the escaping will be different from if the result is to be sent as JSON
			or included in a script tag in the HTML page. The escaping should be appropriate to the output format.
			Additionally, if content is escaped before insert into the database it won&#x27;t be secure against future
			changes to business requirements - for example, if the business decides to serve data through a REST API
			when it was previously only used in a HTML page, the encoding would now be irrelevant.
		&lt;&#x2F;p&gt;
		&lt;p&gt;
			Care must be taken to ensure the sanitisation is done safely - it may be possible to easily
			bypass sanitisation and several such attacks exist. For instance, it may be possible to simply add text such
			as:&lt;&#x2F;p&gt;
		&lt;pre&gt;&lt;code&gt;&amp;lt;scr&amp;lt;script&amp;gt;ipt&amp;gt;alert(1)&amp;lt;&#x2F;script&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
		&lt;p&gt;where the sanitisation filter strips out the script tag, but in doing so causes the split script tag to come
			together and the attack to succeed.
		&lt;&#x2F;p&gt;
		&lt;p&gt;OWASP has a comprehensive section on
			&lt;a href=&quot;https:&#x2F;&#x2F;owasp.org&#x2F;www-community&#x2F;xss-filter-evasion-cheatsheet&quot;&gt;XSS sanitisation bypasses&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
		&lt;p&gt;The OWASP &lt;a
				href=&quot;https:&#x2F;&#x2F;cheatsheetseries.owasp.org&#x2F;cheatsheets&#x2F;Cross_Site_Scripting_Prevention_Cheat_Sheet.html&quot;&gt;cheat
				sheet on XSS prevention&lt;&#x2F;a&gt; is a good starting point for securing an application against XSS.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;csp&quot;&gt;CSP&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;Content Security Policy&lt;&#x2F;em&gt; is an added layer of security to detect and mitigate XSS attacks. By
			specifying the domains that should be considered valid sources for scripts, a web browser will only execute
			scripts loaded from these white listed domains. CSP can specify allowed origins for all dynamic origins,
			including scripts, stylesheets, images, fonts, objects, media (audio, video), frames, and even form-actions.
		&lt;&#x2F;p&gt;
		&lt;p&gt;CSP is set through the Content-Security-Policy HTTP header.&lt;&#x2F;p&gt;
		&lt;p&gt;The difference from CORS is that CORS prevents a third party from accessing a server, while CSP prevents a
			website itself from loading content from a third party, as a defence against XSS.&lt;&#x2F;p&gt;
		&lt;p&gt;CSP is not a silver bullet against XSS but it helps. Ultimately XSS needs to be prevented with careful
			application design and effective user input sanitisation, both on the frontend and the backend.&lt;&#x2F;p&gt;
		&lt;p&gt;Of all the mitigations on this page, CSP is the most complicated because of the depth of its options and also
			likely the hardest to implement because every time a front-end dev adds a new CDN - for fonts, scripts,
			whatever - that CDN needs to be added to the CSP header. Without a well-designed test setup, this could work
			fine in dev but break when pushed to production.&lt;&#x2F;p&gt;
		&lt;p&gt;CSP is complicated enough to require more than a few paragraphs - a full page would do it more justice.&lt;&#x2F;p&gt;
		&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;content-security-policy.com&#x2F;&quot;&gt;Content Security Policy web site&lt;&#x2F;a&gt; describes how to add
			CSP support to your web server and Google&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;csp-evaluator.withgoogle.com&#x2F;&quot;&gt;handy test
				tool&lt;&#x2F;a&gt; can help test these policies.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;hsts&quot;&gt;HSTS&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;HTTP Strict Transport Security&lt;&#x2F;em&gt; protects against protocol downgrade attacks and cookie hijacking by
			allowing a server to declare that browsers should only interact with it using HTTPS connections and never
			HTTP; and the browser should always convert all attempts to access the site using HTTP to HTTPS instead, eg.
			by rewriting all links to use HTTPS instead of HTTP.&lt;&#x2F;p&gt;
		&lt;p&gt;This guards against eavesdropping and man-in-the-middle attacks that work by transparently converting a https
			connection into plain http.&lt;&#x2F;p&gt;
		&lt;p&gt;For example, you might connect to a free wifi service at the airport to access your bank website, but the
			access point is actually a hacker&amp;#39;s laptop that performs a MITM attack and redirects you to a clone of
			the bank&amp;#39;s site. HSTS provides security in this scenario as long as you have accessed your bank&amp;#39;s
			web site at least once over HTTPS (and the bank uses HSTS), the browser will reject this request outright.
		&lt;&#x2F;p&gt;
		&lt;p&gt;This page has details on &lt;a
				href=&quot;https:&#x2F;&#x2F;www.nginx.com&#x2F;blog&#x2F;http-strict-transport-security-hsts-and-nginx&#x2F;&quot;&gt;configuring HSTS for
				Nginx&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;certificate-transparency&quot;&gt;Certificate Transparency&lt;&#x2F;h2&gt;
		&lt;p&gt;&lt;em&gt;Certificate transparency&lt;&#x2F;em&gt; allows HTTPS websites to resist impersonation using fraudulently obtained
			SSL certificates, for example, from a compromised certificate authority as has happened in the past with Comodo.
		&lt;&#x2F;p&gt;
		&lt;p&gt;Certificate Transparency, as the name suggests, is a way making the issuance of website certificates transparent and verifiable.&lt;&#x2F;p&gt;
		&lt;p&gt;It uses public append-only logs of all certificates issued by certificate authorities to function. These logs are maintained as public lists by third parties (Let&#x27;s Encrypt has a &lt;a href=&quot;https:&#x2F;&#x2F;letsencrypt.org&#x2F;docs&#x2F;ct-logs&#x2F;&quot;&gt;log called Oak&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
		&lt;p&gt;When a CA issues a certificate, it records this issuance with one or more CT logs.&lt;&#x2F;p&gt;
		&lt;p&gt;When accepting a TLS certificate, the browser uses the SCT (Signed Certificate Timestamp) field of the certificate to find the appropriate log server and verify that the certificate matches the entry in the log.&lt;&#x2F;p&gt;
		&lt;p&gt;The SCT in the certificate indicates when the issuance occurred, which log it was recorded in, and how to find it (using the signature data).&lt;&#x2F;p&gt;
		&lt;p&gt;A goal of this is to make it impossible for a certificate to be issued for a domain without the domain owner knowing.&lt;&#x2F;p&gt;
		&lt;p&gt;The logs themselves can be analysed and monitored by any interested parties who can watch for suspicious certificates in logs.&lt;&#x2F;p&gt;
		&lt;p&gt;More on &lt;a href=&quot;https:&#x2F;&#x2F;certificate.transparency.dev&#x2F;&quot;&gt;certificate transparency.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;hpkp&quot;&gt;HPKP + Expect-CT (both now obsolete)&lt;&#x2F;h2&gt;
		&lt;p&gt;This feature has been deprecated since this article was written, and this is now superseded by Certificate Transparency and the Expect-CT header.&lt;&#x2F;p&gt;
		&lt;p&gt;Expect-CT has itself become obsolete in June 2021 (the web world moves fast) and Certificate Transparency is now the preferred way of protecting against fraudulently obtained certificates.&lt;&#x2F;p&gt;
		&lt;p&gt;&lt;em&gt;HTTP Public Key Pinning&lt;&#x2F;em&gt; allows HTTPS websites to resist impersonation using fraudulently obtained
			SSL certificates by providing the browser a set of public keys which are the only ones to be trusted for
			future connections to the same origin.&lt;&#x2F;p&gt;
		&lt;p&gt;What if a bad actor manages to hack or trick a legitimate certificate authority into issuing a certificate? HPKP, Expect-CT, and now Certificate Transparency guard against this.&lt;&#x2F;p&gt;
		&lt;p&gt;In the airport scenario above, if the hacker also had a fraudulently obtained certificate for the bank, HSTS
			alone would not protect you - but with HPKP, even this attack can be mitigated. This is a Trust-on-first-use
			technique.&lt;&#x2F;p&gt;
		&lt;p&gt;HPKP will eventually be superseded by the Expect-CT header. &lt;a
				href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Public_Key_Pinning&quot;&gt;More on HPKP&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Headers&#x2F;Expect-CT&quot;&gt;Expect-CT&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;set-cookie&quot;&gt;Set-Cookie&lt;&#x2F;h2&gt;
		&lt;p&gt;This is a fairly standard header and by far the oldest on the page, yet is one of the most important (and
			easiest) to configure correctly.
		&lt;&#x2F;p&gt;
		&lt;p&gt;This can be done by setting a few directives.&lt;&#x2F;p&gt;
		&lt;ul&gt;
			&lt;li&gt;The &lt;strong&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.owasp.org&#x2F;index.php&#x2F;SameSite&quot;&gt;SameSite: Strict&lt;&#x2F;a&gt;&lt;&#x2F;strong&gt; directive
				protects against CSRF attacks by stripping cookies from all cross-domain requests.&lt;&#x2F;li&gt;
			&lt;li&gt;The &lt;strong&gt;Secure&lt;&#x2F;strong&gt; directive ensures the cookies are only ever used in a https connection -
				thus providing additional security over what is provided by HPKP and HSTS.&lt;&#x2F;li&gt;
			&lt;li&gt;The &lt;strong&gt;HttpOnly&lt;&#x2F;strong&gt; directive ensures JavaScript cannot access the cookie.&lt;&#x2F;li&gt;
		&lt;&#x2F;ul&gt;
		&lt;p&gt;You will as a rule want to set all three. More on &lt;a
				href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Headers&#x2F;Set-Cookie&quot;&gt;Set-Cookie&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
		&lt;h2 id=&quot;others&quot;&gt;Other Headers&lt;&#x2F;h2&gt;
		&lt;p&gt;These are generally less important to set but good to be aware of.&lt;&#x2F;p&gt;
		&lt;ul&gt;
			&lt;li&gt;&lt;em&gt;X-XSS Protection&lt;&#x2F;em&gt;: This header is outdated and possibly harmful and CSP does a better job of
				providing protections. The main job of this in Chrome is to trigger the &lt;a
					href=&quot;https:&#x2F;&#x2F;groups.google.com&#x2F;a&#x2F;chromium.org&#x2F;forum&#x2F;#!msg&#x2F;blink-dev&#x2F;TuYw-EZhO9g&#x2F;blGViehIAwAJ&quot;&gt;XSS
					auditor which has been deprecated&lt;&#x2F;a&gt; for &lt;a
					href=&quot;https:&#x2F;&#x2F;portswigger.net&#x2F;blog&#x2F;abusing-chromes-xss-auditor-to-steal-tokens&quot;&gt;being insecure&lt;&#x2F;a&gt;.
			&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;X-Frame-Options&lt;&#x2F;em&gt;: This header indicates whether the site should be allowed to be displayed
				within an iFrame. This has been superseded by &lt;strong&gt;CSP&#x27;s frame-ancestors&lt;&#x2F;strong&gt; option which is
				better supported in modern browsers.&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;X-Content-Type-Options&lt;&#x2F;em&gt;: This prevents the browser from attempting to guess the type of content
				if the Content-Type header is not set. This can prevent XSS exploits for websites that allow users to
				upload and share files.&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;X-Download Options&lt;&#x2F;em&gt;: Again useful in scenarios involving user file uploads, when set to &#x27;noopen&#x27;
				this forces the browser to download a file instead of attempting to display it in the browser.&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;em&gt;Referrer-Policy&lt;&#x2F;em&gt;: When this is set to &#x27;no-referrer&#x27; the browser does not send the referrer when
				making a request and is useful for privacy. &lt;a
					href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Headers&#x2F;Referrer-Policy&quot;&gt;More on
					referrer-policy&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
		&lt;&#x2F;ul&gt;
		&lt;hr &#x2F;&gt;
		&lt;p&gt;Overall, as the web grows in terms of features and complexity, the attack surface also grows correspondingly
			large. A properly configured web server and a well designed application with effective use of HTTPS (eg.
			with Let&#x27;s Encrypt) can provide a good degree of protection.&lt;&#x2F;p&gt;
		&lt;p&gt;As an added bonus, many of the mitigations on this page can be applied at the proxy server (CSP, HSTS, HPKP)
			or network level (better server proxying to remove the need for CORS), and only the CSRF and XSS protections
			really need to be added to the application. of course, the mitigations on this page are only a small subset
			of what is possible.&lt;&#x2F;p&gt;
		&lt;p&gt;Some useful resources to implement these:
		&lt;ul&gt;
			&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;observatory.mozilla.org&#x2F;&quot;&gt;Scan your site for misconfigurations using Mozilla&#x27;s Observatory&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blog.cloudflare.com&#x2F;introducing-certificate-transparency-monitoring&#x2F;&quot;&gt;Get notified every time a certificate is issued for your domain, with Cloudflare&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;securityheaders.com&#x2F;&quot;&gt;Test your site&#x27;s security headers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;jarv.is&#x2F;notes&#x2F;security-headers-cloudflare-workers&#x2F;&quot;&gt;Adding security headers
					automatically with Cloudflare workers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
			&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;csp-evaluator.withgoogle.com&#x2F;&quot;&gt;CSP evaluator to test policies&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
		&lt;&#x2F;ul&gt;
		&lt;&#x2F;p&gt;
		&lt;p&gt;That said, the application is likely the biggest source of security threats anyway, most of the &lt;a
				href=&quot;https:&#x2F;&#x2F;www.owasp.org&#x2F;images&#x2F;7&#x2F;72&#x2F;OWASP_Top_10-2017_%28en%29.pdf.pdf&quot;&gt;OWASP top ten&lt;&#x2F;a&gt; have to
			deal with application programming bugs - highlighting the importance of good design and application
			architecture.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: Prisoners of Geography</title>
        <published>2019-06-22T00:00:00+00:00</published>
        <updated>2019-06-22T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-prisoners-of-geography/"/>
        <id>https://blog.vnaik.com/posts/book-review-prisoners-of-geography/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-prisoners-of-geography/">&lt;p&gt;Tim Marshall, 2015&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;prisoners_of_geography.jpeg&quot; alt=&quot;Prisoners of Geography&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This book examines how a country&#x27;s decisions and actions are influenced primarily by its geography - natural landscape features, neighbors, and topography. Drawing lines on a map while disregarding the topography and the geographical cultures in an area is asking for trouble.&lt;&#x2F;p&gt;
&lt;p&gt;The book looks at ten maps, including Russia, The United States, China, The Middle East, Africa, Latin America, Western Europe, Korea and Japan, India and Pakistan, and the Arctic.&lt;&#x2F;p&gt;
&lt;p&gt;The book does a good job of looking past the rhetoric and media messages and examining the hidden rationality and systematic thinking behind outwardly crazy actions by world leaders. The chapters on the Middle East, China, Russia, and the USA are particularly interesting.&lt;&#x2F;p&gt;
&lt;p&gt;For example, the book makes the point that the USA is possibly the most secure state in the world, because it is almost impossible to challenge it geographically - being in full control of the region coast to coast making it hard for attackers to get a foothold. The Mississippi links the country effectively, and while Cuba sits close to its mouth, as long as Cuba is not controlled by a third party and isn&#x27;t strong the USA has no problem.&lt;&#x2F;p&gt;
&lt;p&gt;All great nations spend peacetime preparing for the day war breaks out. Nations plan for the future fifty or a hundred years from now, and undertake many decisions with this context in mind. All countries strive for strategic depth - which is an area to fall back to in case of an invasion -eg. as Russia did during the invasions of Napoleon and Hitler. Russia and China posses this, while Israel and Pakistan do not. Countries lacking strategic depth are always insecure. This partially explains why Russia is so interested in Crimea and Georgia, and why Tibet and Xinjiang are so crucial to China - these regions vastly increase Russia and China&#x27;s strategic depth.&lt;&#x2F;p&gt;
&lt;p&gt;The book explains the world&#x27;s situation for each of the ten maps, and makes the case that changing the world is not as easy as simply swapping one leader for another - after all, this will not change the geographic realities on the ground. The reality of what countries can do is determined in no small part by the rivers, mountains, concrete, seas that are part of it geography.&lt;&#x2F;p&gt;
&lt;p&gt;An excellent, informative, and interesting book - I enjoyed reading every page and it left me with a lot of insight into world affairs.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>The Gun: The AK-47 and the evolution of war</title>
        <published>2019-06-21T00:00:00+00:00</published>
        <updated>2019-06-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-the-gun/"/>
        <id>https://blog.vnaik.com/posts/book-review-the-gun/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-the-gun/">&lt;p&gt;C.J. Chivers, 2010&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;the_gun.jpg&quot; alt=&quot;Book cover of The Gun&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This book explores the political and economic conditions that gave rise to the assault rifle - with a focus on the AK-47 - and how assault rifles have shaped war and security around the world since the end of the cold war.&lt;&#x2F;p&gt;
&lt;p&gt;It starts off with a short history of automatic weapons - starting from the earliest ones like the Gatling gun - and its effective use by colonisers to &#x27;civilize&#x27; the &#x27;savages&#x27; by killing them, and then on to the Maxim - a much more reliable and efficient killing machine, and so on to the German WWII STG-45 and finally on to the AK-47. The pages of modern warfare abound with war profiteers, arms dealers, kings, and governments bent on domination.&lt;&#x2F;p&gt;
&lt;p&gt;The AK-47 is not the pinnacle of firearm design - it is however, a very effective compromise in the field of weapon design, a field where every decision has its tradeoffs. For example, making a weapon more powerful also makes it heavier and more expensive to manufacture (since it needs more metal) and maintain, but making it too light means it is no longer effective.&lt;&#x2F;p&gt;
&lt;p&gt;The AK-47 made a very good set of choices - for example, its parts are large and loose fitting rather than snug - making them less likely to jam when dirty, inadequately lubricated, or clogged with carbon or dirt. The larger parts are also easier to clean and make the weapon easier to take apart and put back together in the field with less equipment. A weapon so simple and easy to maintain and use even a child could do it - as they often do.&lt;&#x2F;p&gt;
&lt;p&gt;This was counter-intuitive to western designs, which used precision tools to make parts with tight tolerances, which were very prone to jamming when exposed to dust or if not properly lubricated - a problem so bad that during the Vietnam war American soldiers preferred to use captured AK-47s to their own M-16 rifles.&lt;&#x2F;p&gt;
&lt;p&gt;At the end of the cold war, stockpiled AK-47s found their way onto the open market. And because of their simple engineering and their ability to withstand rust, even many of the earliest produced rifles from the 1940s are still in excellent working order today in 2019 - meaning the problem of assault rifle proliferation will be a problem well into the next few decades.&lt;&#x2F;p&gt;
&lt;p&gt;While the AK-47 is the most famous Russian export, the biggest seller of AK-47s in the world remains the United States of America, with the UK, France, Russia, and China close behind (and these are also the five permanent members of the UN security council).&lt;&#x2F;p&gt;
&lt;p&gt;The book is very well researched and well written and I found it an easy read. It is engaging and very accessible, and does not bore the reader with historical minutiae or complex technical discussions about assault weapons. Anyone who wonders about why the world is the way it is would enjoy this book.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Package Managers and Security</title>
        <published>2019-04-17T00:00:00+00:00</published>
        <updated>2019-04-17T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/package-managers-and-security/"/>
        <id>https://blog.vnaik.com/posts/package-managers-and-security/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/package-managers-and-security/">&lt;p&gt;
  Installing packages with npm, pip and the like is such a routine part of software development that we rarely even pause to think about it, often installing packages as recommended by an answer on Stackoverflow without a second thought.&lt;p&gt;
&lt;p&gt;However, there is a growing trend of extensions and packages being taken over by hackers and advertising companies. &lt;sup&gt;&lt;a href=&quot;#link-1&quot;&gt;[1]&lt;&#x2F;a&gt; &lt;a href=&quot;#link-2&quot;&gt;[2]&lt;&#x2F;a&gt; &lt;a href=&quot;#link-3&quot;&gt;[3]&lt;&#x2F;a&gt; &lt;a href=&quot;#link-4&quot;&gt;[4]&lt;&#x2F;a&gt; &lt;a href=&quot;#link-8&quot;&gt;[8]&lt;&#x2F;a&gt;&lt;a href=&quot;#link-9&quot;&gt;[9]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
&lt;&#x2F;p&gt;
&lt;p&gt;This means installing packages can be dangerous and should be done with some amount of thought and planning.&lt;&#x2F;p&gt;
&lt;p&gt;Some dangers and considerations of package managers follow.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;hr &#x2F;&gt;
&lt;!-- EDIT2 SECTION &quot;Version Pinning and auto updates&quot; [582-1255] --&gt;
&lt;h3 class=&quot;sectionedit3&quot; id=&quot;typosquatting&quot;&gt;Typosquatting&lt;&#x2F;h3&gt;
&lt;div class=&quot;level3&quot;&gt;
&lt;p&gt;&lt;strong&gt;Typosquatting&lt;&#x2F;strong&gt; refers to the practise of registering a package with a name similar to an existing package. People install this by mistake. This happened recently with npm when the &lt;strong&gt;crossenv&lt;&#x2F;strong&gt; package was typosquatted when someone create the package &lt;strong&gt;cross-env&lt;&#x2F;strong&gt; to serve malware in addition to the normal functionality of crossenv. &lt;sup&gt;&lt;a href=&quot;#link-5&quot;&gt;[5]&lt;&#x2F;a&gt; &lt;a href=&quot;#link-6&quot;&gt;[6]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;
A carefully crafted malware package can go undected for a long time. In addition typos when installing packages. Eg. typing &lt;&#x2F;p&gt;
&lt;p&gt;&lt;pre&gt;npm install requist&lt;&#x2F;pre&gt;&lt;&#x2F;p&gt;
&lt;p&gt;where you meant to type&lt;&#x2F;p&gt;
&lt;p&gt;&lt;pre&gt;npm install request&lt;&#x2F;pre&gt; &lt;&#x2F;p&gt;
&lt;p&gt;can be dangerous if it installs a functionally equivalent package, but with malware bundled.&lt;&#x2F;p&gt;
&lt;p&gt;This excellent article on &lt;a href=&quot;http:&#x2F;&#x2F;incolumitas.com&#x2F;2016&#x2F;06&#x2F;08&#x2F;typosquatting-package-managers&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;http:&#x2F;&#x2F;incolumitas.com&#x2F;2016&#x2F;06&#x2F;08&#x2F;typosquatting-package-managers&#x2F;&quot; rel=&quot;nofollow&quot;&gt;typosquatting package managers&lt;&#x2F;a&gt; examines the issue in depth.&lt;&#x2F;p&gt;
 &lt;&#x2F;div&gt;
 &lt;hr &#x2F;&gt;
&lt;!-- EDIT1 SECTION &quot;Package managers and security&quot; [1-581] --&gt;
&lt;h3 class=&quot;sectionedit2&quot; id=&quot;version_pinning_and_auto_updates&quot;&gt;Version Pinning and auto updates&lt;&#x2F;h3&gt;
&lt;div class=&quot;level3&quot;&gt;
&lt;p&gt;
It is unsafe to use package managers without explicit version pinning. It is safest to install an exact version of a package, eg. &lt;pre&gt;1.2.3&lt;&#x2F;pre&gt; as opposed to &lt;pre&gt;1.*.*&lt;&#x2F;pre&gt; or even &lt;pre&gt;1.2.*&lt;&#x2F;pre&gt;&lt;p&gt;even if your package manager allows the latter two.&lt;&#x2F;p&gt;
&lt;p&gt; There have been cases of package dev&#x27;s accounts being taken over by hackers who then release a point version that auto upgrades itself.&lt;sup&gt;&lt;a href=&quot;#link-7&quot;&gt;[7]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; Allowing packages to auto-update is &lt;a href=&quot;https:&#x2F;&#x2F;www.lucidchart.com&#x2F;techblog&#x2F;2017&#x2F;03&#x2F;15&#x2F;package-management-stop-using-version-ranges&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;www.lucidchart.com&#x2F;techblog&#x2F;2017&#x2F;03&#x2F;15&#x2F;package-management-stop-using-version-ranges&#x2F;&quot; rel=&quot;nofollow&quot;&gt;not a good idea&lt;&#x2F;a&gt; for security, but is also bad because you may wind up with entirely different packages across your dev and production environment or across developers.
It is important to use npm-shrinkwrap or such to freeze package versions - and it is very important to maintain this carefully.
&lt;&#x2F;p&gt;
&lt;p&gt;Remember that whether or not any update is major or minor is determined solely by the package publisher, so expecting minor versions to be safe in all cases is probably not realistic.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;hr&gt;
&lt;!-- EDIT3 SECTION &quot;Typosquatting&quot; [1256-1819] --&gt;
&lt;h3 class=&quot;sectionedit4&quot; id=&quot;obfuscated_code&quot;&gt;Obfuscated Code&lt;&#x2F;h3&gt;
&lt;div class=&quot;level3&quot;&gt;
&lt;p&gt;
Having obfuscated code in a package is a significant red flag. Obfuscated (or binary) code in a package is reason enough to do without the package. Remember that this is a dev package library not an end user app - and hidden&#x2F;binary code absolutely does not belong in your dev tools. And even if the hidden code is currently not malicious, that may not be true later - hiding code makes it harder to scrutinise, something hackers are very likely to exploit to their advantage.
&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;hr&gt;
&lt;!-- EDIT4 SECTION &quot;Obfuscated Code&quot; [1820-2297] --&gt;
&lt;h3 class=&quot;sectionedit5&quot; id=&quot;how_packages_become_compromised&quot;&gt;How packages become compromised&lt;&#x2F;h3&gt;
&lt;p&gt;To sum up, packages can become compromised in various ways, including but not limited to:&lt;&#x2F;p&gt;
&lt;div class=&quot;level3&quot;&gt;
&lt;ul&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt;Typosquatting (eg. cross-env vs crossenv)&lt;&#x2F;div&gt;
&lt;&#x2F;li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt;Dev accounts getting phished&lt;&#x2F;div&gt;
&lt;&#x2F;li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt;Dev straight out sells the package (offers of $50k for popular packages are not uncommon)&lt;&#x2F;div&gt;
&lt;&#x2F;li&gt;
&lt;li class=&quot;level1&quot;&gt;&lt;div class=&quot;li&quot;&gt;Attackers manage to inject code into the package without the dev catching on&lt;&#x2F;div&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;And once a package is compromised a simple minor point release to inject malicious code combined with a package manager that auto-updates minor versions helps propagate the malware.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;hr&gt;
&lt;!-- EDIT5 SECTION &quot;How packages become compromised&quot; [2298-2653] --&gt;
&lt;h3 class=&quot;sectionedit6&quot; id=&quot;security_updates&quot;&gt;Dependency Hell&lt;&#x2F;h3&gt;
&lt;div class=&quot;level3&quot;&gt;
&lt;p&gt;
The OWASP lists using packages with known vulnerabilities in the &lt;a href=&quot;https:&#x2F;&#x2F;www.owasp.org&#x2F;images&#x2F;f&#x2F;f8&#x2F;OWASP_Top_10_-_2013.pdf&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;www.owasp.org&#x2F;images&#x2F;f&#x2F;f8&#x2F;OWASP_Top_10_-_2013.pdf&quot; rel=&quot;nofollow&quot;&gt;top ten ways applications are vulnerable&#x2F;become compromised.&lt;&#x2F;a&gt;
&lt;&#x2F;p&gt;
&lt;p&gt;
As developers, we are ultimately responsible for our work - and if a third party package is compromised, through no fault of our own, it is still the developers who have to clean up and manage the situation as far as the code is concerned.
&lt;&#x2F;p&gt;
&lt;p&gt;
A barebones ExpressJs app created with the Express creation tool has about 50 dependencies (including sub-dependencies). That&#x27;s a massive attack surface.
&lt;&#x2F;p&gt;
&lt;p&gt;
If a hacker detects a vulnerability in one package every single webapp on the Internet that uses the package is now compromised. This is a very strong incentive for hackers and you can expect the number of attacks on packages to rise over time. Visibility will be higher for more popular packages meaning you are likely to be notified of hacks sooner - if those hacks are ever detected in the first place.
&lt;&#x2F;p&gt;
&lt;p&gt;To sum up,&lt;&#x2F;p&gt;
  &lt;ul&gt;
    &lt;li&gt;There is no foolproof way to be sure your packages are secure so being vigilant is important&lt;&#x2F;li&gt;
    &lt;li&gt;Do not allow any automatic package updates. Instead carefully plan and manage updates manually&lt;&#x2F;li&gt;
    &lt;li&gt;Maintaining a local package cache may help but is hard to maintain&lt;&#x2F;li&gt;
    &lt;li&gt;Package audit tools like &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nodesecurity&#x2F;nsp&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;github.com&#x2F;nodesecurity&#x2F;nsp&quot; rel=&quot;nofollow&quot;&gt;Nsp&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rubysec&#x2F;bundler-audit&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;github.com&#x2F;rubysec&#x2F;bundler-audit&quot; rel=&quot;nofollow&quot;&gt;Bundler-audit&lt;&#x2F;a&gt; can help; and if you use Github it can automaticall audit npm packages for you&lt;&#x2F;li&gt;
    &lt;li&gt;Tools such as &lt;a href=&quot;https:&#x2F;&#x2F;dependabot.com&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;Dependabot&quot; rel=&quot;nofollow&quot;&gt;Dependabot&lt;&#x2F;a&gt; can help you maintain your project dependencies&lt;&#x2F;li&gt;
    &lt;li&gt;It is important to have a smooth deployment process - so security fixes and package updates can be applied quickly and with minimal effort wherever possible, and ideally this can be done out-of-band from regular deployment schedules.&lt;&#x2F;li&gt;
  &lt;&#x2F;ul&gt;
&lt;p&gt;&lt;strong&gt;Edit:&lt;&#x2F;strong&gt; Since this post was written npm has released an &lt;a href=&quot;https:&#x2F;&#x2F;docs.npmjs.com&#x2F;cli&#x2F;audit&quot;&gt;audit tool&lt;&#x2F;a&gt; which can scan all packages in your package-lock.json and inform you of potential security issues.&lt;&#x2F;p&gt;
&lt;p&gt;Minimising package dependencies is still probably a very good idea.&lt;&#x2F;p&gt;
&lt;p&gt;In addition, as of 2019, Github can now scan certain projects (eg. Node and Python) for vulnerabilities on push to origin and advise on updating the packages. With Github&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;dependabot.com&#x2F;blog&#x2F;hello-github&#x2F;&quot;&gt;acquisition of Dependabot&lt;&#x2F;a&gt; you can now have automatic pull requests created for out-of-date packages.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;&#x2F;article&gt;
&lt;hr &#x2F;&gt;
&lt;footer&gt;
  &lt;h3&gt;Footnotes&lt;&#x2F;h3&gt;
&lt;p id=&quot;link-1&quot;&gt;[1]: &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;o_cee&#x2F;status&#x2F;892306836199800836&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;o_cee&#x2F;status&#x2F;892306836199800836&quot; rel=&quot;nofollow&quot;&gt;https:&#x2F;&#x2F;twitter.com&#x2F;o_cee&#x2F;status&#x2F;892306836199800836&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-2&quot;&gt;[2]: &lt;a href=&quot;http:&#x2F;&#x2F;www.drinchev.com&#x2F;blog&#x2F;alert-npm-modules-hijacked&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;http:&#x2F;&#x2F;www.drinchev.com&#x2F;blog&#x2F;alert-npm-modules-hijacked&#x2F;&quot; rel=&quot;nofollow&quot;&gt;http:&#x2F;&#x2F;www.drinchev.com&#x2F;blog&#x2F;alert-npm-modules-hijacked&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-3&quot;&gt;[3]: &lt;a href=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14902630&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14902630&quot; rel=&quot;nofollow&quot;&gt;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14902630&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-4&quot;&gt;[4]: &lt;a href=&quot;https:&#x2F;&#x2F;www.bleepingcomputer.com&#x2F;news&#x2F;security&#x2F;52-percent-of-all-javascript-npm-packages-could-have-been-hacked-via-weak-credentials&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;www.bleepingcomputer.com&#x2F;news&#x2F;security&#x2F;52-percent-of-all-javascript-npm-packages-could-have-been-hacked-via-weak-credentials&#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&#x2F;&#x2F;www.bleepingcomputer.com&#x2F;news&#x2F;security&#x2F;52-percent-of-all-javascript-npm-packages-could-have-been-hacked-via-weak-credentials&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-5&quot;&gt;[5]: &lt;a href=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14901566&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14901566&quot; rel=&quot;nofollow&quot;&gt;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=14901566&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-6&quot;&gt;[6]: &lt;a href=&quot;https:&#x2F;&#x2F;arstechnica.com&#x2F;information-technology&#x2F;2017&#x2F;09&#x2F;devs-unknowingly-use-malicious-modules-put-into-official-python-repository&#x2F;&quot; class=&quot;urlextern&quot; title=&quot;https:&#x2F;&#x2F;arstechnica.com&#x2F;information-technology&#x2F;2017&#x2F;09&#x2F;devs-unknowingly-use-malicious-modules-put-into-official-python-repository&#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&#x2F;&#x2F;arstechnica.com&#x2F;information-technology&#x2F;2017&#x2F;09&#x2F;devs-unknowingly-use-malicious-modules-put-into-official-python-repository&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-7&quot;&gt;[7]: &lt;a href=&quot;https:&#x2F;&#x2F;a9t9.com&#x2F;blog&#x2F;chrome-extension-adware&#x2F;&quot;&gt;Our Copyfish extension was stolen and adware-infested&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-8&quot;&gt;[8]: &lt;a href=&quot;https:&#x2F;&#x2F;www.zdnet.com&#x2F;article&#x2F;zero-day-in-popular-jquery-plugin-actively-exploited-for-at-least-three-years&#x2F;&quot;&gt;Zero-day in popular jQuery plugin actively exploited for at least three years&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p id=&quot;link-9&quot;&gt;[9]: &lt;a href=&quot;https:&#x2F;&#x2F;withatwist.dev&#x2F;strong-password-rubygem-hijacked.html&quot;&gt;strong_password v0.0.7 rubygm hijacked&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: Spacesuit: Fashioning Apollo</title>
        <published>2019-02-12T00:00:00+00:00</published>
        <updated>2019-02-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-spacesuit-fashioning-apollo/"/>
        <id>https://blog.vnaik.com/posts/book-review-spacesuit-fashioning-apollo/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-spacesuit-fashioning-apollo/">&lt;p&gt;Nicholas de Monchaux, 2011&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;spacesuit.jpg&quot; alt=&quot;Book cover of Spacesuit&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This book explores the history behind the space suit used for the Apollo missions and how an unconventional (for space exploration) company that manufactured girdles and bras went toe-to-toe with the top engineering and technology firms of the era for the contract to make the Apollo spacesuit - and won. &lt;&#x2F;p&gt;
&lt;p&gt;Early spacesuits were clunky and hard to move in, not very maneuverable. For extended missions of 10 hours or even a few days (such as the Moon missions), the suits were very uncomfortable to be in. Sweat with nowhere to evaporate would collect in the suit and upon subsequent extraction the astronaut would look like he&#x27;d been submerged underwater for an extended period. Diaphanous hands were a significant mission risk. The A7L that Playtex designed had 21 layers, with a significant focus on comfort of the wearer and of of redundancy. It was very adaptable and was built to be light, tactile, and maneuverable, unlike many of the competitor&#x27;s futuristic looking metal spacesuits - and was a triumph of usability design over looks, of practicality over glamour. &lt;&#x2F;p&gt;
&lt;p&gt;The book starts with the early history of space travel, starting with the first balloon flights, and how people first dealt with high altitude flights which require pressurized suits to prevent death. The book then explores the history of spacesuits including the A7L Apollo suit, and several of the subsequent hard suits that were an improvement but were ultimately never used. The book also sheds some light on the political and social climate of the era of the moon missions - exploring how the cold war shaped the space program - most space rockets were simply repurposed ICBMs with the nuclear warhead replaced with a space capsule, after all.&lt;&#x2F;p&gt;
&lt;p&gt;It also explores how the very practical and results-oriented culture of Playtex - staffed with rank upon rank of seamstresses, and designers - integrated with the heavy bureaucratic and engineering-oriented culture at NASA. The book loosely touches upon several aspects of systems engineering, of fashion, design, redundancy, and the cold war and the military-industrial complex.&lt;&#x2F;p&gt;
&lt;p&gt;The book is very culture oriented, and is not technical at all. It comes with excellent illustrations and photographs, and is a long but light and fun read.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: Command and Control</title>
        <published>2018-12-03T00:00:00+00:00</published>
        <updated>2018-12-03T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-command-and-control/"/>
        <id>https://blog.vnaik.com/posts/book-review-command-and-control/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-command-and-control/">&lt;p&gt;Eric Schlosser, 2013&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;command_and_control-schlosser_eric.jpg&quot; alt=&quot;Command and Control book cover&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This book explores, in depth, a topic that many of us don&#x27;t really givee a thought about - the safety of nuclear weapons. The concept of &#x27;safe&#x27; nuclear weapons sounds like an oxymoron - after all, these are weapons designed to kill thousands, or even millions of people. But because nuclear wars are rare, and nuclear weapons are common, people are more likely to die from an accidental detonation - eg. from a bomb going off in a plane crash, than through a deliberate use of nuclear weapons.&lt;&#x2F;p&gt;
&lt;p&gt;This book provides a history of American nuclear weapon accidents - and explores one case in particular, that of a  Titan II ICBM with a hydrogen bomb payload that accidentally exploded in its silo, thankfully without triggering the nuclear fusion weapon it carried.&lt;&#x2F;p&gt;
&lt;p&gt;The book details several accidents and near misses, ranging from accidents with unreliable missiles with 4 megaton warheads, to crash after crash of bombers carrying hydrogen bombs, often scattering bombs all over the countryside, thankfully without detonating them - in several cases thanks to sheer luck and not at all due to the flimsy and unreliable safety features on the weapons themselves. In one case, a bomber flying over North Carolina in 1958 dropped its bomb when its captain grabbed the bomb release lever by accident - the bomb in question was a nuclear weapon and it landed in the backyard of a house, and did not detonate - perhaps the only case of the United States being bombed during the cold war.&lt;&#x2F;p&gt;
&lt;p&gt;Weapon safety was often an afterthought - Hollywood movies often tell a tale of nuclear weapons that can only be launched after entering a security code, and one would assume that these weapons are carefully guarded and tracked by the military. The reality is that for much of the cold war, security and safeguards were severely lacking.&lt;&#x2F;p&gt;
&lt;p&gt;During the cold war, thermonuclear weapons would be stored in overseas bases often guarded by a single soldier, missiles that only require the flick of a switch to launch. Nuclear weapons also did not have any consistent tracking system. And US mainland based ICBMs were often being secured with weak electronic safeguards where random electricity fluctuations could launch 50 Minuteman missiles each carrying 1 Megaton warheads - missiles which were designed to wipe out 150 cities and 30 % of the population of the Soviet Union.&lt;&#x2F;p&gt;
&lt;p&gt;Cruise missiles with nuclear warheads were stored in bunkers side-by-side with training missiles with dummy warheads, verification checklists were routinely ignored to save time, and appalling safety procedures were the norm. There was a very poor safety culture at the Strategic Air Command - with the SAC often fighting to implement safety features for fear of ceding authority over weapons and even to save costs - all but the most major nuclear weapon accidents (which couldn&#x27;t be easily covered up) were routinely omitted from safety records for fear of attracting greater oversight.&lt;&#x2F;p&gt;
&lt;p&gt;Once started, I found the book hard to put down - Schlosser does an excellent job of storytelling, weaving in the narrative of the Damascus incident along with the background of the cold war. He does an excellent job of providing context for many of the more seemingly stupid command and control decisions made during the war. Sometimes the comprehensiveness of the book works against it though, there is definitely a great deal to take in and reading about yet another near-miss with a nuclear weapon gets boring after you&#x27;ve read about several such accidents already.&lt;&#x2F;p&gt;
&lt;p&gt;This is a must read for anyone who enjoys the movie Dr. Strangelove, anyone who builds things, particularly complicated things, and anyone interested in the Cold War and its history. In addition, this leaves you with an appreciation of how seemingly random the behaviour of complicated systems can sometimes be and how to design security and safeguards for such complex systems. Having finished this book, one gains an appreciation for just how lucky we are as a people - the fact that we haven&#x27;t destroyed the planet with these weapons is very much attributable to blind luck.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Escher&#x27;s Reflecting Sphere</title>
        <published>2018-09-12T00:00:00+00:00</published>
        <updated>2018-09-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/escher-reflecting-sphere/"/>
        <id>https://blog.vnaik.com/posts/escher-reflecting-sphere/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/escher-reflecting-sphere/">&lt;p&gt;The Dutch artist M.C. Escher is one of my favourite artists. His surreal depictions of recursion,
self-reference, and infinity strike a strong chord with me as a programmer.&lt;&#x2F;p&gt;
&lt;p&gt;This is an attempt at creating artwork inspired by his famous self-portrait, using Javascript and ThreeJs, Pixel Shaders,
and a phone camera.&lt;&#x2F;p&gt;
&lt;p&gt;First, the result: A sphere that &#x27;reflects&#x27; the image from your webcam.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;button id=&quot;startCamera&quot;&gt;Press to Start&lt;&#x2F;button&gt;&lt;&#x2F;p&gt;
&lt;div id=&quot;balls&quot;&gt;&lt;&#x2F;div&gt;
&lt;video autoplay playsinline muted id=&quot;videoElement&quot; style=&quot;display:none&quot;&gt;&lt;&#x2F;video&gt;
&lt;canvas height=&quot;1000&quot; width=&quot;1000&quot; id=&quot;videoImage&quot; style=&quot;display:none&quot;&gt;&lt;&#x2F;canvas&gt;
&lt;p&gt;Of course, this has some deficiencies - because a phone camera is flat, you do not see a true 3D view, so the
hand holding the phone is never visible. However, the phone sensors do allow us some other liberties, such
as adding a shadow beneath the sphere.&lt;&#x2F;p&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This is how the effect above is produced.&lt;&#x2F;p&gt;
&lt;hr &#x2F;&gt;
&lt;h3&gt;Step 1: Create a renderer and scene using &lt;a href=&quot;https:&#x2F;&#x2F;threejs.org&quot;&gt;ThreeJS&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;This will be used to render the sphere.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;scene &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;Scene&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;camera &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;PerspectiveCamera&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;45&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;width &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0.01&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;renderer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;WebGLRenderer&lt;&#x2F;span&gt;&lt;span&gt;({ alpha: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;, antialias: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3&gt;Step 2: Add lighting&lt;&#x2F;h3&gt;
&lt;p&gt;ThreeJs has several types of lights (and cameras) each with their own features. A simple AmbientLight would
    suffice for a basic scene, but this does not cast shadows. For this reason I went with a SpotLight, which
    does.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;spotLight &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;SpotLight&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0xffffff&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;spotLight.position.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;150&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;10 &lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;scene.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;spotLight &lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using a camera helper and the OrbitControls helper is invaluable in testing and debugging anything to do
    with ThreeJs.&lt;&#x2F;p&gt; &lt;p&gt;The camera helper helps trace how the light affects the screen and the orbit controls helps adjust the camera for best effect.&lt;&#x2F;p&gt;
&lt;p&gt;Setting up the lights and camera just right so they focus on the right part of the scene at the right
    distance and angle can be tedious without these tools.&lt;&#x2F;p&gt;
&lt;h3&gt;Step 3: Add the sphere and a floor below it to receive shadows&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphere &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;Mesh&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;SphereGeometry&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphereRadius&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;64&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;64&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;MeshPhongMaterial&lt;&#x2F;span&gt;&lt;span&gt;({ color: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0xffffff &lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphere&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;castShadow &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphere&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;receiveShadow &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;scene.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphere&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floorGeometry &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;PlaneBufferGeometry&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;Mesh&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floorGeometry&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;MeshPhongMaterial&lt;&#x2F;span&gt;&lt;span&gt;({ color: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0xffffff &lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; Move floor into position
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;position&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;y &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;sphereRadius&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;rotation&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;PI &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&#x2F; - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;castShadow &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;receiveShadow &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;scene.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This adds the sphere and a floor, and then translates and rotates the floor into position beneath the
    sphere, ready to catch the sphere&#x27;s shadows.&lt;&#x2F;p&gt;
&lt;p&gt;Both objects consist of a geometry, specifying the number of sides and faces; and a
    material which describes its appearance. This is a solid colour for now.&lt;&#x2F;p&gt;
&lt;h3&gt;Step 4: Replace the solid colour material with a webcam feed&lt;&#x2F;h3&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;document&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;&amp;#39;videoImage&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImageContext &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImage.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;getContext&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;&amp;#39;2d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImageContext.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;fillRect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImage&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;width&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImage&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;height&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoTextureM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;new &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;THREE&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;Texture&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;videoImage&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gets the webcam feed, renders it to a canvas, and then creates a ThreeJS texture from the canvas, ready
    to apply onto the sphere replacing the solid colour texture.&lt;&#x2F;p&gt;
&lt;h3&gt;Step 5: Use Shaders to create a pencil sketch effect&lt;&#x2F;h3&gt;
&lt;p&gt;A fragment shader (aka Pixel shader) takes an input pixel (represented by an RGBA colour value), applies some processing on it, and outputs another RGBA value which may be used by the renderer to represent the final pixel on the screen.&lt;&#x2F;p&gt;
&lt;p&gt;This is based on an awesome shader from user Starea on Shadertoy: &lt;a href=&quot;https:&#x2F;&#x2F;www.shadertoy.com&#x2F;view&#x2F;ldSyzV&quot;&gt;Sketchy Stippling Stylization&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; It takes each pixel
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec3 col &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;texture2D&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;webcam&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vUv&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;rgb&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; Applies a random blur to it
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec2 r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vUv&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec3 blurred &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;=
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;texture2D&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;webcam&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vUv &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;cr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;vec2&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;44.0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;1000.0&lt;&#x2F;span&gt;&lt;span&gt;) ).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;rgb&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; Does a colour dodge over the original image,
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; merging the blur over the original drawImage
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; This retains the sharp edges of the original
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; image along with the blurred edges of the blur layer
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec3 inv &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;vec3&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;blurred&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec3 lighten &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;colorDodge&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;col&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;inv&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; Converts to greyscale
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;vec3 gcol &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;vec3&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;greyScale&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;lighten&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; And outputs to screen
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;gl_FragColor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8ec07c;&quot;&gt;vec4&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;gcol&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Fragment shaders are a fascinating subject, and too complicated to go into detail here.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;pixelshaders.com&quot;&gt;Pixelshaders.com&lt;&#x2F;a&gt; is an incredible resource to get started with shader programming, explaining the basics for beginners.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;shadertoy.com&quot;&gt;Shadertoy&lt;&#x2F;a&gt; provides many shaders to play with, you can edit them right in the browser.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h3&gt;Step 6: Use the phone gyroscope to move the lights as you move your phone&lt;&#x2F;h3&gt;
&lt;p&gt;The shadow below looks more realistic if it moves along with your phone. Making this happen is as simple as getting the angle of rotation from the phone gyroscope and moving the light appropriately.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#282828;color:#fdf4c1aa;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;window&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;addEventListener&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b8bb26;&quot;&gt;&amp;quot;deviceorientation&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fa5c4b;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;event&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#928374;&quot;&gt;&#x2F;&#x2F; Move the light to match the position of gravity
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;spotLight&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;position&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fabd2f;&quot;&gt;event&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdf4c1;&quot;&gt;gamma &lt;&#x2F;span&gt;&lt;span style=&quot;color:#fe8019;&quot;&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d3869b;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And this creates the final effect of the reflecting sphere.&lt;&#x2F;p&gt;
&lt;p&gt;The script file &lt;strong&gt;script.js&lt;&#x2F;strong&gt; loaded on this page contains the full code of this implementation. View page source to see it.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: The Box</title>
        <published>2018-09-11T00:00:00+00:00</published>
        <updated>2018-09-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-the-box/"/>
        <id>https://blog.vnaik.com/posts/book-review-the-box/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-the-box/">&lt;p&gt;Marc Levinson, 2006&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;the_box.jpg&quot; alt=&quot;The Box book cover&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;When we think about how technology has shaped civilization, we often think of glamourous technologies - such as the Internet, the microchip, or the light bulb. However, mundane, everyday stuff - such as the barbed wire fence, or in the case of this book, the humble shipping container, has had a much bigger impact on humanity than people realise.&lt;&#x2F;p&gt;
&lt;p&gt;The shipping container brought about globalisation, destroyed unions around the world, and forever changed the way we work and do business. This book explores how the shipping container revolution came to be, and how the container itself caused all those effects.&lt;&#x2F;p&gt;
&lt;p&gt;The book is meticulously researched and goes into fine detail about the history of the container, but that is also its weak point - most of the book deals with history - eg., so-and-so committee met on such-and-such a date to discuss whether the optimal height of the container should be 8 feet or 8.5 feet. While the history is insightful, I found page after page of historic minutiae boring.&lt;&#x2F;p&gt;
&lt;p&gt;I recommend skimming through the book - most of the interesting things are in the first couple of chapters and the last chapter.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Book Review: Genghis Khan and the Making of the Modern World</title>
        <published>2018-08-11T00:00:00+00:00</published>
        <updated>2018-08-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/book-review-genghis-khan/"/>
        <id>https://blog.vnaik.com/posts/book-review-genghis-khan/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/book-review-genghis-khan/">&lt;p&gt;Jack Weatherford, 2004&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;img src=&quot;.&#x2F;genghis_khan.jpg&quot; alt=&quot;Genghis Khan and the Making of the Modern World book cover&quot; &#x2F;&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;A very insightful book that illustrates how much Genghis Khan was responsible for laying the foundations of the modern world.&lt;&#x2F;p&gt;
&lt;p&gt;From the separation of church and state, religious tolerance, the postal system, a unified calendar, and cannons, to the creation of the basis of modern China and Russia, Genghis Khan had a major impact on civilization.&lt;&#x2F;p&gt;
&lt;p&gt;While his conquests and massacres are often talked about, people fail to recognise his contributions to the societies he conquered - once the conquest itself was over. In addition he was a tactical genius, far surpassing the achievements of other great generals such as Alexander, Caesar, Napoleon and the like - and the fact that he was not a member of the aristocracy and indeed rose from poverty and the life of a bandit to conquer most of the known world makes this even more impressive. He had no starting advantage in the form of instruction in battlecraft, tactics, or ruling empires, which most other generals received from childhood.&lt;&#x2F;p&gt;
&lt;p&gt;Genghis Khan unified, in 25 years, more of the world than the Romans managed in 400 years. His superb organisational skills and keen understanding of people and governments led him to create an unstoppable juggernaut.&lt;&#x2F;p&gt;
&lt;p&gt;This is a tremendously interesting book with loads of insight about a part of history that is very often glossed over. Highly recommended.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Pattern Study</title>
        <published>2018-08-02T00:00:00+00:00</published>
        <updated>2018-08-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/pattern-study/"/>
        <id>https://blog.vnaik.com/posts/pattern-study/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/pattern-study/">&lt;style&gt;
@media (max-width: 500px) {
    figure {
        margin: 0 auto;
    }
}
&lt;&#x2F;style&gt;
&lt;p&gt;I recently came across this interesting geometric pattern that involves squares (trapezoids, more precisely). This is an exploration of this pattern.&lt;&#x2F;p&gt;
&lt;p&gt;All illustrations on this page are programatically generated in your browser. View source to see more.&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;canvas id=&quot;box2&quot; width=&quot;600px&quot; height=&quot;600px&quot; class=&quot;resize&quot;&gt;&lt;&#x2F;canvas&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This pattern is made up of several alternating squares, each containing a number of rhombuses.&lt;&#x2F;p&gt;
&lt;p&gt;The different elements become more clear when the squares are randomly coloured.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;&lt;canvas id=&quot;box3&quot; width=&quot;600px&quot; height=&quot;600px&quot; class=&quot;resize&quot;&gt;&lt;&#x2F;canvas&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;This is an individual box.&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;canvas id=&quot;box1&quot; width=&quot;600px&quot; height=&quot;600px&quot; class=&quot;resize&quot;&gt;&lt;&#x2F;canvas&gt;&lt;&#x2F;figure&gt;
&lt;p&gt;It&#x27;s constructed as illustrated below, by inscribing successively smaller rhombuses inside the original square. Each of the rhombuses are slightly rotated with respect to the previous one.&lt;&#x2F;p&gt;
&lt;figure&gt;&lt;canvas id=&quot;box6&quot; width=&quot;300px&quot; height=&quot;200px&quot;&gt;&lt;&#x2F;canvas&gt;&lt;button onclick=&quot;drawDemoBox()&quot;&gt;Restart&lt;&#x2F;button&gt;&lt;&#x2F;figure&gt;
Alternate squares are rotated in opposite directions.
&lt;p&gt;It is interesting how a spiral emerges from a figure constructed entirely out of straight lines.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, I used an animation to really bring the figure to life. The outermost square is rotated through 20 degrees in each step. Each inscribed one is rotated by a successively smaller amount, and this leads to some very interesting patterns emerging.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;&lt;canvas id=&quot;box4&quot; width=&quot;600px&quot; height=&quot;600px&quot; class=&quot;resize&quot;&gt;&lt;&#x2F;canvas&gt;&lt;&#x2F;figure&gt;
And an individual tile on its own.
&lt;figure&gt;&lt;canvas id=&quot;box5&quot; width=&quot;600px&quot; height=&quot;600px&quot; class=&quot;resize&quot;&gt;&lt;&#x2F;canvas&gt;&lt;&#x2F;figure&gt;
View the source of this webpage to see the code used to generate these images.
It&#x27;s fascinating how sometimes a simple set of rules applied multiple times results in something quite unlike what was started with.
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Phone Numbers and Identity</title>
        <published>2018-07-27T00:00:00+00:00</published>
        <updated>2018-07-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/phone-numbers-and-identity/"/>
        <id>https://blog.vnaik.com/posts/phone-numbers-and-identity/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/phone-numbers-and-identity/">&lt;p&gt;Using a phone to verify identity - eg. when signing up for a service online - is quite common these days. This typically happens by sending an SMS to your phone with a code that you type into the website - two factor authentication.&lt;&#x2F;p&gt;
&lt;p&gt;One reason companies do this is to make it harder for people to create throwaway accounts to spam the service, although this is often done also for security.&lt;&#x2F;p&gt;
&lt;p&gt;While this may help companies fight spam, 2FA with phones does not actually make your account safer for a few reasons:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;You don&#x27;t own your phone number, your service provider does. And they can lease it to someone else in accordance to their terms and conditions. I learned this the hard way when my phone company leased my number to someone else after I moved countries. The new person got all my messages from my bank, tax, and every other service I signed up for. Most services also won&#x27;t let you change your phone number without first sending a confirmation SMS to your current phone.&lt;&#x2F;li&gt;
&lt;li&gt;Phone numbers are vulnerable to social engineering. A good social engineer can impersonate you, claim you lost your phone, and convince customer service to transfer your phone number directly to their phone. Customer service refuses? Try again in a few hours to get a different agent, and continue till someone agrees to the request.&lt;&#x2F;li&gt;
&lt;li&gt;Using (illegal, but easy to set up) fake  cell towers (eg. Stingray), hackers can intercept 2fa messages. The phone network infrastructure is &lt;a href=&quot;https:&#x2F;&#x2F;www.nytimes.com&#x2F;2018&#x2F;12&#x2F;26&#x2F;opinion&#x2F;cellphones-security-spying.html&quot;&gt;not as secure&lt;&#x2F;a&gt; as is widely believed and these sort of hacks have &lt;a href=&quot;https:&#x2F;&#x2F;arstechnica.com&#x2F;information-technology&#x2F;2017&#x2F;05&#x2F;thieves-drain-2fa-protected-bank-accounts-by-abusing-ss7-routing-protocol&#x2F;&quot;&gt;already been carried out&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;If your phone is set to display the message text on the lock screen, any code can be read over your shoulder without even having to unlock your phone.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Using a phone number to confirm identity is especially dangerous because it provides the illusion of security without actually providing any guarantees. It potentially weakens security because if your phone number should happen to be compromised fixing the situation can be extremely difficult or even impossible.&lt;&#x2F;p&gt;
&lt;p&gt;On the other hand, using a dedicated security device such as a &lt;a href=&quot;https:&#x2F;&#x2F;www.yubico.com&#x2F;&quot; nofollow&gt;Yubikey&lt;&#x2F;a&gt; or using a dedicated app such as Google Authenticator for 2FA vastly improves account security.&lt;&#x2F;p&gt;
&lt;p&gt;To sum up: &lt;strong&gt;Always enable 2FA either with a hardware device (eg. Yubikey) or an app such as the Google Authenticator and do not rely on SMS messages to provide the second factor.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Photograph Ships with Software Defined Radio</title>
        <published>2018-01-24T00:00:00+00:00</published>
        <updated>2018-01-24T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://blog.vnaik.com/posts/photograph-ships-sydney-with-sdr/"/>
        <id>https://blog.vnaik.com/posts/photograph-ships-sydney-with-sdr/</id>
        
        <content type="html" xml:base="https://blog.vnaik.com/posts/photograph-ships-sydney-with-sdr/">&lt;figure class=&quot;fullwidth&quot;&gt;
    &lt;video width=&quot;100%&quot; autoplay muted controls&gt;
    &lt;source src=&quot;3110003971583999891_vid.mp4&quot; type=&quot;video&#x2F;mp4&quot;&gt;
    &lt;&#x2F;video&gt;
    OVATION OF THE SEAS, March 12 2020
&lt;&#x2F;figure&gt;
An RTL-SDR radio dongle can pick up all kinds of interesting radio signals using cheap hardware that connects to a laptop or Raspberry Pi over USB. The dongle and associated software allow accesssing these signals programatically using Python or your language of choice.
&lt;p&gt;The RTL-SDR website &lt;a href=&quot;https:&#x2F;&#x2F;www.sigidwiki.com&#x2F;wiki&#x2F;Signal_Identification_Guide&quot;&gt;lists several signal types&lt;&#x2F;a&gt; including&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;military&lt;&#x2F;li&gt;
&lt;li&gt;aviation&lt;&#x2F;li&gt;
&lt;li&gt;satellite&lt;&#x2F;li&gt;
&lt;li&gt;marine signals&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Ships and smaller boats usually have an AIS transponder. The &lt;a href=&quot;https:&#x2F;&#x2F;www.sigidwiki.com&#x2F;wiki&#x2F;Automatic_Identification_System_(AIS)&quot;&gt;Automatic Identification Sydtem (AIS)&lt;&#x2F;a&gt; broadcasts a vessel&#x27;s location, name, size, and speed every few minutes. The main use of this is collission avoidance, but is also useful for traffic management and like with this project, tracking ships.&lt;&#x2F;p&gt;
&lt;p&gt;These transponders can be turned off, but ships within Sydney harbour usually leave them on for collission avoidance purposes - even military vessesls usually run these when in the harbour.&lt;&#x2F;p&gt;
&lt;p&gt;Back in 2017 I moved to an apartment with a view of Sydney Harbour, and I decided to build out a project to finally use a Raspberry Pi Zero I had lying around. The idea was to photograph ships, using a time-lapse, as they moved under the harbour bridge.&lt;&#x2F;p&gt;
&lt;p&gt;I used an &lt;a href=&quot;http:&#x2F;&#x2F;www.nooelec.com&#x2F;store&#x2F;nesdr-mini-rtl2832-r820t.html&quot;&gt;RTL2832 dongle&lt;&#x2F;a&gt; for this
project, and there are several alternatives available as listed on the &lt;a href=&quot;https:&#x2F;&#x2F;www.rtl-sdr.com&#x2F;&quot;&gt;RTL-SDR
website.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Along with the dongle and Raspberry Pi, I used a Raspberry Pi camera and a cheap phone maginifier lens to enhance the image quality, my setup being some distance from the bridge. I ran this out of my apartment between 2018 and 2022, at which point I moved and turned this off.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;
    &lt;video width=&quot;100%&quot; autoplay muted controls&gt;
    &lt;source src=&quot;5037780001648521445_vid.mp4&quot; type=&quot;video&#x2F;mp4&quot;&gt;
    &lt;&#x2F;video&gt;
    &lt;a href=&quot;https:&#x2F;&#x2F;www.navy.gov.au&#x2F;capabilities&#x2F;ships-boats-and-submarines&#x2F;hmas-canberra-iii&quot;&gt;HMAS Canberra&lt;&#x2F;a&gt;, one of Australia&#x27;s two helicopter carriers moves under the bridge under some dramatic weather, March 29, 2022
&lt;&#x2F;figure&gt;
&lt;p&gt;This project uses the RTL dongle to listen to these signals and track ships as they move into and out of Sydney harbour. The area under the harbour bridge and to Circular Quay (the geofenced area below) is the designated geofence - with a bit of trial and error, I was able to figure out the precise moment to trigger a timelapse capture using the Raspberry Pi python SDK.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;
    &lt;img src=&quot;photographing-ships-geofence.png&quot; &#x2F;&gt;
    The geofenced area where ships are detected
&lt;&#x2F;figure&gt;
&lt;p&gt;Not all ships correctly advertise their size over AIS - often the only reliable pieces of information are the ship MMSI &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Maritime_Mobile_Service_Identity&quot;&gt;(Maritime Mobile Service Identity)&lt;&#x2F;a&gt;, a unique code assigned to each ship in the world and its GPS coordinates. Since the RPI camera isn&#x27;t hugely capable, there is little use in trying to run a capture of vessels of a size under about 40 metres - they just show up as specks in the video. I filter out ships smaller than 80 metres in length (as self-reported by their AIS messages) to reduce the number of false captures although this often fails because not all ships report accurate sizes.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;
    &lt;video width=&quot;100%&quot; autoplay muted controls&gt;
    &lt;source src=&quot;5030001231659856071_vid.mp4&quot; type=&quot;video&#x2F;mp4&quot;&gt;
    &lt;&#x2F;video&gt;
    &lt;a href=&quot;https:&#x2F;&#x2F;www.navy.gov.au&#x2F;capabilities&#x2F;ships-boats-and-submarines&#x2F;mv-sycamore&quot;&gt;MV Sycamore&lt;&#x2F;a&gt;, navy aviation training vessel, August 7, 2022
&lt;&#x2F;figure&gt;
&lt;p&gt;The capture software is written in Python and uses AWS for backend processing work. The code is all present in this repository, &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;varunnaik&#x2F;shippix&quot;&gt;Sydney Ships&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The project uses the excellent &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;dgiardini&#x2F;rtl-ais&quot;&gt;RTL-AIS&lt;&#x2F;a&gt; project to process
    incoming
    AIS messages and &lt;a href=&quot;https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;libais&#x2F;&quot;&gt;libais&lt;&#x2F;a&gt; to access them in Python.&lt;&#x2F;p&gt;
&lt;figure class=&quot;fullwidth&quot;&gt;
    &lt;video width=&quot;100%&quot; controls&gt;
    &lt;source src=&quot;3104380001571118461_vid.mp4&quot; type=&quot;video&#x2F;mp4&quot;&gt;
    &lt;&#x2F;video&gt;
    The Sunshine Princess crosses the Sydney Harbour, 26 November 2017
&lt;&#x2F;figure&gt;
&lt;p&gt;The data reported by the ships themselves is not always accurate, with some jumps in position that needed to be
filtered out. As you might imagine, Sydney harbour is a busy area with lots of traffic&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
