<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Daniel Dušek</title>
    <description>The latest articles on Forem by Daniel Dušek (@dusekdan).</description>
    <link>https://forem.com/dusekdan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F368805%2F1366a378-d43d-47f1-96ae-385c185e748b.jpeg</url>
      <title>Forem: Daniel Dušek</title>
      <link>https://forem.com/dusekdan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dusekdan"/>
    <language>en</language>
    <item>
      <title>How to add ls and other Linux commands into Windows command line</title>
      <dc:creator>Daniel Dušek</dc:creator>
      <pubDate>Sun, 25 Dec 2022 18:41:34 +0000</pubDate>
      <link>https://forem.com/dusekdan/how-to-add-ls-and-other-linux-commands-into-windows-command-line-56ap</link>
      <guid>https://forem.com/dusekdan/how-to-add-ls-and-other-linux-commands-into-windows-command-line-56ap</guid>
      <description>&lt;p&gt;Whenever I happen to be using the command line on Windows, I always end up running &lt;code&gt;ls&lt;/code&gt; command and getting the annoying error &lt;code&gt;'ls' is not recognized as an internal or external command, operable program or batch file&lt;/code&gt;. This command is sometimes available in powershell, but never by default in cmd. Turns out there is an easy way to make it available, and in this post I will explain how.&lt;/p&gt;

&lt;p&gt;The trick relies on &lt;code&gt;git&lt;/code&gt; being installed on Windows system already. My assumption here is that you, the reader, is someone who works in tech and therefore chances are, you already have git on your machine. If that is not the case, you can &lt;a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git" rel="noopener noreferrer"&gt;install git by following this guide&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;TL;DR - &lt;strong&gt;what's the trick&lt;/strong&gt; - &lt;code&gt;git&lt;/code&gt; is a tool that allows you to manage versions of your code projects and happens to bring the Windows executables for a lot of linux commands with the installation. You can add the directory where these executables are stored into the path and make them available in the CMD. Steps how to achieve this are bellow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find the &lt;code&gt;usr\bin&lt;/code&gt; subfolder of the git installation folder (by default, it will be &lt;code&gt;C:\Program Files\Git\usr\bin&lt;/code&gt;). If you installed git somewhere else, find the &lt;code&gt;usr\bin&lt;/code&gt; directory inside that other location.&lt;/li&gt;
&lt;li&gt;Open &lt;code&gt;This PC&lt;/code&gt; or &lt;code&gt;Computer&lt;/code&gt;, right click in the directory and select &lt;code&gt;Properties&lt;/code&gt;. Control Panel &amp;gt; System and Security &amp;gt; System window will open.&lt;/li&gt;
&lt;li&gt;On the left, choose &lt;code&gt;Advanced system settings&lt;/code&gt; and click &lt;code&gt;Environment Variables&lt;/code&gt; at the bottom.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6u3lg4d7yvvit1atsjk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe6u3lg4d7yvvit1atsjk.PNG" alt="Image of System and Security / System in Control Panel, Advanced system settings option in the left menu is highlighted" width="553" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the &lt;code&gt;Environment Variables&lt;/code&gt; window, find &lt;code&gt;Path&lt;/code&gt; variable in the &lt;code&gt;System Variables&lt;/code&gt; on the bottom part of the screen.&lt;/li&gt;
&lt;li&gt;Click &lt;code&gt;Edit&lt;/code&gt; and add path to the &lt;code&gt;user\bin&lt;/code&gt; subdirectory of git installation folder in there:

&lt;ul&gt;
&lt;li&gt;If git is installed in &lt;code&gt;C:\Program Files\Git&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Path to add is: &lt;code&gt;C:\Program Files\Git\usr\bin&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Click &lt;code&gt;OK&lt;/code&gt;, then &lt;code&gt;OK&lt;/code&gt;, then &lt;code&gt;OK&lt;/code&gt; and then &lt;strong&gt;open the new command line&lt;/strong&gt;. Running the &lt;code&gt;ls&lt;/code&gt; command now will result in listing the items of a current working directory. &lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common issues
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;I can't find my git installation folder, how do I look it up?&lt;/strong&gt; If you can run the &lt;code&gt;git&lt;/code&gt; command from commandline:

&lt;ul&gt;
&lt;li&gt;Go to &lt;code&gt;This PC&lt;/code&gt;, right click and select &lt;code&gt;Properties&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;Advanced system settings&lt;/code&gt; on the left&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;Environment Variables&lt;/code&gt; on the bottom and look for anything with a &lt;code&gt;git&lt;/code&gt; in the path. There you will eventually find the actual git installation directory.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;I followed the tutorial and still can't use &lt;code&gt;ls&lt;/code&gt; in my CMD, what should I do?&lt;/strong&gt; Start a &lt;strong&gt;new&lt;/strong&gt; commandline window:

&lt;ul&gt;
&lt;li&gt;Either press &lt;code&gt;Windows Key&lt;/code&gt; + &lt;code&gt;R&lt;/code&gt; and type in CMD, or just find it in the start menu.&lt;/li&gt;
&lt;li&gt; Run &lt;code&gt;ls&lt;/code&gt; again and it should work now. &lt;/li&gt;
&lt;li&gt;Why? You were probably trying to run the command from previously opened CMD, which read the old value of Path variable when it was starting. It doesn't know about the changes you have made into the path yet.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Setting up virtual hosts on XAMPP* local webserver</title>
      <dc:creator>Daniel Dušek</dc:creator>
      <pubDate>Mon, 22 Mar 2021 07:31:25 +0000</pubDate>
      <link>https://forem.com/dusekdan/setting-up-virtual-hosts-on-xampp-local-webserver-iij</link>
      <guid>https://forem.com/dusekdan/setting-up-virtual-hosts-on-xampp-local-webserver-iij</guid>
      <description>&lt;p&gt;This tutorial will walk you through setting up a virtual host for your PHP project. I split this tutorial in two parts - beginning with quick walkthrough, from start to finish in few steps. In the second part, I dive deep on each step of the tutorial to make sure you understand everything that you have done. I have no illusion that most of you will read the second part.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I explain the concepts from a Windows-user perspective (paths, file names). If you happen to be a unix/linux user, I am sure you know where you have your stuff and can follow along.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why create virtual hosts?
&lt;/h2&gt;

&lt;p&gt;Your PHP application will likely run on real url address, such as &lt;code&gt;danieldusek.com&lt;/code&gt;, you want to emulate this locally and &lt;code&gt;localhost&lt;/code&gt; just doesn't do it for you. You want &lt;code&gt;danieldusek.localhost&lt;/code&gt; instead. Or... your application maybe doesn't handle relative paths well. &lt;/p&gt;

&lt;h2&gt;
  
  
  Quick step by step virtual host setup in xampp
&lt;/h2&gt;

&lt;p&gt;Make sure you know:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Path to your PHP project, e.g.: &lt;code&gt;c:\users\username\source\mysite&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Path to your xampp/lampp installation, e.g.: &lt;code&gt;c:\xampp&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then go and do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your &lt;code&gt;xampp&lt;/code&gt; install directory and locate &lt;code&gt;httpd-vhosts.conf&lt;/code&gt; file under &lt;code&gt;apache/conf/extra&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;Open &lt;code&gt;httpd-vhosts.conf&lt;/code&gt; file in text editor of your choice.&lt;/li&gt;
&lt;li&gt;At the very end of the file paste the following code, but replace:

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;c:\users\username\source\mysite&lt;/code&gt; with actual path to your PHP project,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;danieldusek.localhost&lt;/code&gt; with your own name (do yourself a favor and use actually &lt;code&gt;.localhost&lt;/code&gt; TLD)
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;VirtualHost&lt;/span&gt; &lt;span class="err"&gt;*:80&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    DocumentRoot "c:\users\username\source\mysite"
    ServerName danieldusek.localhost
    &lt;span class="nt"&gt;&amp;lt;Directory&lt;/span&gt; &lt;span class="err"&gt;"c:\users\username\source\mysite"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        Require all granted
    &lt;span class="nt"&gt;&amp;lt;/Directory&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/VirtualHost&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have just configured local server to look for scripts to execute in whichever directory you chose, whenever it receives request looking for &lt;code&gt;yourdomain.localhost&lt;/code&gt; (or whatever you have decided to go with).&lt;/p&gt;

&lt;p&gt;Be sure to &lt;strong&gt;RESTART THE APACHE SERVER&lt;/strong&gt; after saving the file.  &lt;/p&gt;

&lt;p&gt;The last step is to tell your computer to route all &lt;code&gt;yourdomain.localhost&lt;/code&gt; requests to localhost (your computer):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;c:\windows\system32\drivers\etc&lt;/code&gt; folder and find &lt;code&gt;hosts&lt;/code&gt; file&lt;/li&gt;
&lt;li&gt;Open &lt;code&gt;hosts&lt;/code&gt; file in text editor of your choice, that can save files as administrator (Visual Studio Code can elevate to Administrator in a user friendly way when saving)&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;127.0.0.1 yourdomain.localhost&lt;/code&gt; line at the very end of it.&lt;/li&gt;
&lt;li&gt;Save.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When you now access &lt;code&gt;yourdomain.localhost&lt;/code&gt; in the browser, you will see your project. If not, your setup is broken and/or you don't know what you are doing. Please proceed to the second part of this tutorial where I explain the concepts in more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step by step breakdown of what is actually going on
&lt;/h2&gt;

&lt;p&gt;I decided to run an experiment where I will be explaining every concept of this tutorial and everything related to it, instead of providing troubleshooting FAQ. If my hypothesis is correct and my explanation up to par, you should be able to fix your problem on your own.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it all works?
&lt;/h3&gt;

&lt;p&gt;My basic assumption here is that you have installed an XAMPP or similar package on your device. You did that because you want to develop an application in PHP or other supported language.&lt;/p&gt;

&lt;p&gt;XAMPP package comes with an apache server which knows how to process web requests, interpret PHP (if installed) scripts and provide content back to user sending the web requests. &lt;strong&gt;One of the things that apache server does while processing user's request is checking for what domain the request was sent&lt;/strong&gt;. Remember this. Doesn't matter whether the domain name is valid or whether it exists.&lt;/p&gt;

&lt;p&gt;When apache web server runs on a computer, it can be reached by accessing &lt;code&gt;127.0.0.1&lt;/code&gt; - or its &lt;code&gt;localhost&lt;/code&gt; alias - in the browser. Alternatively, if you happen to be connected to the internet and you have a public IP assigned to your computer, other people can send requests to your apache web server as well - they will use your public IP, instead of the &lt;code&gt;localhost&lt;/code&gt;/&lt;code&gt;127.0.0.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;During the XAMPP / web server installation, you are allowed to choose on which ports the server will operate. By convention, http traffic will be served on port &lt;code&gt;80&lt;/code&gt; and https traffic on &lt;code&gt;443&lt;/code&gt;. This convention is again important - these ports are well known and when your browser makes an http request, it lands on port 80 of the target server. Same it goes for https and 443. If you have chosen to use different ports for http and https traffic respectively, you will need to specify them in address. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example scenario&lt;/strong&gt;: You installed server to process http traffic on port &lt;code&gt;81&lt;/code&gt;. When you want to reach the server from your computer, you will have to access &lt;code&gt;http://127.0.0.1:81&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These ports can be changed, but the process to do so would be out of scope for this tutorial. This &lt;a href="https://stackoverflow.com/a/21914920"&gt;stack overflow post&lt;/a&gt; does a great job at explaining how to do that.&lt;/p&gt;

&lt;p&gt;To summarize - if you followed this tutorial, you achieved two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Told your apache server where to look for scripts when processing requests that are aimed at a specific domain that you specify in &lt;code&gt;httpd-vhosts.conf&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Told your computer to re-route requests to &lt;code&gt;yourdomain.localhost&lt;/code&gt; to &lt;code&gt;127.0.0.1&lt;/code&gt;, also known as localhost.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How does editing &lt;code&gt;httpd-vhosts.conf&lt;/code&gt; works?
&lt;/h3&gt;

&lt;p&gt;Let's have a look at the snippet of code that gets appended to the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;VirtualHost&lt;/span&gt; &lt;span class="err"&gt;*:80&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    DocumentRoot "c:\users\username\source\mysite"
    ServerName danieldusek.localhost
    &lt;span class="nt"&gt;&amp;lt;Directory&lt;/span&gt; &lt;span class="err"&gt;"c:\users\username\source\mysite"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        Require all granted
    &lt;span class="nt"&gt;&amp;lt;/Directory&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/VirtualHost&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First thing that needs an explanation is the port number at line 1: &lt;code&gt;&amp;lt;VirtualHost *:80&amp;gt;&lt;/code&gt;. It is 80, once again, as a side effect of the http=80 convention mentioned above. If you have installed on different port, you need to update it.&lt;/p&gt;

&lt;p&gt;The asterisk infront of it tells the webserver to match all addresses. This can be used when you have multiple named addresses on your web server and want to fallback to the first one in case request does not match any other in the config file. Source: &lt;a href="https://httpd.apache.org/docs/2.4/vhosts/examples.html"&gt;apache.org documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;Directory ...&amp;gt;&lt;/code&gt; are markings in a configuration language used by apache that indicate that everything inside them should be used on a given directory and all of it children directories and files. The things that are enclosed in them are called directives. Source: &lt;a href="https://httpd.apache.org/docs/2.4/mod/core.html#directory"&gt;apache.org documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As for the actual directive used - &lt;code&gt;Require all granted&lt;/code&gt; - this tells the apache server that it should not refuse to serve these pages to any IP requesting them. &lt;/p&gt;

&lt;p&gt;In most scenarios, you will be on a local network without a public IP. So using this broad directive causes that effectively anyone on the same local network can request pages from your webserver. Should you have a public IP address, then anyone on the internet might be able to do that. If this is a concern for you, &lt;a href="http://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require"&gt;read the apache documentation&lt;/a&gt; and look for &lt;code&gt;Require ip [IP]&lt;/code&gt; directive.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;ServerName yourdomain.localhost&lt;/code&gt; is pretty self-explanatory. Requests for given domain will be routed to the folder provided under &lt;code&gt;DocumentRoot&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why should you use &lt;code&gt;.localhost&lt;/code&gt; TLD?
&lt;/h3&gt;

&lt;p&gt;Short answer: Because you should not use an existing TLD.&lt;/p&gt;

&lt;p&gt;Long answer: You have no control over which TLDs will be created in the future, but you can refer to &lt;a href="https://tools.ietf.org/html/rfc2606"&gt;RFC2606, page 2&lt;/a&gt; which lists domains that you can use for private testing. These are &lt;code&gt;.test&lt;/code&gt;, &lt;code&gt;.example&lt;/code&gt;, &lt;code&gt;.invalid&lt;/code&gt;, and &lt;code&gt;.localhost&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does &lt;code&gt;hosts&lt;/code&gt; file update works?
&lt;/h3&gt;

&lt;p&gt;In the second part of the local domain setup we edit the &lt;code&gt;hosts&lt;/code&gt; file and add a &lt;code&gt;127.0.0.1 yourdomain.localhost&lt;/code&gt; line at the end of it. This line maps - as perceived by your computer - the &lt;code&gt;yourdomain.localhost&lt;/code&gt; domain to &lt;code&gt;127.0.0.1&lt;/code&gt; ip (your computer's ip; a localhost). You can put any domain and any IP address there instead and it will work the same way.&lt;/p&gt;

&lt;p&gt;This is very similar to how Domain Name System (DNS) works. DNS is a protocol that maps domain names to IP addresses of computers that host websites the user is trying to reach. It is very convenient for users to type &lt;code&gt;danieldusek.com&lt;/code&gt; instead of remembering that it is hosted on &lt;code&gt;89.221.213.23&lt;/code&gt;. It also gives me flexibility in changing the hosting provider without the need to tell people to now go to different address. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;hosts&lt;/code&gt; file is a local hint for the operating system that is used with higher priority. If a domain name is found in the &lt;code&gt;hosts&lt;/code&gt; file, the system will not try to get the information about target IP address from the DNS server, but will use the one specified. &lt;/p&gt;

&lt;p&gt;And that's about it.&lt;/p&gt;

</description>
      <category>virtualhosts</category>
      <category>tutorial</category>
      <category>localhostdomains</category>
      <category>php</category>
    </item>
    <item>
      <title>Escaping Improperly Sandboxed Iframes</title>
      <dc:creator>Daniel Dušek</dc:creator>
      <pubDate>Wed, 06 May 2020 11:39:17 +0000</pubDate>
      <link>https://forem.com/dusekdan/escaping-improperly-sandboxed-iframes-1ak7</link>
      <guid>https://forem.com/dusekdan/escaping-improperly-sandboxed-iframes-1ak7</guid>
      <description>&lt;p&gt;Thanks to iframe's sandbox attribute, it is possible to specify restrictions applied on content displayed inside the iframe. The documentation &lt;strong&gt;strongly discourages&lt;/strong&gt; from using both &lt;code&gt;allow-scripts&lt;/code&gt; and &lt;code&gt;allow-same-origin&lt;/code&gt; values due to security risks it may introduce. In this blogpost, I am going to explain and demonstrate why.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe"&gt;Mozilla's developer documentation&lt;/a&gt; on &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;, you can find the following remark related to &lt;code&gt;allow-scripts&lt;/code&gt; and &lt;code&gt;allow-same-origin&lt;/code&gt; values of the &lt;code&gt;sandbox&lt;/code&gt; attribute:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When the embedded document has the same origin as the embedding page, it is &lt;strong&gt;strongly discouraged&lt;/strong&gt; to use both &lt;code&gt;allow-scripts&lt;/code&gt; and &lt;code&gt;allow-same-origin&lt;/code&gt;, as that lets the embedded document remove the sandbox attribute — making it no more secure than not using the sandbox attribute at all.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When I first read this note, I thought that escaping will be fairly straightforward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accessing &lt;code&gt;window.parent&lt;/code&gt; from the page inside the iframe,&lt;/li&gt;
&lt;li&gt;getting the iframe element reference via &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction"&gt;Document Object Model (DOM)&lt;/a&gt; functions, such as &lt;code&gt;.getElementById()&lt;/code&gt; or &lt;code&gt;.getElementsByTagName()&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;removing the iframe's &lt;code&gt;sandbox&lt;/code&gt; element via &lt;code&gt;.removeAttribute("sandbox")&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;calling &lt;code&gt;alert()&lt;/code&gt; function to create a modal window despite &lt;code&gt;allow-modals&lt;/code&gt; was not set for the iframe, hence escaping from the iframe's restrictions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Unfortunatelly, this rather naïve approach does not work. From my experiments, the reason for that is the fact that browsers apply restrictions on the &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt;'s content &lt;strong&gt;when loading the page&lt;/strong&gt;. When I subsequently change or remove the sandbox attribute and then call "illegal" functions, browser will still block them.&lt;/p&gt;

&lt;p&gt;On the following lines I will demonstrate the simplest solution I have been able to come up with. Before I get to it, let me explain the terminology I am using - a &lt;code&gt;child page&lt;/code&gt; refers to a page that is being displayed in the &lt;code&gt;iframe&lt;/code&gt;, while a &lt;code&gt;parent page&lt;/code&gt; refers to a page that contains the &lt;code&gt;&amp;lt;iframe&amp;gt;&lt;/code&gt; element. &lt;/p&gt;

&lt;p&gt;Let's have a look at parent page, in &lt;code&gt;index.html&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;PoC - Discouraged combination of sandbox attribute values (parent page)&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;iframe&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"kid.htm"&lt;/span&gt; &lt;span class="na"&gt;sandbox=&lt;/span&gt;&lt;span class="s"&gt;"allow-same-origin allow-scripts"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"escapeMe"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the page that we need to modify from the child page (&lt;code&gt;kid.htm&lt;/code&gt;). To escape the iframe as I outlined above, I will be popping an &lt;code&gt;alert()&lt;/code&gt; from the child page inside the parent. &lt;/p&gt;

&lt;p&gt;One of the simplest solutions I have been able to come up with is to simply create a new iframe in place of the old one, and let the child page know it has been loaded in unrestricted mode. I will walk you through the process step by step.&lt;/p&gt;

&lt;p&gt;First, I will obtain a reference to the parent window and verify the &lt;code&gt;iframe&lt;/code&gt; in question still exists. If the original iframe &lt;strong&gt;is missing&lt;/strong&gt;, it means that the child page is loaded from another &lt;code&gt;iframe&lt;/code&gt; - the one we are going to create in step 2 as a replacement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;escapeMe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Create replacement iframe&lt;/span&gt;
    &lt;span class="c1"&gt;// 2. Delete the old one&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// When the original iframe no longer exists, we can assume&lt;/span&gt;
    &lt;span class="c1"&gt;// it is possible to execute our code without restrictions&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This should not have happened.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the second step, I expand the body of the condition to create unrestricted iframe and to remove the original one with restrictions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;escapeMe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// 1. Create replacement iframe&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;replacement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;iframe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;replacement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;src&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;kid.htm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;replacement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;escapedAlready&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;replacement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Delete the old one&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;escapeMe&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parentNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;removeNode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// When the original iframe no longer exists, we can assume&lt;/span&gt;
    &lt;span class="c1"&gt;// it is possible to execute our code without restrictions&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This should not have happened.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when the parent page loads the child page into the &lt;code&gt;iframe&lt;/code&gt; with &lt;code&gt;allow-scripts&lt;/code&gt; and &lt;code&gt;allow-same-origin&lt;/code&gt;, the child page manages to escape the original iframe's restrictions and execute its code.&lt;/p&gt;

&lt;p&gt;Both of the files I used in this demonstration are &lt;a href="https://github.com/dusekdan/RandomSecurity/tree/master/iframeSandboxDiscouragedCombination"&gt;available on my Github&lt;/a&gt;. You can also try for yourself on my &lt;a href="https://dusekdan.github.io/RandomSecurity/iframeSandboxDiscouragedCombination/"&gt;Github Pages&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Can you think of a simpler way to escape? Please let me know!&lt;/p&gt;

</description>
      <category>security</category>
      <category>iframe</category>
      <category>sandbox</category>
      <category>escaping</category>
    </item>
  </channel>
</rss>
