6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Product for Google IoT Solutions - 5分でFirebaseのセキュリティとルールがわかる

Last updated at Posted at 2016-03-29

背景

問題

  • 以前の説明の例に、以下の記述があります。
 var ref = new Firebase("https://{app_name}.firebaseio.com");
  • app_nameがそのままに公開されたとなると、誰でもapp_nameを使って、firebaseへアクセスできてしまいます。

説明

無題の図形描画 (1).png

  • 例えば、siteAのapp_nameで、siteAのFirebaseへアクセスして、userAのageを30に設定しました。
  • 悪い人は、siteAのapp_nameを、siteBに使って、userAのageを20に改ざんできます。
  • 結果、Firebaseが危険に晒されます。

siteA

コード

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <title>firebase test</title>
  <script src="https://cdn.firebase.com/js/client/2.4.1/firebase.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
  <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<button id="login"  class="btn btn-primary">Firebase Twitter</button>
  <p id="result"></p>
  <script>
  var ref = new Firebase("https://{app_name}.firebaseio.com");

  $("#login").click(function () {
      ref.authWithOAuthPopup("twitter", function (error, authData) {
      if (error) {
        $("#result").text("Login Failed!");
      } else {
        $("#result").text("Authenticated successfully with payload");
        ref.set({age: 30});
        $("#result").text("ageを30に設定しました");
      }
    });
  })
</script>
</body>
</html>

実行と結果

firebase-security2.png

firebase-security1.png

siteB(悪い人)

コード

<!DOCTYPE HTML>
<html lang="en-US">
<head>
  <title>firebase test</title>
  <script src="https://cdn.firebase.com/js/client/2.4.1/firebase.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
  <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<button id="login"  class="btn btn-primary">ageを変更</button>
  <p id="result"></p>
  <script>
  var ref = new Firebase("https://{app_name}.firebaseio.com");
        ref.set({age: 20});
        $("#result").text("ageを20に設定しました");
</script>
</body>
</html>

実行と結果

firebase-security3.png

firebase-security4.png

対策 - Firebaseのセキュリティとルールを利用

Firebaseのセキュリティとルールを設定します

firebase-security5.png

説明

  • Firebaseのconsoleに、「Security & Rules」をクリックして、セキュリティとルール操作画面が出てます。
  • 上記の図の内容より、設定します。
    • 全てのユーザに関する内容を、usersの下に追加します。uidの値より、ユーザを区別することができます。
    • uid下に、uidを持っているユーザのみは、読み書き権限を設定します。
  • 右上のsaveボタンをクリックして、Firebaseに保存できます。

siteAのコード

......
ref.authWithOAuthPopup("twitter", function (error, authData) {
      if (error) {
        $("#result").text("Login Failed!");
      } else {
        $("#result").text("Authenticated successfully with payload");
        //ref.set({age: 30});
        ref.child('users').child(authData.uid).set({age:30});
        $("#result").text("ageを30に設定しました");
      }
......

結果

firebase-security6.png

  • このようにすると、ユーザが認証していない場合は誰も対象ユーザに関する情報を改ざんできなくなります。

感想

  • Firebaseのセキュリティとルールを利用して、安全を保証できます。
  • Firebaseのセキュリティ対応は、その他のサービスと違うことがあります。
    • Firebaseには、セキュリティルールを設定することができます。
    • その他のサービスには、認証や、サーバー側の権限確認等で安全を保証します。
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?