Skip to content

Commit d75d805

Browse files
n0mahdclaude
andcommitted
fix(server): read a project's skills only for a registered project
The skill list and a single skill took any absolute folder as the project and scanned the skill folders under it, so a client could have the server read the folders of any path on the machine. A given folder must now be the workspace root of a project the environment knows, the same check the skill writes make, or the read is refused with `projectNotRegistered`. Global reads, which have no folder, are unchanged. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 2d33903 commit d75d805

5 files changed

Lines changed: 155 additions & 24 deletions

File tree

‎apps/server/src/server.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
592592
ReplayMarkers.layer,
593593
).pipe(
594594
// Core Services
595+
// It checks a project's folder against ProjectService, which the next layer provides.
596+
Layer.provideMerge(SkillCatalog.layer),
595597
Layer.provideMerge(layerOrchestrationApplication),
596598
Layer.provideMerge(RuntimeLayer.layerEventInfrastructure),
597599
Layer.provideMerge(Layer.merge(ProjectStore.layer, ThreadSearch.layer)),
@@ -659,7 +661,6 @@ const layerRuntimeCoreDependencies = layerRuntimeCoreDependenciesBase.pipe(
659661
),
660662
),
661663
Layer.provideMerge(layerWorkspace),
662-
Layer.provideMerge(SkillCatalog.layer),
663664
Layer.provideMerge(ProjectEnrichmentService.layer),
664665
Layer.provideMerge(Layer.mergeAll(NativeAppIconResolver.layer, layerProjectFaviconResolver)),
665666
Layer.provideMerge(layerRepositoryIdentityResolver),

‎apps/server/src/skills/SkillCatalog.test.ts‎

Lines changed: 97 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { it, describe, expect } from "@effect/vitest";
33
import {
4+
ProjectId,
45
ProviderDriverKind,
56
ProviderInstanceId,
67
SkillGetResult,
78
SkillListResult,
9+
SkillRequestError,
10+
type Project,
811
type SkillAgentAccess,
912
type SkillSummary,
1013
} from "@t3tools/contracts";
@@ -13,9 +16,11 @@ import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
1316
import * as Effect from "effect/Effect";
1417
import * as FileSystem from "effect/FileSystem";
1518
import * as Layer from "effect/Layer";
19+
import * as Option from "effect/Option";
1620
import * as Path from "effect/Path";
1721
import * as Schema from "effect/Schema";
1822

23+
import * as ProjectService from "../project/ProjectService.ts";
1924
import * as Settings from "../serverSettings.ts";
2025
import * as SkillCatalog from "./SkillCatalog.ts";
2126

@@ -84,30 +89,63 @@ const ALL_AGENTS_ENABLED = Object.fromEntries(
8489
]),
8590
);
8691

87-
/** The catalog as it sees a machine whose home is `home`, with these server settings. */
92+
const makeProject = (workspaceRoot: string): Project => ({
93+
id: ProjectId.make("project-skill-catalog"),
94+
title: "App",
95+
workspaceRoot,
96+
repositoryIdentity: null,
97+
faviconPath: null,
98+
projectIcon: null,
99+
defaultModelSelection: null,
100+
defaultThreadEnvMode: null,
101+
autoPull: false,
102+
scripts: [],
103+
createdAt: "2026-01-01T00:00:00.000Z",
104+
updatedAt: "2026-01-01T00:00:00.000Z",
105+
deletedAt: null,
106+
});
107+
108+
/**
109+
* The catalog as it sees a machine whose home is `home`, with these server settings. Only the
110+
* `registered` folders are projects; by default that is the machine's `repos/app`.
111+
*/
88112
const withCatalog = <A, E, R>(
89113
home: string,
90114
use: (catalog: SkillCatalog.SkillCatalog["Service"]) => Effect.Effect<A, E, R>,
91115
options: {
92116
readonly settings?: Parameters<typeof Settings.layerTest>[0];
93117
readonly env?: NodeJS.ProcessEnv;
118+
readonly registered?: readonly string[];
94119
} = {},
95120
) =>
96121
Effect.gen(function* () {
97-
return yield* use(yield* SkillCatalog.SkillCatalog);
98-
}).pipe(
99-
Effect.provide(
100-
SkillCatalog.layer.pipe(
101-
Layer.provide(
102-
Settings.layerTest({
103-
...options.settings,
104-
providerInstances: { ...ALL_AGENTS_ENABLED, ...options.settings?.providerInstances },
105-
}),
122+
const path = yield* Path.Path;
123+
const registered = options.registered ?? [path.join(home, "repos/app")];
124+
const projects = Layer.mock(ProjectService.ProjectService)({
125+
getByWorkspaceRoot: (root) =>
126+
Effect.succeed(registered.includes(root) ? Option.some(makeProject(root)) : Option.none()),
127+
});
128+
return yield* Effect.gen(function* () {
129+
return yield* use(yield* SkillCatalog.SkillCatalog);
130+
}).pipe(
131+
Effect.provide(
132+
SkillCatalog.layer.pipe(
133+
Layer.provide(
134+
Layer.mergeAll(
135+
projects,
136+
Settings.layerTest({
137+
...options.settings,
138+
providerInstances: {
139+
...ALL_AGENTS_ENABLED,
140+
...options.settings?.providerInstances,
141+
},
142+
}),
143+
),
144+
),
106145
),
107146
),
108-
),
109-
Effect.provideService(HostProcessEnvironment, { HOME: home, ...options.env }),
110-
);
147+
);
148+
}).pipe(Effect.provideService(HostProcessEnvironment, { HOME: home, ...options.env }));
111149

112150
const byKey = (skills: readonly SkillSummary[]) =>
113151
new Map(skills.map((skill) => [`${skill.scope}:${skill.name}`, skill]));
@@ -635,6 +673,52 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("SkillCatalog", (it)
635673
);
636674
});
637675

676+
describe("project folders", () => {
677+
it.effect.skipIf(!symlinksSupported)(
678+
"reads a project's skill folders only when the folder is a registered project",
679+
() =>
680+
Effect.gen(function* () {
681+
const { home, project } = yield* makeMachine;
682+
const refused = new SkillRequestError({ reason: "projectNotRegistered" });
683+
const get = (cwd: string) => ({
684+
cwd,
685+
scope: "project" as const,
686+
name: "verify",
687+
home: ".agents/skills/verify",
688+
});
689+
690+
// A folder that holds skills but isn't a project (the home, a project's subfolder, a
691+
// relative path, a path that isn't there) is refused for the list and for one skill.
692+
for (const cwd of [home, `${project}/.agents`, "repos/app", `${home}/missing`]) {
693+
const registered = [project];
694+
expect(
695+
yield* withCatalog(home, (catalog) => catalog.list({ cwd }).pipe(Effect.flip), {
696+
registered,
697+
}),
698+
).toEqual(refused);
699+
expect(
700+
yield* withCatalog(home, (catalog) => catalog.get(get(cwd)).pipe(Effect.flip), {
701+
registered,
702+
}),
703+
).toEqual(refused);
704+
}
705+
706+
// The registered project, and the Global folders without any `cwd`, are read as before.
707+
const listed = yield* withCatalog(home, (catalog) => catalog.list({ cwd: project }));
708+
expect(listed.skills.some((skill) => skill.scope === "project")).toBe(true);
709+
const global = yield* withCatalog(home, (catalog) => catalog.list({}), {
710+
registered: [],
711+
});
712+
expect(global.skills.map((skill) => skill.scope)).toEqual(
713+
global.skills.map(() => "global"),
714+
);
715+
expect(global.skills.length).toBeGreaterThan(0);
716+
const detail = yield* withCatalog(home, (catalog) => catalog.get(get(project)));
717+
expect(detail.home).toBe(`${project}/.agents/skills/verify`);
718+
}),
719+
);
720+
});
721+
638722
describe("get", () => {
639723
it.effect.skipIf(!symlinksSupported)(
640724
"returns the full SKILL.md, the file list and which files can run",

‎apps/server/src/skills/SkillCatalog.ts‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
type SkillListResult,
3030
type SkillScope,
3131
type SkillSummary,
32+
SkillRequestError,
3233
} from "@t3tools/contracts";
3334
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
3435
import * as Context from "effect/Context";
@@ -54,6 +55,7 @@ import {
5455
parseSkillFrontmatter,
5556
resolveClaudeConfigDirPath,
5657
} from "../provider/Drivers/ClaudeSkills.ts";
58+
import * as ProjectService from "../project/ProjectService.ts";
5759
import { deriveProviderInstanceConfigMap } from "../provider/ProviderInstanceRegistryHydration.ts";
5860
import * as Settings from "../serverSettings.ts";
5961

@@ -146,11 +148,11 @@ export class SkillCatalog extends Context.Service<
146148
{
147149
/**
148150
* One compact record per skill home, in the project (when `cwd` is given) and in the user's
149-
* home folder.
151+
* home folder. A `cwd` that isn't a registered project's workspace root is refused.
150152
*/
151-
readonly list: (input: SkillListInput) => Effect.Effect<SkillListResult>;
153+
readonly list: (input: SkillListInput) => Effect.Effect<SkillListResult, SkillRequestError>;
152154
/** The full SKILL.md text and the file list of one skill from `list`. */
153-
readonly get: (input: SkillGetInput) => Effect.Effect<SkillGetResult>;
155+
readonly get: (input: SkillGetInput) => Effect.Effect<SkillGetResult, SkillRequestError>;
154156
}
155157
>()("t3/skills/SkillCatalog") {}
156158

@@ -160,6 +162,7 @@ const make = Effect.gen(function* () {
160162
const platform = yield* HostProcessPlatform;
161163
const environment = yield* HostProcessEnvironment;
162164
const serverSettings = yield* Settings.ServerSettingsService;
165+
const projects = yield* ProjectService.ProjectService;
163166
const homeDirectory =
164167
(platform === "win32" ? environment.USERPROFILE : environment.HOME)?.trim() || NodeOS.homedir();
165168

@@ -267,8 +270,30 @@ const make = Effect.gen(function* () {
267270
return absolute;
268271
};
269272

270-
const absoluteCwd = (cwd: string | undefined) =>
271-
cwd !== undefined && path.isAbsolute(cwd) ? cwd : undefined;
273+
/**
274+
* A project's folders are read only when `cwd` is the workspace root of a project the
275+
* environment knows, so a request can't have the server walk skill folders under any path on
276+
* the machine. Without a `cwd` only the Global folders are read.
277+
*/
278+
const requireProject = Effect.fnUntraced(function* (cwd: string | undefined) {
279+
if (cwd === undefined) return undefined;
280+
// The lookup resolves a relative path against the server's own folder, so it never sees one.
281+
const project = path.isAbsolute(cwd)
282+
? yield* projects.getByWorkspaceRoot(cwd).pipe(
283+
Effect.catchTags({
284+
// A folder that is gone or isn't a folder can't be a project's root.
285+
ProjectOperationError: (error) =>
286+
error.operation === "normalize-workspace"
287+
? Effect.succeed(Option.none<never>())
288+
: Effect.die(error),
289+
}),
290+
)
291+
: Option.none();
292+
if (Option.isNone(project)) {
293+
return yield* new SkillRequestError({ reason: "projectNotRegistered" });
294+
}
295+
return cwd;
296+
});
272297

273298
/** A global folder as shown to the user: `~/...` under the home directory, else its path. */
274299
const globalLabel = (directory: string) => {
@@ -420,7 +445,7 @@ const make = Effect.gen(function* () {
420445
});
421446

422447
const list: SkillCatalog["Service"]["list"] = Effect.fn("SkillCatalog.list")(function* (input) {
423-
const cwd = absoluteCwd(input.cwd);
448+
const cwd = yield* requireProject(input.cwd);
424449
const displayRoots = yield* displayRootsOf(cwd);
425450
const instances = yield* loadInstances(cwd);
426451
const roots = rootsFor(cwd, instances);
@@ -591,7 +616,7 @@ const make = Effect.gen(function* () {
591616
});
592617

593618
const get: SkillCatalog["Service"]["get"] = Effect.fn("SkillCatalog.get")(function* (input) {
594-
const cwd = absoluteCwd(input.cwd);
619+
const cwd = yield* requireProject(input.cwd);
595620
const base = input.scope === "project" ? cwd : homeDirectory;
596621
if (!base || !isSkillFolderName(input.name)) return NOT_FOUND;
597622
// Only the folders agents read are looked in, so the request can't name an arbitrary path.

‎packages/contracts/src/rpc.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,13 @@ import {
318318
} from "./providerUsageLimits.ts";
319319
import { UsagePricing, UsageReadError, UsageSummary, UsageSummaryInput } from "./usage.ts";
320320
import { ServerSettings, ServerSettingsError, ServerSettingsPatch } from "./settings.ts";
321-
import { SkillGetInput, SkillGetResult, SkillListInput, SkillListResult } from "./skills.ts";
321+
import {
322+
SkillGetInput,
323+
SkillGetResult,
324+
SkillListInput,
325+
SkillListResult,
326+
SkillRequestError,
327+
} from "./skills.ts";
322328
import {
323329
ScheduledTaskDeleteInput,
324330
ScheduledTaskDeleteResult,
@@ -597,13 +603,13 @@ const WsServerGetConfigRpc = Rpc.make(WS_METHODS.serverGetConfig, {
597603
const WsServerListSkillsRpc = Rpc.make(WS_METHODS.serverListSkills, {
598604
payload: SkillListInput,
599605
success: SkillListResult,
600-
error: EnvironmentAuthorizationError,
606+
error: Schema.Union([SkillRequestError, EnvironmentAuthorizationError]),
601607
});
602608

603609
const WsServerGetSkillRpc = Rpc.make(WS_METHODS.serverGetSkill, {
604610
payload: SkillGetInput,
605611
success: SkillGetResult,
606-
error: EnvironmentAuthorizationError,
612+
error: Schema.Union([SkillRequestError, EnvironmentAuthorizationError]),
607613
});
608614

609615
const WsServerRefreshProvidersRpc = Rpc.make(WS_METHODS.serverRefreshProviders, {

‎packages/contracts/src/skills.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,18 @@ export const SkillGetResult = Schema.Struct({
100100
filesTruncated: Schema.Boolean,
101101
});
102102
export type SkillGetResult = typeof SkillGetResult.Type;
103+
104+
/**
105+
* A skill read that couldn't be carried out, as opposed to a folder with no skills: a project's
106+
* folders are only read when the environment knows the folder as a project.
107+
*/
108+
export class SkillRequestError extends Schema.TaggedError<SkillRequestError>()(
109+
"SkillRequestError",
110+
{
111+
reason: Schema.Literals(["projectNotRegistered"]),
112+
},
113+
) {
114+
override get message(): string {
115+
return "That folder isn't a project in this environment.";
116+
}
117+
}

0 commit comments

Comments
 (0)