prism

2013-08-21

expressjs conditional CSRF

現在一般基本的網路服務,為了安全性,會加上 CSRF 的保護,如果

用 ajax 的方式傳資料,一般可以跳過,或是,加在 ajax 的 header 裏面


在 Django 下面的話,如果不檢查的話,會加個 decorator @csrf_exempt

在 expressjs 下面,加 ajax header 是可行的,不過懶一點,目前 API 路徑的
請求,先跳過,以下筆記



 /**
 * Module dependencies.
 */

var express = require('express')
  , csrf = express.csrf()
  , fs = require('fs')
  , mongoStore = require('connect-mongo')(express)
  , flash = require('connect-flash')
  , helpers = require('view-helpers')
  , mongoose = require('mongoose')
  , http = require('http')
  , path = require('path')
  , i18n = require('i18n');


  // I olny cut the block of csrf setup

  // adds CSRF support
  if (process.env.NODE_ENV !== 'test') {

    // conditinal CSRF
    
    var conditionalCSRF = function(req, res, next){
      
      // bypass urlpath start with api and moreurl

      if (! /^\/(api|moreurl)/.test(req.path)){
        csrf(req, res, next);
      } else {
        next();
      }
    }
    //app.use(express.csrf());
    app.use(conditionalCSRF);
  }

  // This could be moved to view-helpers :-)
  app.use(function(req, res, next){
    res.locals.csrf_token = req.session._csrf
    next()
  })


參考
http://stackoverflow.com/questions/13516898/disable-csrf-validation-for-some-requests-on-express

http://stackoverflow.com/questions/11200068/how-to-implement-csrf-protection-in-ajax-calls-using-express-js-looking-for-com

2013-08-20

Full Stack

部落格,以當筆記,日記的好處來說,他是比較老派的東西

資料永遠找的到,再來就是因為是舊東西,官方不會一直改介面,或是功能

算算先前自己做玩具時,都沒有好好紀錄,最後結束剩的就真的只有

回憶,這一次,先不管,是好,是壞,會成功,會失敗,都要來紀錄,這就是對軟體的愛吧

今天是颱風假,就說說這一次用的 Stack 好了

白天的工作職務,算是硬派的系統分析,及 Python 程式撰寫

身為熱血工程師 (雖然年紀不小了,不過好歹,血還是熱的),晚上寫的東西

想試試和白天不同的,所以 backend 就先這樣吧

Backend


nginx

node.js

Golang

mongodb


Front End

這一部份有些還沒有想好,大概就是會有


Web (html5,很想直接放生 IE 舊版的,不過會做嵌入的 js 或是 iframe ,不知道,這樣會不會讓人覺得太大牌)

Mobile Web ,基本上和 Web 一樣

Android App (就自己先刻)

iPhone App (沒有 Mac book Air 先放棄,其他先做 已敗家)

Real Time


就 nginx push stream module 先頂一下吧

collaboration


https://bitbucket.org/ 的免費方案加減用了

原型

要做新玩具,當然就是用新技術來做才叫潮

所以馬上就用最新版的 bootstrap 3 還有

最新的 angular.js 1.2.0rc1 AngularJS

不由的抱怨下,bootstrap 一下子把 icon 移出去,一下子又把 svg icon 移回來,還蠻機車的,最

白痴的就是, namespace 改來改去,但是我還是很不爭氣的繼續用了 ( 加入他們),前端的 icon fonts 部份,真的

推荐一下 http://fontello.com/ 真是有潔癖的前端工程師救星

可以把你需要的 svg icon 選起來,客製成自己專屬的 icon font,還蠻乾淨的

Angular.js 1.2.0rc1 部份,也是向更大一包邁進,和原本 1.1.5 系列不同的是,模組切更多,有用到

$routeProvider, $locationProvider 的把 angular-route.min.js 加入頁面,app 記得要 module inject

像是我開始做的 dashboard 原型這樣


var app = angular.module('homeApp', ['ngRoute']);


PS: 用新技術不是為了潮啦,現在誰不做 mobile app,server side 的部份搞 API 大融合,client 部份,就可以 Web App , Desktop App, App (iOS, Android) 3P,4P 皆宜

發想筆記

最近有不自量力的投了一份高薪 SA 的職缺,不過被打槍了,也看到了自己不足的地方,就算你可以很快的學會

機會還是會保留給已經會,且有經驗的人


我想該是為自己努力的時候了,尤其是身為一個軟體開發相關的人員,相信這還是最終要走的道路,你可以熱愛

創造有用,有趣的軟體,可是在台灣的職場上,要找尋,可以把軟體開發,當成終身樂趣的工作,少之又少

應該可以說沒有,沒有的東西,除了自己創造,應該沒有其他辦法

先前,自己有做過一些玩具,網路名片,房地產,股市網站,這一次,想當個 copy cat 好了,就來做類似

http://registrano.com/ 的東西,我想解決的問題點,除了報名,想要處理報到,還有排隊

的問題,相信一個報名的工具,應該很多地方都可以用,只用在活動報名有點可惜

目前,進行第一步,就先買個地點便宜,可以做商業登記的點,作為辦公室好了,既然是網路業,公司登記先省了

主機,網域,應該都是美國的,買好的辦公室,就先當永久住址就好,辦公室還是先租人 :-)

2013-08-16

Install nginx-push-stream-module

要在 Debian 上面安裝,標準官方的 nginx 加上 https://github.com/wandenberg/nginx-push-stream-module 模組

做個筆記,這裡的 nginx 並不是 Debian 官方的 deb,是由 nginx 官方釋出的 deb

裝 官方 nginx ,加個 sources.list 先


# nginx
# wget http://nginx.org/keys/nginx_signing.key
# sudo apt-key add nginx_signing.key
#
deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx



#裝 build depend package

apt-get build-dep nginx

#下載 source
apt-get source nginx

#git clone nginx module ,照官方文件設定環境

git clone https://github.com/wandenberg/nginx-push-stream-module
NGINX_PUSH_STREAM_MODULE_PATH=$PWD/nginx-push-stream-module

# 這時候資料夾,應該有 nginx-1.4.2 的資料夾,看現在新的套件版本

cd nginx-1.4.2

# 編輯一下 vim debian/rules


override_dh_auto_build:
dh_auto_build
mv objs/nginx objs/nginx.debug
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
$(WITH_SPDY) \
--with-cc-opt="$(CFLAGS)" \
--with-ld-opt="$(LDFLAGS)" \
--add-module=../nginx-push-stream-module \ # 就加這一行而已
--with-ipv6
dh_auto_build


#執行

dpkg-buildpackage -b

這樣就包好,可以安裝測試了,不想要更新的話,就把他版本 hold 住


最近試了, socket.iosockjs 還是都沒有讓人覺得很滿意

看看國外 disqus 推荐的 https://github.com/wandenberg/nginx-push-stream-module

還蠻簡單用的,而且複雜性低,不用多加掛一個 socket server ,就交給 nginx 負責 pub 及 sub 的部份,架構算乾淨,自己的下一個玩具,就用他了