Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 177x 4x 173x 63x 63x 151x 150x 1x 149x 63x 140x 138x 138x 137x 63x 32x 30x 30x 30x 20x 63x 193x 119x 118x 1x 117x 19x 19x 18x 18x 72x 103x 17x 108x 107x 107x 1x 106x 105x 104x 104x 2x 102x 6x 6x 6x 6x 108x 106x 105x 103x 1x 19x 17x 17x 9x 10x 1x 9x 7x 1x 6x 10x 10x 1x 9x 9x 9x 11x 11x 11x 1x 10x 8x 3x 1x 2x 5x 2x 19x 166x 166x 63x 63x 19x 8x 6x 3x 8x 6x 3x 9x 8x 2x 11x 10x | import { gql } from 'graphql-tag';
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
import { ConnectionManager } from '../services/ConnectionManager';
import { getGitLabApiUrlFromContext } from '../oauth/token-context';
import { extractSimpleId, toGid } from '../utils/idConversion';
import { gitlab } from '../utils/gitlab-api';
import { normalizeProjectId } from '../utils/projectIdentifier';
import { encodeApiSegment } from '../utils/api-path';
import type { EffectivePolicy } from './policy';
type Metadata = Record<string, unknown>;
type Query = TypedDocumentNode<Metadata, Record<string, unknown>>;
export type ReadOwnership = (query: Query, variables: Record<string, unknown>) => Promise<Metadata>;
// These roots and project.id exist at v16.0.0-ee: types/query_type.rb,
// types/{work_item,container_repository,todo}_type.rb and ee/types/vulnerability_type.rb.
const WORK_ITEM_OWNER: Query = gql`
query OperatorWorkItemOwner($id: WorkItemID!) {
workItem(id: $id) {
project {
id
}
}
}
`;
const REPOSITORY_OWNER: Query = gql`
query OperatorRepositoryOwner($id: ContainerRepositoryID!) {
containerRepository(id: $id) {
project {
id
}
}
}
`;
const VULNERABILITY_OWNER: Query = gql`
query OperatorVulnerabilityOwner($id: VulnerabilityID!) {
vulnerability(id: $id) {
project {
id
}
}
}
`;
const TODO_OWNER: Query = gql`
query OperatorTodoOwner($id: TodoID!) {
todo(id: $id) {
project {
id
}
}
}
`;
// TimelogResolver at v16.0.0-ee filters projectId before pagination.
const PROJECT_TIMELOGS: Query = gql`
query OperatorProjectTimelogs($projectId: ProjectID!, $after: String) {
timelogs(projectId: $projectId, first: 100, after: $after) {
nodes {
id
}
pageInfo {
endCursor
hasNextPage
}
}
}
`;
/** Ownership verification has a fixed work ceiling and never commits a partial verification. */
const MAX_METADATA_REQUESTS = 100;
type Args = Record<string, unknown>;
interface OwnershipCheck {
allowed: ReadonlySet<string>;
expired: () => boolean;
budget: () => void;
query: ReadOwnership;
owned: (document: Query, field: string, id: string) => Promise<void>;
}
function scalarId(value: unknown): string {
if (typeof value !== 'string' && typeof value !== 'number')
throw new TypeError('Invalid resource ID');
return String(value);
}
function createCheck(
allowed: ReadonlySet<string>,
expired: () => boolean,
read?: ReadOwnership,
): OwnershipCheck {
let requests = 0;
const budget = (): void => {
if (expired()) throw new Error('Tool call expired during project ownership verification');
if (requests >= MAX_METADATA_REQUESTS)
throw new Error('Project ownership verification exceeded its metadata request limit');
requests++;
};
const query: ReadOwnership = async (document, variables) => {
budget();
const result = read
? await read(document, variables)
: await ConnectionManager.getInstance()
.getClient(getGitLabApiUrlFromContext())
.request(document, variables);
if (expired()) throw new Error('Tool call expired during project ownership verification');
return result;
};
const owned = async (document: Query, field: string, id: string): Promise<void> => {
const answer = await query(document, { id });
const row = answer[field] as { project?: { id?: unknown } | null } | null | undefined;
const projectId = row?.project?.id;
if (typeof projectId !== 'string' || !allowed.has(extractSimpleId(projectId)))
throw new Error(`${field} is not owned by a project permitted by GITLAB_ALLOWED_PROJECT_IDS`);
};
return { allowed, expired, budget, query, owned };
}
function addWorkItemId(ids: Set<string>, value: unknown): void {
if (value === undefined || value === null) return;
const id = toGid(scalarId(value), 'WorkItem');
if (!ids.has(id) && ids.size >= MAX_METADATA_REQUESTS)
throw new Error('Too many work item ownership checks in one call');
ids.add(id);
}
function workItemIds(tool: string, args: Args): Set<string> {
const ids = new Set<string>();
// Namespace + IID wins over a global ID, just as in the work item handler.
if (!(tool === 'browse_work_items' && args.namespace !== undefined && args.iid !== undefined))
addWorkItemId(ids, args.id);
for (const field of ['targetId', 'parentId', 'beforeId', 'afterId'])
addWorkItemId(ids, args[field]);
if (Array.isArray(args.childrenIds)) for (const id of args.childrenIds) addWorkItemId(ids, id);
return ids;
}
interface TimelogPage {
nodes: Array<{ id: string }>;
pageInfo: { hasNextPage: boolean; endCursor?: string | null };
}
async function timelogPage(
check: OwnershipCheck,
projectId: string,
after: string | null,
): Promise<TimelogPage> {
const answer = await check.query(PROJECT_TIMELOGS, {
projectId: toGid(projectId, 'Project'),
after,
});
const connection = answer.timelogs as TimelogPage | null;
if (
!Array.isArray(connection?.nodes) ||
connection.nodes.length > 100 ||
typeof connection.pageInfo?.hasNextPage !== 'boolean'
)
throw new Error('GitLab did not return verifiable timelog ownership');
return connection;
}
function timelogCursor(page: TimelogPage, previous: string | null): string | null {
if (!page.pageInfo.hasNextPage) return null;
const cursor = page.pageInfo.endCursor;
if (!cursor || cursor === previous)
throw new Error('GitLab returned an invalid timelog ownership cursor');
return cursor;
}
async function checkTimelog(args: Args, check: OwnershipCheck): Promise<void> {
const id = extractSimpleId(scalarId(args.timelogId));
for (const projectId of check.allowed) {
let after: string | null = null;
for (;;) {
const page = await timelogPage(check, projectId, after);
if (page.nodes.some((entry) => extractSimpleId(entry.id) === id)) return;
after = timelogCursor(page, after);
if (after === null) break;
}
}
throw new Error('Timelog is not owned by a project permitted by GITLAB_ALLOWED_PROJECT_IDS');
}
async function checkWorkItems(tool: string, args: Args, check: OwnershipCheck): Promise<void> {
const ids = workItemIds(tool, args);
const { owned } = check;
// Verify serially so the first denied owner prevents requests for later relationship targets.
for (const id of ids) await owned(WORK_ITEM_OWNER, 'workItem', id);
if (args.action === 'delete_timelog') await checkTimelog(args, check);
}
type SuggestionNote = { suggestions?: Array<{ id: number }> };
function keepSuggestions(notes: SuggestionNote[], wanted: Set<string>): void {
if (!Array.isArray(notes) || notes.length > 20)
throw new Error('GitLab did not return verifiable suggestion ownership');
for (const note of notes) {
if (note.suggestions && note.suggestions.length > MAX_METADATA_REQUESTS)
throw new Error('Too many suggestions in ownership metadata');
for (const suggestion of note.suggestions ?? []) wanted.delete(String(suggestion.id));
}
}
async function checkSuggestions(args: Args, check: OwnershipCheck): Promise<void> {
const ids = args.action === 'apply_suggestion' ? [args.suggestion_id] : args.suggestion_ids;
if (!Array.isArray(ids) || ids.length > MAX_METADATA_REQUESTS)
throw new Error('Too many suggestion ownership checks in one call');
const wanted = new Set(ids.map(scalarId));
let page = 1;
while (wanted.size > 0) {
check.budget();
const result = await gitlab.getPage<SuggestionNote>(
`projects/${normalizeProjectId(scalarId(args.project_id))}/merge_requests/${encodeApiSegment(scalarId(args.merge_request_iid))}/notes`,
{ query: { per_page: 20, page } },
);
if (check.expired())
throw new Error('Tool call expired during suggestion ownership verification');
keepSuggestions(result.items, wanted);
if (result.nextPage === null) break;
if (result.nextPage <= page)
throw new Error('GitLab returned an invalid suggestion ownership cursor');
page = result.nextPage;
}
if (wanted.size > 0)
throw new Error(
'GitLab did not confirm that every suggestion belongs to the permitted merge request',
);
}
/** Check real owners of global IDs; a supplied project hint is not ownership evidence. */
export async function assertOperatorOwnership(
tool: string,
args: Record<string, unknown>,
policy: EffectivePolicy,
expired: () => boolean,
read?: ReadOwnership,
): Promise<void> {
const allowed = policy.operatorProjectIds;
if (!allowed) return;
const check = createCheck(allowed, expired, read);
switch (tool) {
case 'browse_work_items':
case 'manage_work_item':
return checkWorkItems(tool, args, check);
case 'browse_registry':
case 'manage_registry':
if (args.repository_id !== undefined)
await check.owned(
REPOSITORY_OWNER,
'containerRepository',
`gid://gitlab/ContainerRepository/${extractSimpleId(scalarId(args.repository_id))}`,
);
return;
case 'browse_vulnerabilities':
case 'manage_vulnerability':
if (args.vulnerability_id !== undefined)
await check.owned(
VULNERABILITY_OWNER,
'vulnerability',
`gid://gitlab/Vulnerability/${extractSimpleId(scalarId(args.vulnerability_id))}`,
);
return;
case 'manage_todos':
if (args.id !== undefined)
await check.owned(
TODO_OWNER,
'todo',
`gid://gitlab/Todo/${extractSimpleId(scalarId(args.id))}`,
);
return;
case 'manage_mr_discussion':
if (args.action === 'apply_suggestion' || args.action === 'apply_suggestions')
await checkSuggestions(args, check);
}
}
|