Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Incorrect authz policy order #1218

Closed

Description

@kiootic

Currently, some API requires the request contains either a valid session, or master key. This is represented as:

policy.AnyOf(
	authz.PolicyFunc(policy.RequireMasterKey),
	policy.AllOf(
		authz.PolicyFunc(policy.DenyNoAccessKey),
		policy.RequireValidUser,
	),
)

However, due to ordering of the policies, if the session is expired, the produced error reason is AccessKeyNotAccepted, instead of the expected reason NotAuthenticated. Client SDK would not refresh the session automatically and breaks the session.

To fix it, the order should be changed so that NotAuthenticated would be produced.

Activity

  1. self-assigned this
    on Feb 26, 2020
  2. louischan-oursky commented on Feb 26, 2020

    @louischan-oursky
    Contributor

    I still do not understand why this happened. If we treat the anyOf and allOf having short circuit behavior.

    The boolean expression of the above policy is MasterKey || (API Key && ValidSession).

    If it is Master Key, then it passes.
    Otherwise if it is not API Key, then it fails with invalid API Key.
    Otherwise if it is valid session, then it passes.
    Otherwise if it is invalid session, then it fails with invalid session.

  3. kiootic commented on Feb 26, 2020

    @kiootic
    ContributorAuthor

    The problem is that it's not a simple true/false checks; it returns an error.

    SDK refresh session only if error has reason NotAuthenticated, which is produced by RequireValidUser policy. In expired session case, the compound policy got both AccessKeyNotAccepted and NotAuthenticated errors, and return first error (i.e. AccessKeyNotAccepted). This breaks the SDK refreshing behavior.

  4. louischan-oursky commented on Feb 26, 2020

    @louischan-oursky
    Contributor

    I guess the AccessKeyNotAccepted error is produced by RequireMasterKey, am I correct?

    In JavaScript, false || 0 evaluates to 0. Following the short circuit behavior, MasterKey || (API Key && ValidSession) should return the error of ValidSession

  5. kiootic commented on Feb 26, 2020

    @kiootic
    ContributorAuthor

    I guess the AccessKeyNotAccepted error is produced by RequireMasterKey, am I correct?

    Yes.

    In JavaScript, false || 0 evaluates to 0. Following the short circuit behavior, MasterKey || (API Key && ValidSession) should return the error of ValidSession

    Suppose errors are falsy values. In case of expired session, MasterKey() returns error AccessKeyNotAccepted, APIKey() returns ok (i.e. truthy value), and ValidSession() returns error NotAuthenticated. Then, MasterKey() || (APIKey() && ValidSession()) returns NotAuthenticated. Therefore, we should change AnyOf to return last error, instead of first error. Do you mean this?

  6. louischan-oursky commented on Feb 26, 2020

    @louischan-oursky
    Contributor

    Yes. AnyOf should return the last error.

  7. louischan-oursky commented on Feb 26, 2020

    @louischan-oursky
    Contributor

    Since the behavior of AnyOf is changed, please check if any existing code is relying on the old behavior (returning the first error)

  8. kiootic commented on Feb 26, 2020

    @kiootic
    ContributorAuthor

    The only API that uses AnyOf is this case, so there is no problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ 换网址