Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

注意!这个项目的密码验证和私密书签完全在前端实现 #4

Open
molikai-work opened this issue Sep 12, 2024 · 2 comments

Comments

@molikai-work
Copy link

即使在未登录的情况下,api依然返回所有数据,是前端根据登陆状态决定是否显示或者隐藏:
image

在后端代码中仅在verifyPassword端口做了密码验证,saveOrder完全没有安全验证或者其他验证措施,

if (url.pathname === '/api/saveOrder' && request.method === 'POST') {
    const { userId, links, categories } = await request.json();
    await env.CARD_ORDER.put(userId, JSON.stringify({ links, categories }));
    return new Response(JSON.stringify({ success: true }), { status: 200 });
}

if (url.pathname === '/api/verifyPassword' && request.method === 'POST') {
    const { password } = await request.json();
    const isValid = password === env.ADMIN_PASSWORD;
    return new Response(JSON.stringify({ valid: isValid }), {
        status: isValid ? 200 : 403,
        headers: { 'Content-Type': 'application/json' },
    });
}

要做的更改例如:

  1. getLinks添加验证,并添加处理措施,如果在请求中未包含正确的密码字段则只返回可公开的数据。
  2. saveOrder添加验证,只有在提供正确的密码之后才能继续处理数据,否则拒绝。

优化建议,不要将前端文件直接放在后端代码中供访问主页时返回,
可以将前端文件放在远程地址,例如github存储库,
然后在返回函数改为使用fetch请求远程地址并返回,
示例:

if (url.pathname === '/') {
    return fetch('https://example.github.io/Card-Tab/')
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.text();
        })
        .then(htmlContent => {
            return new Response(htmlContent, {
                headers: { 'Content-Type': 'text/html' }
            });
        })
        .catch(error => {
            return new Response('获取 HTML 内容时出错', {
                status: 500
            });
        });
}
@hmhm2022
Copy link
Owner

感谢指导,其实我是前端小白,属于是有了个想法以后 边干边学的。
只关注界面和功能优化了,确实在安全方面埋了坑,这个后面会考虑你的建议。
我最初的想法就是做一个轻量化容易上手,对于纯小白特别友好的书签导航页,力求简单,所以前后端分离我不太会考虑。
基于以上,特别注重隐私的朋友请谨慎将你的私密链接放置到本项目中,并建议部署了项目的朋友备份好自己的书签(直接到KV里 拷贝出来即可)

@h20see
Copy link

h20see commented Oct 21, 2024

使用cf自带域名,xx.xx.dev/api/getLinks?userId=testUser确实会输出全部书签,停用cf自带域名使用个人域名,个人域名/api/getLinks?userId=testUser就不会输出书签了,网页显示书签也正常,貌似是隐藏成功的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants