<?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>DEV Community: Deepak</title>
    <description>The latest articles on DEV Community by Deepak (@programmingmuffin).</description>
    <link>https://dev.to/programmingmuffin</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%2F861039%2Faa565d7a-42af-4f56-8124-437086d01e78.png</url>
      <title>DEV Community: Deepak</title>
      <link>https://dev.to/programmingmuffin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/programmingmuffin"/>
    <language>en</language>
    <item>
      <title>Editing the Java compiler</title>
      <dc:creator>Deepak</dc:creator>
      <pubDate>Sat, 04 Mar 2023 06:03:27 +0000</pubDate>
      <link>https://dev.to/programmingmuffin/editing-the-java-compiler-39ab</link>
      <guid>https://dev.to/programmingmuffin/editing-the-java-compiler-39ab</guid>
      <description>&lt;p&gt;Compilers always fascinated me throughout my career. I suppose it was the ability of the compiler to take a high level piece of code and quickly translate it into something that executes. I have tried writing a compiler from scratch once (for my own programming language) and it didn't go well but I was able to learn a lot from it. Fast forward to today, I finally edited the "openjdk17" / "corretto-17" java compiler to add my own grammar and functionality.&lt;/p&gt;

&lt;p&gt;The "javac" command that you generally use to compile high level java code to a .class file or bytecode, is actually surprisingly written in java. This is known as a bootstrap compiler or a "self compiling compiler". It's a compiler written for a language in that language.&lt;/p&gt;

&lt;p&gt;The Javac compiler has several stages at a detailed level but the main steps of compiling still adheres to the compiler theory / compiler design concepts. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexical Analysis (Tokenizing)&lt;/li&gt;
&lt;li&gt;Syntax Analysis (Parsing) (Generates a parse tree)&lt;/li&gt;
&lt;li&gt;Semantic Analysis (Type checking, Dead code analysis, etc)&lt;/li&gt;
&lt;li&gt;Intermediate Code Generation (optional)&lt;/li&gt;
&lt;li&gt;Target Code Generation (Bytecode)&lt;/li&gt;
&lt;li&gt;Code Optimization&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the parse tree is generated, every other stage is a visitor. It follows the visitor design pattern. There is a Flow visitor (checks Dead code), There is an Enter visitor (which collects Symbols like methods, classes). There is a Attr visitor which does Type checking. Etc.&lt;/p&gt;

&lt;p&gt;To get started on editing the javac compiler, you need to first set up the openjdk codebase locally. Unfortunately, I couldn't find a good set of tools for compiler development but I found vscode to be the easiest in this case. IntelliJ also works but I prefer vscode here. &lt;a href="https://openjdk.org/groups/build/doc/building.html" rel="noopener noreferrer"&gt;This page describes how to setup the codebase locally.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The feature I am trying to add here, is the "Javascript's Option Chain" operator or the "?." operator. After navigating through the code, it appears that the grammar has to be written alongside the ternary operator's grammar code. This piece of code is present in the &lt;code&gt;JavacParser.java&lt;/code&gt; file. Here's where all parsing to a &lt;code&gt;JCTree&lt;/code&gt; happens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt; &lt;span class="cm"&gt;/** Expression1Rest = ["?" Expression ":" Expression1]
     */&lt;/span&gt;
    &lt;span class="nc"&gt;JCExpression&lt;/span&gt; &lt;span class="nf"&gt;term1Rest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JCExpression&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;QUES&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;nextToken&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="c1"&gt;// option chaining&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;DOT&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;DOT&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;ident&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ident&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="nc"&gt;JCExpression&lt;/span&gt; &lt;span class="n"&gt;returnable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Conditional&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                        &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Parens&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Binary&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;optag&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TokenKind&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;EQEQ&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Literal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TypeTag&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;BOT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;)),&lt;/span&gt;
                        &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Literal&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TypeTag&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;BOT&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt; &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Select&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ident&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;term1Rest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;returnable&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
            &lt;span class="c1"&gt;// ternary&lt;/span&gt;
            &lt;span class="nc"&gt;JCExpression&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;term&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;accept&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;COLON&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;JCExpression&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;term1&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;F&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;at&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;Conditional&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the grammar is accepted by calling &lt;code&gt;accept(DOT)&lt;/code&gt;. DOT is a token defined in &lt;code&gt;Tokens.java&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The way this functionality works, is by replacing the &lt;code&gt;JCFieldAccess Select(...)&lt;/code&gt; subtree with a custom tree similar to this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ident&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how option chaining works. &lt;/p&gt;

&lt;p&gt;An example of how this feature works is like so:&lt;/p&gt;

&lt;p&gt;Code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;wheel1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;w1&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="nc"&gt;Integer&lt;/span&gt; &lt;span class="n"&gt;wheel2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;w2&lt;/span&gt;&lt;span class="o"&gt;?.&lt;/span&gt;&lt;span class="na"&gt;x&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value of wheel1 is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;wheel1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
                &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"value of wheel2 is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;wheel2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="nc"&gt;Wheel&lt;/span&gt; &lt;span class="n"&gt;w1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="nc"&gt;Wheel&lt;/span&gt; &lt;span class="n"&gt;w2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Wheel&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Wheel&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
                &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Wheel&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F2zwp1z4bz60lccag8ka2.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%2F2zwp1z4bz60lccag8ka2.png" alt=" " width="717" height="148"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This still isn't perfect and has its own problems but regardless it was a quite interesting journey of understanding how the compiler is written. I was able to learn a lot of things from this.&lt;/p&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://openjdk.org/groups/compiler/doc/hhgtjavac/index.html" rel="noopener noreferrer"&gt;https://openjdk.org/groups/compiler/doc/hhgtjavac/index.html&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  My commit:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/corretto/corretto-17/commit/37b125992c7d2b6fb55216e553168bf7b8a3a4b4" rel="noopener noreferrer"&gt;https://github.com/corretto/corretto-17/commit/37b125992c7d2b6fb55216e553168bf7b8a3a4b4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>agile</category>
      <category>productivity</category>
      <category>career</category>
    </item>
    <item>
      <title>Less boring way to build an MVP</title>
      <dc:creator>Deepak</dc:creator>
      <pubDate>Sat, 04 Jun 2022 05:53:14 +0000</pubDate>
      <link>https://dev.to/programmingmuffin/less-boring-way-to-build-an-mvp-n28</link>
      <guid>https://dev.to/programmingmuffin/less-boring-way-to-build-an-mvp-n28</guid>
      <description>&lt;p&gt;Whenever you want to build a product, you never start out with building the entire product. Often times your first goal is to build a &lt;strong&gt;Minimum Viable Product (MVP)&lt;/strong&gt;. This is done so that you can test the &lt;strong&gt;Product Market Fit&lt;/strong&gt; before you even build the entire complex project. &lt;/p&gt;

&lt;p&gt;However, building an MVP isn't fun if you're uncertain that it will give any good results at all. In fact, it's very draining. I thrive on certain good results and uncertainty doesn't help it.&lt;/p&gt;

&lt;p&gt;The first step to enhance the experience of building an MVP is by building the end goal right away. It sounds weird but it totally works. &lt;/p&gt;

&lt;p&gt;Let's suppose that we're trying to build a social media website where people can post memes in their pages, and others add comments and stuff. The traditional way to go about this is like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Build the login UI (Nothing fun yet)&lt;/li&gt;
&lt;li&gt;Setup the database&lt;/li&gt;
&lt;li&gt;Build the login backend&lt;/li&gt;
&lt;li&gt;Setup the tables for pages&lt;/li&gt;
&lt;li&gt;Setup the tables for posts&lt;/li&gt;
&lt;li&gt;Build the UI for the posts and images (Still can't use it because it's tied to the backend and the backend isn't implemented yet)&lt;/li&gt;
&lt;li&gt;And so on and so forth...&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most people would feel like quiting at &lt;strong&gt;step 3&lt;/strong&gt;. This is because we haven't reached our end goal yet and we're uncertain if it works and we don't even know how the end product would look like. Let's try a slightly better approach. Our main goal here is to let people create pages, post memes and add comments on them. Let's do that first.&lt;/p&gt;

&lt;blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Build the end goal UI functionality. (Allow people to upload things and view whatever is uploaded (don't save it. You don't have to). Mock any APIs that you may need.&lt;/li&gt;
&lt;li&gt;Build other less fun UI components like login page, etc.&lt;/li&gt;
&lt;li&gt;Slowly build the backend component by component for all of this and make them better.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;I find this to be a much better way to go about prototyping products. At &lt;strong&gt;step 1&lt;/strong&gt;, you already have what you're looking for which is going to inspire you. You can play around with what's going to be the future MVP which is going to be fully functional. At &lt;strong&gt;step 2&lt;/strong&gt;, it is less boring considering you already have your end goal. All you're doing is just adding more UI components to your end goal. At &lt;strong&gt;step 3&lt;/strong&gt;, it would feel like you're adding upgrades or patches to things that are already present.&lt;/p&gt;

&lt;p&gt;Even though this doesn't solve the entire uncertainty problem, it is still much better than the traditional approach to go about prototyping products. Let me know if you have tried this and I will look forward to finding better ways of building an MVP.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Confused Deputy Attack in OAuth 2.0</title>
      <dc:creator>Deepak</dc:creator>
      <pubDate>Wed, 11 May 2022 20:33:09 +0000</pubDate>
      <link>https://dev.to/programmingmuffin/confused-deputy-attack-in-oauth-20-31fh</link>
      <guid>https://dev.to/programmingmuffin/confused-deputy-attack-in-oauth-20-31fh</guid>
      <description>&lt;p&gt;If you were to build an app or a website today for the general public, it's very likely that you'll be implementing an OAuth based login system. If you don't know what OAuth is (It is summarised in this blog), I recommend you to read the original RFC of OAuth 2.0 (&lt;a href="https://datatracker.ietf.org/doc/html/rfc6749"&gt;RFC 6749&lt;/a&gt;). In short, OAuth login systems look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KJNl82Gn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38cfgb2lxdal399lv60a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KJNl82Gn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38cfgb2lxdal399lv60a.png" alt="Image description" width="646" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the user clicks on &lt;strong&gt;Login with X&lt;/strong&gt; or anything similar, this is what happens in the background (According to RFC 6749 but with one tweak being "server calls the user_details API", for our example here.):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9jUkSBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7ce7n0j8ohhvl7gwsml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9jUkSBN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7ce7n0j8ohhvl7gwsml.png" alt="Image description" width="880" height="570"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the only thing that's being checked by the OAuth provider is the token. There are no extra fields that's being used to check things. Let's say that the scope for which the client is registered, is "profile_info" and returns us the email ID of the user.&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;confused deputy attack&lt;/strong&gt; comes into the picture.&lt;/p&gt;

&lt;p&gt;Suppose you registered a new client with the name "Normal Client", client_id 1 and client_secret 123, and another client with the name "Attacker Client", client_id 2 and client_secret 456. Let's say that for some reason, &lt;a href="mailto:alice@x.com"&gt;alice@x.com&lt;/a&gt; logged into &lt;strong&gt;Normal Client&lt;/strong&gt; which generated a token for the Normal Client, and received access to delete something. Let's also say that for some reason, &lt;a href="mailto:alice@x.com"&gt;alice@x.com&lt;/a&gt; logged into &lt;strong&gt;Attacker Client&lt;/strong&gt; generating a token for the Attacker Client. Let's say that the Normal Client's server is the &lt;strong&gt;Normal Server&lt;/strong&gt; and that the Attacker Client's server is the &lt;strong&gt;Attacker Server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Since there are no additional checks on the token, the &lt;strong&gt;Attacker Server&lt;/strong&gt; could use this token generated for the Attacker Client, which is not identical to the &lt;strong&gt;Normal Client's&lt;/strong&gt; token, to gain access as &lt;a href="mailto:alice@x.com"&gt;alice@x.com&lt;/a&gt; to &lt;strong&gt;Normal Server&lt;/strong&gt;. This will allow the Attacker Server to gain access they weren't given.&lt;/p&gt;

&lt;h4&gt;
  
  
  Solution:
&lt;/h4&gt;

&lt;p&gt;Anything that can add additional validation to the token either at the OAuth provider's side or the server's side will make things a lot more secure and prevent Confused Deputy Attacks. Examples of this are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generating JWT tokens instead of normal tokens and embed information like client_id within the token. Since nobody can alter the state of the JWT token, the client_id will not be tampered with. This client_id can be checked at the server's end or at the OAuth Provider's end.&lt;/li&gt;
&lt;li&gt;Using a flag based scope. Even though scopes are meant to describe the level of information that is given to the authorising server (profile_info, profile_picture, contacts), the RFC doesn't really restrict the usage of scope to this. Therefore scopes can be used to restrict access. Although, this becomes a problem if the OAuth provider is widely used.&lt;/li&gt;
&lt;li&gt;Sending client_id in the user_details API or the API that validates the token. This doesn't follow the OAuth RFC but it still can be done.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Confused_deputy_problem"&gt;https://en.wikipedia.org/wiki/Confused_deputy_problem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://security.stackexchange.com/questions/66077/oauth-implicit-flow-and-confused-deputy-problem"&gt;https://security.stackexchange.com/questions/66077/oauth-implicit-flow-and-confused-deputy-problem&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://datatracker.ietf.org/doc/html/rfc6749"&gt;https://datatracker.ietf.org/doc/html/rfc6749&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
