Skip to content

Commit

Permalink
Merge branch 'feature/security' of https://github.com/NetCommons3/Wys…
Browse files Browse the repository at this point in the history
…iwyg into feature/security
  • Loading branch information
otokomae committed Apr 27, 2016
2 parents 3c6bb52 + fdbeeeb commit d83e7b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
24 changes: 21 additions & 3 deletions Controller/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ class FileController extends WysiwygAppController {
public function beforeFilter() {
parent::beforeFilter();

// TinyMCE のアップロードフォームに Token埋め込み方法が未解決のため
// ひとまずセキュリティ Component から uploadアクションを除外する
$this->Security->unlockedActions = array('upload', 'download');
// アップロードでは CSRFトークン対応は行うが、フォーム改ざんチェックは行わない
$this->Security->validatePost = false;
}

/**
Expand Down Expand Up @@ -181,4 +180,23 @@ protected function _isUploadedFile($params) {
}
return false;
}

/**
* csrfToken method
*
* @return void
*/
public function csrfToken() {
$security = $this->Components->load('Security');
$security->generateToken($this->request);

$data = array(
'_Token' => array(
'key' => $this->request->params['_Token']['key']
)
);

$this->set(compact('data'));
$this->set('_serialize', array('data'));
}
}
3 changes: 3 additions & 0 deletions View/Helper/WysiwygHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public function wysiwygScript() {
'file_upload_path' => $this->NetCommonsHtml->url('/wysiwyg/file/upload'),
'image_upload_path' => $this->NetCommonsHtml->url('/wysiwyg/image/upload'),

'csrfTokenPath' =>
$this->NetCommonsHtml->url('/wysiwyg/file/csrfToken.json'),

// mobile判別
'is_mobile' => Configure::read('isMobile'),
];
Expand Down
48 changes: 30 additions & 18 deletions webroot/js/wysiwyg_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,34 @@ var NC3_APP = new (function nc3WysiwygApp() {
// API(formDataを使用)
/////////////////////////////////////////////////
/**
* トークンの確認(TODO)
* トークンの確認
* success時にアップロードの実行
*/
var __checkToken = function() {
var url, formData;
var __getCsrfToken = function(url, formData, onsuccess, onerr, name, isDEBUG) {
var u = url;
var fd = formData;
var onss = onsuccess;
var oner = onerr;
var n = name;

__httpReq(
'get',
tinymce.editors[0].settings.nc3Configs.csrfTokenPath,
{},
function(res) {
// 取得した csrfToken をフォームデータとして作成する
fd.append('data[_Token][key]', res.data._Token.key);

// アップロードの実行
__httpReq('post', u, fd, onss, oner, n);
},
function(res) {
},
'getCsrfToken'
);
};

/**
* 画像のアップロード
*/
Expand All @@ -127,15 +150,10 @@ var NC3_APP = new (function nc3WysiwygApp() {
onsuccess();
return false;
}

var url = __appURLs.uploadImage(roomId);
__httpReq(
'post',
url,
formData,
onsuccess,
onerror,
'uploadImage'
);
__getCsrfToken(url, formData, onsuccess, onerr, 'uploadImage', isDEBUG);

};
/**
* ファイルのアップロード
Expand All @@ -145,15 +163,9 @@ var NC3_APP = new (function nc3WysiwygApp() {
onsuccess(DUMMY_DATA.upload_file);
return false;
}

var url = __appURLs.uploadFile(roomId);
__httpReq(
'post',
url,
formData,
onsuccess,
onerror,
'uploadFile'
);
__getCsrfToken(url, formData, onsuccess, onerr, 'uploadFile', isDEBUG);
};
/**
* 書籍検索(Google books API)
Expand Down

0 comments on commit d83e7b1

Please sign in to comment.