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

Fix undefined access error in empty access policies #855

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

dennis531
Copy link
Contributor

@dennis531 dennis531 commented Jul 25, 2024

Changes:

  • Add check if fetched acls contain an ace property
  • Hopefully improve code readability

Close #334

@dennis531 dennis531 added the type:bug Something isn't working label Jul 25, 2024
Copy link
Contributor

Use docker or podman to test this pull request locally.

Run test server using develop.opencast.org as backend:

podman run --rm -it -p 127.0.0.1:3000:3000 ghcr.io/opencast/opencast-admin-interface:pr-855

Specify a different backend like stable.opencast.org:

podman run --rm -it -p 127.0.0.1:3000:3000 -e PROXY_TARGET=https://stable.opencast.org ghcr.io/opencast/opencast-admin-interface:pr-855

It may take a few seconds for the interface to spin up.
It will then be available at http://127.0.0.1:3000.
For more options you can pass on to the proxy, take a look at the README.md.

Copy link
Contributor

github-actions bot commented Jul 25, 2024

This pull request is deployed at test.admin-interface.opencast.org/855/2024-08-29_11-08-20/ .
It might take a few minutes for it to become available.

src/slices/eventDetailsSlice.ts Outdated Show resolved Hide resolved
}
if (policy.action === "read" || policy.action === "write") {
newPolicies[policy.role][policy.action] = policy.allow;
} else if (policy.allow === true) { //|| policy.allow === "true") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know allow is a boolean, don't we?

Suggested change
} else if (policy.allow === true) { //|| policy.allow === "true") {
} else if (policy.allow) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer a strict type check to prevent false truthy values, for example "false", see https://developer.mozilla.org/en-US/docs/Glossary/Truthy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the whole idea of Typescript that we have the strict type check? And allow is defined as a boolean, isn't it? So, this can never have the value "false" can it?

Copy link
Contributor Author

@dennis531 dennis531 Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The policy variable has the type Ace which defines allow as boolean:

export type Ace = {
allow: boolean,
role: string,
action: string
}

But yes, it can, because Typescript only does a type check at compile time, see https://learning-notes.mistermicheels.com/javascript/typescript/runtime-type-checking/. A solution could be a validation library for external data.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although if it can be string, we should adjust our type definition to correctly reflect that (allow: boolean | string), or fix our backend to always give us a boolean. Since in this case we have full control over the external data (aka the admin ui backend), introducing a validation library is arguably overkill.

Copy link
Member

@lkiesow lkiesow Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I thought TypeScript would include some runtime type checks.

[…] or fix our backend to always give us a boolean […]

I think that's already what the backend does. So I think we can still assume this to be a boolean.

But let's assume we aren't sure about the type here, that would mean the code would convert a value like null, undefined, "true" or "True" to false. This is different from how Opencast's core would handle this: A non-existing allow value is assumed to be true. A string would cause an error.

In other words, this isn't really a type check, but just setting a default value of false for everything but true. That means, what this actually does is just to re-define which values are truthy.

If we want an actual type check, we could e.g. add a typeof type guard before line 877. Something like:

if (typeof policy.allow !== "boolean") {
  console.error("Invalid value of allow in acl");
  continue:
}

If we want a behavior identical to Opencast's, we could even do

if (policy.allow === undefined) {
  policy.allow = true
}
if (typeof policy.allow !== "boolean") {
  console.error("Invalid value of allow in acl");
  continue:
}

Or we could just assume that the backend provides correct data :D

In the end, I think this is an interesting thing to argue about, but it has little real word relevance. If you ant to modify this, that's good. If you don't, that's fine as well, and we can merge it like this.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type checking is not really necessary if we know that the backend provides the correct data. Nevertheless, we should unify the behavior of unset parameters.

@lkiesow lkiesow self-assigned this Jul 30, 2024
Copy link
Contributor

github-actions bot commented Aug 9, 2024

This pull request has conflicts ☹
Please resolve those so we can review the pull request.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Admin interface displays ACL rules from different events
3 participants