Aug 29, 2013

Overview of Design Patterns

Overview of Design Patterns

As per WIKI, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

Why do we need them?
Well, rather than having to re-invent the wheel, we can re-use an existing solution recognized across organizations as something that simply works. Since it is well documented, it is comparatively easier to solve the problem and solution in our mind.

How many are there? 
There is no definitive list of the patterns. People working on various challenges document them and spread the word about a possible solution. Over a period of time, this solution is recognized as a standard pattern for solving a particular problem. Obviously the most common patterns are the ones for Gang of Four or you can also look at Wikipedia for design patterns.

Design pattern is divided based on purpose and scope.


Purpose
Creational
Structural
Behavioural
Scope
Class
Factory Method
Adapter
Interpreter
Template Method
Object
Abstract Factory
Builder
Prototype
Singleton
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Chain of Responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor





























Design patterns are divided into three fundamental groups.
  • Behavioral patterns are those patterns that are most specifically concerned with communication between objects.
  • All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged.
  • Structural patterns describe how classes and objects can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe how inheritance can be used to provide more useful program interfaces. Object patterns, on the other hand, describe how objects can be composed into larger structures using object composition, or the inclusion of objects within other objects. 

Each fundamental groups are further divided into multiple ways:

  Creational Patterns
  Creates an instance of several families of classes
  Builder
  Separates object construction from its representation
  Creates an instance of several derived classes
  A fully initialized instance to be copied or cloned
  A class of which only a single instance can exist

  Structural Patterns
  Adapter
  Match interfaces of different classes
  Bridge
  Separates an object’s interface from its implementation
  A tree structure of simple and composite objects
  Add responsibilities to objects dynamically
  Facade
  A single class that represents an entire subsystem
  A fine-grained instance used for efficient sharing
  Proxy
  An object representing another object

  Behavioral Patterns
  A way of passing a request between a chain of objects
  Command
  Encapsulate a command request as an object
  A way to include language elements in a program
  Sequentially access the elements of a collection
  Defines simplified communication between classes
  Memento
  Capture and restore an object's internal state
  A way of notifying change to a number of classes
  State
  Alter an object's behavior when its state changes
  Encapsulates an algorithm inside a class
  Defer the exact steps of an algorithm to a subclass
  Visitor
  Defines a new operation to a class without change


Useful websites:
GoF Patterns
DoFactory



Aug 27, 2013

Anti-Cross Site Scripting Library (AntiXSS)

Before understanding Anti-Cross Site Scripting Library (AntiXSS) let us understand Cross-Site Scripting(XSS).

Cross-site Scripting (XSS)
Cross-Site Scripting attacks are a type of injection problem, in which malicious scripts are injected into the otherwise benign and trusted web sites. Cross-site scripting (XSS) attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user in the output it generates without validating or encoding it.
An attacker can use XSS to send a malicious script to an unsuspecting user. The end user’s browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by your browser and used with that site. These scripts can even rewrite the content of the HTML page.

Cross-Site Scripting (XSS) attacks occur when:
1. Data enters a Web application through an untrusted source, most frequently a web request.
2. The data is included in dynamic content that is sent to a web user without being validated for malicious code.

The malicious content sent to the web browser often takes the form of a segment of JavaScript, but may also include HTML, Flash or any other type of code that the browser may execute. The variety of attacks based on XSS is almost limitless, but they commonly include transmitting private data like cookies or other session information to the attacker, redirecting the victim to web content controlled by the attacker, or performing other malicious operations on the user's machine under the guise of the vulnerable site.



AntiXSS helps you to protect your current applications from cross-site scripting attacks, at the same time helping you to protect your legacy application with its Security Runtime Engine. AntiXSS incorporates radically and innovatively rethought features, offering you a newer, more powerful weapon against the often employed cross-site scripting (XSS) attack. AntiXSS gives you:

  • Improved Performance. AntiXSS has been completely rewritten with performance in mind, and yet retains the fundamental protection from XSS attacks that you have come to rely on for your applications.
  • Secure Globalization. The web is a global market place, and cross-site scripting is a global issue. An attack can be coded anywhere, and Anti-XSS now protects against XSS attacks coded in dozens of languages.
  • Standards Compliance. AntiXSS is written to comply with modern web standards. You can protect your web application without adversely affecting its UI.


How it works?
Proper output encoding and good input validation will fix the XSS issue. For output encoding use AntiXSS Library for its comprehensive encoding capabilities. AntiXSS works by looking at all the characters in the input and encoding characters not in the whitelist.


XSS Vulnerability:
Response.Write(Request.Params["input"]);

To prevent XSS, we need to use below code (AntiXSS):
AntiXss.UrlEncode(TextBox1.Text)

AntiXss.HtmlAttributeEncode(TextBox1.Text)

AntiXss.XmlEncode(TextBox1.Text)

AntiXss.JavaScriptEncode(item)


You can download AntiXSS library from here.


Aug 26, 2013

OWIN and Katana



OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

An OWIN-compatible Web server is responsible for populating the environment dictionary with data such as the body streams and header collections for an HTTP request and response. It is then the responsibility of the application or framework components to populate or update the dictionary with additional values and write to the response body stream.

What is the difference between OWIN/Katana?

OWIN defines the structure and requirements of HTTP request and response interactions. The assembly codifies the definitions in the spec to allow you to avoid using type aliases in all of your files.

Katana is a flexible set of components for building and hosting OWIN-based web applications. It supports the Microsoft hosts and frameworks and provides a command line tool for running OWIN applications from the command line.

You need below components: 
Microsoft.Owin.Host.HttpListener package from NuGet. 

This is supported in Visual Studio 2012 and above.