首页 > 解决方案 > 在没有更改后,在 Next.js 应用程序中读取 Firestore 的突然飙升 - 请参见图表

问题描述

在此处输入图像描述

在图表的第 1 天到第 10 天,我正在处理这个项目 - 你可以看到的读取范围在每天 100 到 300 之间,定期刷新来自 firestore 的文档列表。第 11 天到第 15 天,我正在拜访家人并远离项目。在第 16 天,我启动了这个项目 15 分钟,但没有做任何更改(我没有注意到它飙升这么高,因为它没有超过配额,而且我没有开发或跟踪)。今天,在实际从事该项目的同时,我达到了免费配额。峰值发生在我开始工作的两个小时内。

我认为读取来自我的 Next.js 应用程序中的这个 clientList 组件:

let unsubscribe;
const Clients = () => {
  const classes = useStyles();
  firebase.auth().onAuthStateChanged(function (user) {
    if (user) {
      const db = firebase.firestore();
      unsubscribe = db.collection("users")
        .doc(user.uid)
        .collection("clients")
        .onSnapshot((snap) => {
          const clients = snap.docs.map((doc) => ({
            id: doc.id,
            ...doc.data(),
          }));
          unsubscribe();
          setClients(clients);
        });
    } else {
      [];
    }
  });

 

  const [clients, setClients] = useState([]);

  return (


    <Fragment>
      {clients.map((client, i) => (
        <Paper className={classes.paper} key={client.id}>
          <Typography
            className={client.name}
            color="textSecondary"
            gutterBottom
          >
            {client.name}
          </Typography>
  
        </Paper>
      ))}
    </Fragment>

  );
};

export default Clients;

在恐慌中,我更改了我的安全详细信息:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
    match /users/{userId}/clients/{document=**} {
      allow read: if request.auth.uid == userId;
    }
  }
}

回到这个:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone with your database reference to view, edit,
    // and delete all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // all client requests to your Firestore database will be denied until you Update
    // your rules
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2019, 9, 10);
    }
  }
}

直到我确定我原来的安全规则没问题。我对 clientList.js 的最后一次提交是在第 10 天。今天在处理项目时,我正在使用按钮显示和隐藏表单,这两者都与 clientList.js 无关(由于动态而称为 Client导入)或 Firestore。

我目前不使用 firebase CLI,并且会打开一个 firestore 仪表板 chrome 窗口和一个 localhost 窗口(与不超过或接近配额的十天相同)。没有无法识别的身份验证用户,并且匿名身份验证不是一种选择。

谁能建议我如何解决这样的问题?

标签: javascriptfirebasegoogle-cloud-firestorenext.jsfirebase-security

解决方案


您可以在 GCP 云控制台中检查 stackdriver 是否有任何可能导致使用此 stackdriver filter resource.type="datastore_database" 的高读取使用率的管理作业

最常见的意外使用来源包括:

  • 使用管理的进出口。
  • 在文档更改事件期间读取由活动侦听器(监视)产生的操作。

您还可以向 GCP 支持查询有关读取来源的更多信息,但这不会包含太多细节,它只会包括每个来源渠道的操作数(导出、批量获取文档、通过监视更改、列表集合ids、列表文档、查询等)

根据this other SO post,如果不实施额外的逻辑来跟踪它们,很难确定读取的来源


推荐阅读