Skip to content

Commit

Permalink
Add Actor ID validation (#539)
Browse files Browse the repository at this point in the history
* Add Actor ID validation

Signed-off-by: heunghingwan <[email protected]>

* Validatie Actor ID cannot be empty

Signed-off-by: heunghingwan <[email protected]>

* Encode URI instead of checking

Signed-off-by: heunghingwan <[email protected]>

* Make empty check more general

Signed-off-by: heunghingwan <[email protected]>

* Update ActorId.ts

Signed-off-by: heunghingwan <[email protected]>

* only use encoded actor id in http protocol

Signed-off-by: heunghingwan <[email protected]>

* fix prettier error

Signed-off-by: heunghingwan <[email protected]>

---------

Signed-off-by: heunghingwan <[email protected]>
  • Loading branch information
heunghingwan authored Nov 28, 2023
1 parent f661574 commit 46c83d9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/actors/ActorId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default class ActorId {
private readonly id: string;

constructor(id: string) {
if (!id) {
throw new Error("ActorId cannot be empty");
}
this.id = id;
}

Expand All @@ -28,6 +31,10 @@ export default class ActorId {
return this.id;
}

getURLSafeId() {
return encodeURIComponent(this.id);
}

toString() {
return this.id;
}
Expand Down
20 changes: 10 additions & 10 deletions src/actors/client/ActorClient/ActorClientHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async invoke(actorType: string, actorId: ActorId, methodName: string, body?: any): Promise<object> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/method/${methodName}`, {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/method/${methodName}`, {
method: "POST", // we always use POST calls for Invoking (ref: https://github.com/dapr/js-sdk/pull/137#discussion_r772636068)
body,
});
Expand All @@ -37,7 +37,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async stateTransaction(actorType: string, actorId: ActorId, operations: OperationType[]): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/state`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -47,7 +47,7 @@ export default class ActorClientHTTP implements IClientActor {
}

async stateGet(actorType: string, actorId: ActorId, key: string): Promise<KeyValueType | string> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/state/${key}`);
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/state/${key}`);
return result as any;
}

Expand All @@ -57,7 +57,7 @@ export default class ActorClientHTTP implements IClientActor {
name: string,
reminder: ActorReminderType,
): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -72,18 +72,18 @@ export default class ActorClientHTTP implements IClientActor {
}

async reminderGet(actorType: string, actorId: ActorId, name: string): Promise<object> {
const result = await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`);
const result = await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`);
return result as object;
}

async unregisterActorReminder(actorType: string, actorId: ActorId, name: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/reminders/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/reminders/${name}`, {
method: "DELETE",
});
}

async registerActorTimer(actorType: string, actorId: ActorId, name: string, timer: ActorTimerType): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/timers/${name}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -99,13 +99,13 @@ export default class ActorClientHTTP implements IClientActor {
}

async unregisterActorTimer(actorType: string, actorId: ActorId, name: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getId()}/timers/${name}`, {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}/timers/${name}`, {
method: "DELETE",
});
}

async deactivate(actorType: string, actorId: string): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId}`, {
async deactivate(actorType: string, actorId: ActorId): Promise<void> {
await this.client.execute(`/actors/${actorType}/${actorId.getURLSafeId()}`, {
method: "DELETE",
});
}
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/http/actors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ describe("http/actors", () => {
});
});

describe("actorId", () => {
it("should be able to create an actorId", () => {
const actorId = ActorId.createRandomId();
expect(actorId.getId()).toBeDefined();
expect(actorId.getURLSafeId()).toBeDefined();
expect(actorId.toString()).toBeDefined();
});

it("should not be able to create an actorId with an empty string", () => {
expect(() => new ActorId("")).toThrowError("ActorId cannot be empty");
});

it("should be able to create an actorId with url unsafe characters like '/'", () => {
const actorId = new ActorId("test/actor");
expect(actorId.getURLSafeId()).toEqual("test%2Factor");
expect(actorId.getId()).toEqual("test/actor");
expect(actorId.toString()).toEqual("test/actor");
});
});

describe("actorProxy", () => {
it("should be able to create an actor object through the proxy", async () => {
const builder = new ActorProxyBuilder<DemoActorCounterInterface>(DemoActorCounterImpl, client);
Expand Down

0 comments on commit 46c83d9

Please sign in to comment.