简体   繁体   English

限制AWS Cognito用户池javascript api的请求来源

[英]Restricting request origin for AWS Cognito User Pools javascript api

I'm concerned that the AWS Cognito User Pools Javascript API doesn't seem to care which website requests are coming from (all you need to use the API are the User Pool ID and Client ID, which would be readily available in my javascript source). 我担心AWS Cognito用户池Javascript API似乎并不关心哪些网站请求来自(您需要使用API​​的是用户池ID和客户端ID,这些都可以在我的javascript源代码中找到) )。

Am I right to be concerned that another site could hijack my user pool, potentially tricking users into signing up to it? 我是否有理由担心另一个网站可能会劫持我的用户池,可能会欺骗用户注册它?

If this is a valid concern, is there any way to prevent this? 如果这是一个有效的问题,有什么办法可以防止这种情况吗? The pre-authentication Lambda payload doesn't seem to include any request origin data, so I guess that isn't the way to do it. 预认证Lambda有效负载似乎不包含任何请求源数据,所以我想这不是这样做的方法。

If it isn't something I need to be concerned about, why is that? 如果不是我需要关注的事情,那为什么呢?

So, I have contemplated this in great length and decided this: 所以,我已经考虑了很长的时间,并决定:

Yes it's ok to have those digits on your front end. 是的,你的前端有这些数字是可以的。

Of course - for many reasons as follows. 当然 - 由于以下原因很多。

First : 第一

Question: 题:

Am I right to be concerned that another site could hijack my user pool, potentially tricking users into signing up to it? 我是否有理由担心另一个网站可能会劫持我的用户池,可能会欺骗用户注册它?

Response: 响应:

If I were to take your UserPoolID and ClientID - could I "hijack" to your application? 如果我要拿你的UserPoolIDClientID - 我可以“劫持”你的应用程序吗?

Answer: 回答:

Not exactly ...maybe kinda sorta but why ... 完全 ......也许有点儿,但为什么 ......

The level of "tenancy" or "permission" you give to a client is entirely up to you and your IAM Roles. 您向客户提供的“租赁”或“许可”级别完全取决于您和您的IAM角色。 Lets say we don't consider my second and more relavant reason, yet - (origin checks) . 让我们说,我们不考虑我的第二个和更相关的原因,但 - (原产地检查)

If I steal your access keys and misuse your app/brand/whatever, I am simply driving clients to your site. 如果我窃取您的访问密钥并滥用您的应用/品牌/任何东西,我只是将客户推送到您的网站。 I cannot gain access to your client list, data, logs, records, etc. IF you set your authenticated user permissions to not allow that. 我无法访问您的客户端列表,数据,日志,记录等。 如果您将经过身份验证的用户权限设置为不允许 Locking down your "Admin Level" permissions to client lists, pool info, data, etc. 锁定您对客户端列表,池信息,数据等的“管理级别”权限。

Example (added to your Statement section): 示例(添加到Statement部分):

{
    "Effect": "Deny",
    "Action": [
        "cognito-identity:CreateIdentityPool",
        "cognito-identity:DeleteIdentityPool",
        "cognito-identity:DeleteIdentities",
        "cognito-identity:DescribeIdentity",
        "cognito-identity:DescribeIdentityPool",
        "cognito-identity:GetIdentityPoolRoles",
        "cognito-identity:ListIdentities",
        "cognito-identity:ListIdentityPools",
        "cognito-identity:LookupDeveloperIdentity",
        "cognito-identity:MergeDeveloperIdentities",
        "cognito-identity:SetIdentityPoolRoles",
        "cognito-identity:UnlinkDeveloperIdentity",
        "cognito-identity:UpdateIdentityPool"
    ],
    "Resource": [
        "arn:aws:cognito-identity:us-east-1:ACCOUNT_DIGITS:identitypool/us-east-1:PoolID_NUMBERS"
    ]
}

Or simply the opposite: 或者恰恰相反:

{
    "Effect": "Allow",
    "Action": [
        "cognito-identity:GetOpenIdTokenForDeveloperIdentity"
    ],
    "Resource": "arn:aws:cognito-identity:us-east-1:ACCOUNT_DIGITS:identitypool/us-east-1:NUMBERS-NUMBERS-PoolID"
}

Only need the "cognito-identity:GetOpenIdTokenForDeveloperIdentity" part. 只需要"cognito-identity:GetOpenIdTokenForDeveloperIdentity"部分。 Locking down your "User Level" permissions to stuff 锁定你的“用户级别”权限

Example: 例:

{
    "Effect": "Allow",
    "Action": [ "s3:PutObject", "s3:GetObject" ],
    "Resource": [
        "arn:aws:s3:::[bucket]/[folder]/${cognito-identity.amazonaws.com:sub}/*"
    ]
}

As an obvious rule of thumb - only give users permission to what they need. 作为一个明显的经验法则 - 只允许用户获得他们需要的权限。 Lock down all the crap you can possibly lock down and use the policy simulator. 锁定您可能锁定的所有垃圾并使用策略模拟器。 Conclusion to reason one: 结论理由一:

You can lock down all the things that would expose your client base and make it pointless for someone to 'hyjack' your site. 您可以锁定所有会暴露您的客户群的事情,并使某人“劫持”您的网站毫无意义。 Counter argument: 相反的观点:

Ya, but what IF 雅,但是如果

Here is a doc that might help for IAM stuff And some more 这是一个可能有助于IAM内容的文档 等等


Second : 第二

Question: 题:

The pre-authentication Lambda payload doesn't seem to include any request origin data, so I guess that isn't the way to do it. 预认证Lambda有效负载似乎不包含任何请求源数据,所以我想这不是这样做的方法。

Response: 响应:

Hmm. 嗯。

Answer: 回答:

Yes it does include request origin data - IF one sets it up. 是的它确实包括请求源数据 - 如果设置了它。

Question: 题:

I'm concerned that the AWS Cognito User Pools Javascript API doesn't seem to care which website requests are coming from 我担心AWS Cognito用户池Javascript API似乎并不关心哪些网站请求来自

Answer: 回答:

For this - you're correct. 为此 - 你是对的。 If you are using static served files with user pools triggers - there is little done to check origin. 如果您使用具有用户池触发器的静态服务文件 - 检查原点几乎没有做。

So - if you really want to - you can set all this up using API Gateway to Lambda. 所以 - 如果你真的想 - 你可以使用API​​网关将所有这些设置为Lambda。 This will remove direct interaction with User Pools from the client side and put it on the back end. 这将从客户端删除与用户池的直接交互,并将其放在后端。

Preface: 前言:

Sets to setup: 设置为:

  1. Go into User Pools and set up a pool 进入用户池并设置一个池
  2. Add a cognito idetity pool 添加一个cognito idetity池
  3. Go into Lambda and hook up a function with an API Gateway Trigger Event 进入Lambda并使用API​​网关触发事件连接函数
  4. put in your code - this is a "login" example: 放入你的代码 - 这是一个“登录”示例:


const
    AWS      = require( 'aws-sdk' ),
    UserPool = new AWS.CognitoIdentityServiceProvider();

exports.handler = ( event, context, callback ) => {
    console.log( event );
    const params = {
        AuthFlow: 'CUSTOM_AUTH',
        ClientId: 'numbers',
        AuthParameters: {
            USERNAME: event.email,
            PASSWORD: event.password
        }
    };

    UserPool.initiateAuth( params, ( err, data ) => {
        callback( err, data );
    } );
};

In the above - yes you can do: 在上面 - 是的你可以这样做:

UserPool.initiateAuth( params, callback );

Instead of: 代替:

UserPool.initiateAuth( params, ( err, data ) => {
    callback( err, data );
} );

But this throws weird errors - there is an issue open on GitHub about it already. 但是这会引发奇怪的错误--GitHub上已经有一个问题已经开启了。

  1. Go to the trigger event from API Gateway 从API网关转到触发事件
  2. Click on your method and go into the section Integration Request 单击您的方法,然后进入Integration Request部分
  3. At the bottom you'll see Body Mapping Templates 在底部,您将看到Body Mapping Templates
  4. Add a new one and put in application/json 添加一个新的并放入application/json
  5. You should see the sample template come up that follows: 您应该看到以下示例模板:

This is Apache Template Velocity Language - different from the JSONScheme language used by the other mapping template things: 这是Apache Template Velocity Language - 与其他映射模板使用的JSONScheme语言不同:

#set($allParams = $input.params())
{
    "body-json" : $input.json('$'),
    "params" : {
        #foreach($type in $allParams.keySet())
            #set($params = $allParams.get($type))
            "$type" : {
                #foreach($paramName in $params.keySet())
                    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
                    #if($foreach.hasNext),#end
                #end
            }
        #if($foreach.hasNext),#end
        #end
    },
    "stage-variables" : {
        #foreach($key in $stageVariables.keySet())
        "$key" : "$util.escapeJavaScript($stageVariables.get($key))"
        #if($foreach.hasNext),#end
        #end
    },
    "context" : {
        "account-id" : "$context.identity.accountId",
        "api-id" : "$context.apiId",
        "api-key" : "$context.identity.apiKey",
        "authorizer-principal-id" : "$context.authorizer.principalId",
        "caller" : "$context.identity.caller",
        "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
        "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
        "cognito-identity-id" : "$context.identity.cognitoIdentityId",
        "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
        "http-method" : "$context.httpMethod",
        "stage" : "$context.stage",
        "source-ip" : "$context.identity.sourceIp",
        "user" : "$context.identity.user",
        "user-agent" : "$context.identity.userAgent",
        "user-arn" : "$context.identity.userArn",
        "request-id" : "$context.requestId",
        "resource-id" : "$context.resourceId",
        "resource-path" : "$context.resourcePath"
    }
}

With this, you can get the source-ip , cognito information, etc. 有了这个,你可以获得source-ip ,cognito信息等。

This method is a secure method for locking down origin. 此方法用于锁定原点的安全方法。 You can either check origin by doing an if check in Lambda, or a IAM condition - blocking all requests from other origins. 您可以通过在Lambda中执行if检查或IAM条件来检查原点 - 阻止来自其他来源的所有请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM