MyNixOS website logo
Description

CORS for WAI.

This package provides an implemenation of Cross-Origin resource sharing (CORS) for Wai that aims to be compliant with http://www.w3.org/TR/cors.

Build Status

Cross-Origin Resource Sharing (CORS) For Wai

This package provides a Haskell implementation of CORS for WAI that aims to be compliant with http://www.w3.org/TR/cors.

Note On Security

This implementation doesn't include any server side enforcement. By complying with the CORS standard it enables the client (i.e. the web browser) to enforce the CORS policy. For application authors it is strongly recommended to take into account the security considerations in section 6.3 of the CORS standard. In particular the application should check that the value of the Origin header matches the expectations.

Websocket connections don't support CORS and are ignored by the CORS implementation in this package. However Websocket requests usually (at least for some browsers) include the @Origin@ header. Applications are expected to check the value of this header and respond with an error in case that its content doesn't match the expectations.

Installation

Assuming the availability of recent versions of GHC and cabal this package is installed via

cabal update
cabal install wai-cors

Usage

The function 'simpleCors' enables support of simple cross-origin requests. More advanced CORS policies can be enabled by passing a 'CorsResourcePolicy' to the 'cors' middleware.

The file examples/Scotty.hs shows how to support simple cross-origin requests (as defined in http://www.w3.org/TR/cors) in a scotty application.

{-# LANGUAGE OverloadedStrings #-}

module Main
( main
) where

import Network.Wai.Middleware.Cors
import Web.Scotty

main :: IO ()
main = scotty 8080 $ do
    middleware simpleCors
    matchAny "/" $ text "Success"

The result of following curl command will include the HTTP response header Access-Control-Allow-Origin: *.

curl -i http://127.0.0.1:8080 -H 'Origin: 127.0.0.1' -v

Documentation for more general usage can be found in the module Network.Wai.Middleware.Cors.

Test

In order to run the automated test suite PhantomJS (at least version 2.0) must be installed in the system.

cabal install --only-dependencies --enable-tests
cabal test --show-details=streaming

If PhantomJS is not available the tests can be exectued manually in a modern web-browser as follows.

Start the server application:

cd test
ghc -main-is Server Server.hs
./Server

Open the file test/index.html in a modern web-browser. On page load a Javascript script is exectued that runs the test suite and prints the result on the page.

Metadata

Version

0.2.7

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows