Skip to content

Commit

Permalink
Merge pull request #28 from desci-labs/m0ar/hotfix-dpid-regex
Browse files Browse the repository at this point in the history
Fix regex calling anything containing at least 1 digit a DPID
  • Loading branch information
m0ar authored Aug 21, 2024
2 parents 8b2402d + b60d145 commit 3e8b376
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/api/v2/queries/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getCeramicClient } from "../../../util/config.js";
import { resolveHistory, type CeramicClient } from "@desci-labs/desci-codex-lib";
import parentLogger from "../../../logger.js";
import { resolveDpid } from "../resolvers/dpid.js";
import { isDpid } from "../../../util/validation.js";

const logger = parentLogger.child({
module: "api/v2/queries/history",
Expand Down Expand Up @@ -95,9 +96,6 @@ export const getVersions = async (ceramic: CeramicClient, streamId: string) => {
}));
};

/** Consider ID a dPID if it looks like a number */
const isDpid = (id: string) => /^[0-9]+$/.test(id);

const getCodexHistories = async (streamIds: string[]) => {
if (streamIds.length === 0) return [];

Expand Down
9 changes: 3 additions & 6 deletions src/api/v2/resolvers/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import analytics, { LogEventType } from "../../../analytics.js";
import { getIpfsGateway, getNodesUrl } from "../../../util/config.js";
import { DpidResolverError, resolveDpid } from "./dpid.js";
import type { HistoryQueryResult } from "../queries/history.js";
import { isDpid, isVersionString } from "../../../util/validation.js";

const MODULE_PATH = "/api/v2/resolvers/generic" as const;

Expand Down Expand Up @@ -68,7 +69,7 @@ export const resolveGenericHandler = async (
logger.info({ path, query }, `Resolving path: ${path}`);

const [dpid, ...rest] = path.split("/");
if (!matchPlainDpid(dpid)) {
if (!isDpid(dpid)) {
logger.error({ path }, "invalid dpid");
return res.status(400).send({
error: "invalid dpid",
Expand All @@ -95,7 +96,7 @@ export const resolveGenericHandler = async (
let suffix = "";
if (rest.length > 0) {
const maybeVersionString = rest[0];
if (matchVersion(maybeVersionString)) {
if (isVersionString(maybeVersionString)) {
versionIx = getVersionIndex(maybeVersionString);
suffix = rest.slice(1).join("/");
logger.info({ dpid, path, versionIx, suffix }, "extracted version from path");
Expand Down Expand Up @@ -235,10 +236,6 @@ export const resolveGenericHandler = async (
}
};

const matchPlainDpid = (path: string) => /\d+/.test(path);

const matchVersion = (path: string) => /v?\d+/.test(path);

const getVersionIndex = (versionString: string): number => {
let index;
if (versionString.startsWith("v")) {
Expand Down
3 changes: 3 additions & 0 deletions src/util/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isDpid = (id: string) => /^[0-9]+$/.test(id);

export const isVersionString = (maybeVersion: string) => /v?\d/.test(maybeVersion);

0 comments on commit 3e8b376

Please sign in to comment.