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

WIP: Improve parser for WAC Allow header #1326

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

import { Quad, NamedNode } from "@rdfjs/types";
import { Access } from "./acl/acl";
import { ImmutableDataset, LocalNodeIri, Subject } from "./rdf.internal";
import { internal_AccessPermissions } from "./resource/wacAllow.internal";

/**
* Alias to indicate where we expect to be given a URL represented as an RDF/JS NamedNode.
Expand Down Expand Up @@ -108,11 +108,6 @@ export type WithResourceInfo = {
};
};

type internal_WacAllow = {
user: Access;
public: Access;
};

/**
* What access the current user has to a particular Resource, and what access everybody has.
*
Expand Down Expand Up @@ -175,7 +170,7 @@ export type WithServerResourceInfo = WithResourceInfo & {
* @see https://github.com/solid/solid-spec/blob/cb1373a369398d561b909009bd0e5a8c3fec953b/api-rest.md#wac-allow-headers
* @see https://github.com/solid/specification/issues/171
*/
permissions?: internal_WacAllow;
permissions?: internal_AccessPermissions;
};
};

Expand Down
55 changes: 1 addition & 54 deletions src/resource/resource.internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/

import LinkHeader from "http-link-header";
import { Access } from "../acl/acl";
import { WithServerResourceInfo } from "../interfaces";
import { parseWacAllowHeader } from "./wacAllow.internal";

/**
* @internal
Expand Down Expand Up @@ -75,59 +75,6 @@ export function internal_parseResourceInfo(
return resourceInfo;
}

/**
* Parse a WAC-Allow header into user and public access booleans.
*
* @param wacAllowHeader A WAC-Allow header in the format `user="read append write control",public="read"`
* @see https://github.com/solid/solid-spec/blob/cb1373a369398d561b909009bd0e5a8c3fec953b/api-rest.md#wac-allow-headers
*/
function parseWacAllowHeader(wacAllowHeader: string) {
function parsePermissionStatement(permissionStatement: string): Access {
const permissions = permissionStatement.split(" ");
const writePermission = permissions.includes("write");
return writePermission
? {
read: permissions.includes("read"),
append: true,
write: true,
control: permissions.includes("control"),
}
: {
read: permissions.includes("read"),
append: permissions.includes("append"),
write: false,
control: permissions.includes("control"),
};
}
function getStatementFor(header: string, scope: "user" | "public") {
const relevantEntries = header
.split(",")
.map((rawEntry) => rawEntry.split("="))
.filter((parts) => parts.length === 2 && parts[0].trim() === scope);

// There should only be one statement with the given scope:
if (relevantEntries.length !== 1) {
return "";
}
const relevantStatement = relevantEntries[0][1].trim();

// The given statement should be wrapped in double quotes to be valid:
if (
relevantStatement.charAt(0) !== '"' ||
relevantStatement.charAt(relevantStatement.length - 1) !== '"'
) {
return "";
}
// Return the statment without the wrapping quotes, e.g.: read append write control
return relevantStatement.substring(1, relevantStatement.length - 1);
}

return {
user: parsePermissionStatement(getStatementFor(wacAllowHeader, "user")),
public: parsePermissionStatement(getStatementFor(wacAllowHeader, "public")),
};
}

/** @hidden Used to instantiate a separate instance from input parameters */
export function internal_cloneResource<ResourceExt extends object>(
resource: ResourceExt
Expand Down
186 changes: 186 additions & 0 deletions src/resource/wacAllow.internal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/**
* Copyright 2021 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { describe, it, expect } from "@jest/globals";
import { parseWacAllowHeader } from "./wacAllow.internal";

const noAccess = {
read: false,
append: false,
write: false,
control: false,
};

const noPermissions = {
user: noAccess,
public: noAccess,
};

describe("parseWacHeader", () => {
it("should parse an empty header as no permissions", () => {
const result = parseWacAllowHeader("");

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result).toMatchObject(noPermissions);
});

it("should parse a single scope", () => {
const result = parseWacAllowHeader('public="read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result.user).toMatchObject(noAccess);

expect(result.public).toMatchObject({
...noAccess,
read: true,
});
});

it("should parse multiple scopes", () => {
const result = parseWacAllowHeader('user="write", public="read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result.user).toMatchObject({
read: false,
append: true, // note: write implies append
write: true,
control: false,
});

expect(result.public).toMatchObject({
read: true,
append: false,
write: false,
control: false,
});
});

// This is subject to change, see:
// https://github.com/solid/web-access-control-spec/issues/82
it("should handle parsing unknown access modes", () => {
const result = parseWacAllowHeader(
'public="read destroy",friends="love read"'
);

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");
expect(result).not.toHaveProperty("friends");

expect(result.user).toMatchObject(noAccess);

expect(result.public).toMatchObject({
read: true,
append: false,
write: false,
control: false,
});
});

it("should handle the append permission without write", () => {
const result = parseWacAllowHeader('user="append read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result.user).toMatchObject({
read: true,
append: true,
write: false,
control: false,
});

expect(result.public).toMatchObject(noAccess);
});

it("should handle whitespace in header values", () => {
const result = parseWacAllowHeader(' user = " append " ');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

// expects only append permission:
expect(result.user).toMatchObject({
read: false,
append: true,
write: false,
control: false,
});

expect(result.public).toMatchObject(noAccess);
});

describe("parsing of malformed headers", () => {
it("missing quotes should result in no permissions", () => {
const result = parseWacAllowHeader("user=write,public=read");

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result).toMatchObject(noPermissions);
});

it("missing quotes on one group should not affect the other", () => {
const result = parseWacAllowHeader('user=write,public="read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result.user).toMatchObject(noAccess);
expect(result.public).toMatchObject({
...noAccess,
read: true,
});
});

it("mismatched quotes should result in no permissions", () => {
const result = parseWacAllowHeader('user="write,public=read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result).toMatchObject(noPermissions);
});

it("missing scope should result in no permissions", () => {
const result = parseWacAllowHeader('="write read"');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result).toMatchObject(noPermissions);
});

it("missing/empty statement should result in no permissions", () => {
const result = parseWacAllowHeader('public=" "');

expect(result).toHaveProperty("user");
expect(result).toHaveProperty("public");

expect(result).toMatchObject(noPermissions);
});
});
});
109 changes: 109 additions & 0 deletions src/resource/wacAllow.internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright 2021 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// TODO: Should we move this type internally, as the Access in this file is not strictly the same as ACL Access
import { Access } from "../acl/acl";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@NSeydoux @VirginiaBalseiro what're your thoughts on this one? I think parsing WAC Allow shouldn't be re-using data structures from ACL, as they are different things (the types just happen to currently be compatible)

Copy link
Contributor

Choose a reason for hiding this comment

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

Well it kind of makes sense that the Access definition defined in ACL is used for the WAC-Allow header, given that the WAC-Allow header was initially described in the ACL spec. We have another Access type definition from the access/universal submodule for universal (i.e. for both ACL-controlled and ACP-controlled Pods), which is somewhat different (because ACP has a different notion of Control than ACL), but could maybe used here, since the WAC-Allow header is now present in both ACL and ACP specs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it make sense to rename Access in ./acl/acl to AclAccess (we would need to export it still as Access but mark that as a deprecated name (that'd give interface/type naming consistency within Acl

Then have wacAllow use a internal_Access type that's compatible? There's also a WacAccess in https://github.com/inrupt/solid-client-js/blob/c8da4e5b0a7baa20ff04558b7e06d1a42e8df3ae/src/access/wac.ts — I'm not sure what this is exactly, and it's undocumented in the currently released SDK version, as far as I can tell.

Perhaps we move the WacAccess up to a ./wac-allow directory, and move WacAccess and the parser / transform there?

Copy link
Contributor

Choose a reason for hiding this comment

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

The Access interface (there is no reason for it to be a type considering its declaration) should in my opinion:

  1. Be moved to a type folder under src: /src/type/
  2. Have all the modes
  3. Be used everywhere such Objects are inputs/outputs (we need to remove all the aliasing and other similar definitions)

It would be particularly convenient to have one place where all the standard interfaces are defined.
We really need to remove all the aliasing going on, it is exactly the opposite of what interfaces are for. You don't want 20 names for the same thing, you want 1 name for a specific shape and as few of them as required.

As a sidenote, the ACP specification will adopt the standard Solid access modes shortly, that is, use the acl:Control mode just like WAC. It is a design flaw that it currently invents another mechanism. The operations entailed by Access Modes are defined at the Solid protocol level.


export type internal_AccessPermissions = {
user: Access;
public: Access;
};

interface internal_parsedWacHeader {
[group: string]: string[];
}

function internal_parseWacHeader(header: string): internal_parsedWacHeader {
const rawEntries = header.split(",").map((rawEntry) => rawEntry.split("="));

const entries: internal_parsedWacHeader = {};

for (const rawEntry of rawEntries) {
if (rawEntry.length !== 2) {
continue;
}

const scope = rawEntry[0].trim();
const statement = rawEntry[1].trim();

if (!scope || !statement) {
continue;
}

if (
statement.length === 2 ||
statement.charAt(0) !== '"' ||
statement.charAt(statement.length - 1) !== '"'
) {
continue;
}

const accessModes = statement.substring(1, statement.length - 1).trim();

if (accessModes.length === 0) {
continue;
}

entries[scope] = accessModes.split(" ");
}

return entries;
}

function internal_getWacPermissions(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure this is named correctly / strictly belongs here; perhaps internal_getPermissions would be better, as it's just checking a parsed WAC Allow for given values

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me, we can always move it if it becomes relevant elsewhere.

parsed: internal_parsedWacHeader,
scope: string
): Access {
const permissions = parsed[scope];

if (!permissions || !Array.isArray(permissions)) {
return {
read: false,
append: false,
write: false,
control: false,
};
}

return {
read: permissions.includes("read"),
append: permissions.includes("append") || permissions.includes("write"),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the way https://github.com/inrupt/solid-client-js/blob/main/src/resource/resource.internal.ts#L87-L100 is written was motivated by type definitions, if I recall correctly the TS compiler was complaining if the appendand write values were not constants, but this looks definitely simpler.

write: permissions.includes("write"),
control: permissions.includes("control"),
};
}

/**
* Parse a WAC-Allow header into user and public access booleans.
*
* @param wacAllowHeader A WAC-Allow header in the format `user="read append write control",public="read"`
* @see https://github.com/solid/solid-spec/blob/cb1373a369398d561b909009bd0e5a8c3fec953b/api-rest.md#wac-allow-headers
*/
export function parseWacAllowHeader(
wacAllowHeader: string
): internal_AccessPermissions {
const parsed = internal_parseWacHeader(wacAllowHeader);

return {
user: internal_getWacPermissions(parsed, "user"),
public: internal_getWacPermissions(parsed, "public"),
};
}