Skip to content

Commit

Permalink
feat: security tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
maxholman committed Aug 7, 2024
1 parent 1e3739e commit f219c0b
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import type { Jsonifiable } from 'type-fest';
import type { JsonifiableObject } from 'type-fest/source/jsonifiable.js';
import type { HttpMethod } from './types.js';

function maybeWithNullProto<T>(val: T): T {
return typeof val === 'object'
? Object.assign(Object.create(null), val)
: val;
}

type Middleware<
CommandInput extends JsonifiableObject | unknown = unknown,
CommandOutput extends Jsonifiable | unknown = unknown,
Expand All @@ -14,37 +20,37 @@ export abstract class Command<
CommandBody extends Jsonifiable | unknown = unknown,
CommandQuery extends JsonifiableObject | unknown = unknown,
> {
public method: HttpMethod = 'get';
public readonly method: HttpMethod = 'get';

#pathname: string;
readonly #pathname: string;

#body: CommandBody | undefined;
readonly #body: CommandBody | undefined;

#query: CommandQuery | undefined;
readonly #query: CommandQuery | undefined;

protected middleware: Middleware<CommandInput, CommandOutput>[] = [];

constructor(pathname: string, body?: CommandBody, query?: CommandQuery) {
this.#pathname = pathname;
this.#body = body;
this.#query = query;
this.#body = maybeWithNullProto(body);
this.#query = maybeWithNullProto(query);
}

public serialize() {
return JSON.stringify(this.toJSON());
}

public toString(): string {
public toString() {
return this.serialize();
}

public toJSON() {
return {
return maybeWithNullProto({
method: this.method,
pathname: this.#pathname,
body: this.#body,
query: this.#query,
};
pathname: this.pathname,
body: this.body,
query: this.query,
});
}

public get body() {
Expand Down

0 comments on commit f219c0b

Please sign in to comment.