Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions e2e/data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { test, expect, type Page } from '@playwright/test';
import { collect, expectClean, waitForShell } from './support/harness.ts';
import { PROBLEMS, CONTESTS, LEADERBOARD } from './support/fixtures.ts';
import { setScenario } from './support/scenario.ts';
import { getProblemSource } from '../src/lib/services/problemSource.ts';

test.beforeEach(async () => {
await setScenario('data');
Expand Down Expand Up @@ -67,15 +68,15 @@ test.describe('problems data', () => {

// First click -> codeforces only.
await sourceFilter.click();
const codeforces = PROBLEMS.filter((p) => !p.url.includes('kattis.com'));
const codeforces = PROBLEMS.filter((p) => getProblemSource(p.url) === 'codeforces');
await expect(rows).toHaveCount(codeforces.length);
for (const p of codeforces) {
await expect(page.getByRole('link', { name: p.name, exact: true })).toBeVisible();
}

// Second click -> kattis only.
await sourceFilter.click();
const kattis = PROBLEMS.filter((p) => p.url.includes('kattis.com'));
const kattis = PROBLEMS.filter((p) => getProblemSource(p.url) === 'kattis');
await expect(rows).toHaveCount(kattis.length);
for (const p of kattis) {
await expect(page.getByRole('link', { name: p.name, exact: true })).toBeVisible();
Expand Down
8 changes: 1 addition & 7 deletions src/lib/services/problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { supabase } from './database';
import { user } from './auth';
import { getProblemSource } from './problemSource';
import { get } from 'svelte/store';

/**
Expand Down Expand Up @@ -52,13 +53,6 @@ export type UserSolvedProblem = {
solved_at: string;
};

/**
* Determine the problem source based on URL
*/
export function getProblemSource(url: string): 'codeforces' | 'kattis' {
return url.includes('kattis.com') ? 'kattis' : 'codeforces';
}

/**
* Columns selected when reading problems for display. Kept explicit so we only
* fetch what the UI needs instead of every row column.
Expand Down
6 changes: 6 additions & 0 deletions src/lib/services/problemSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { parseKattisProblemId } from './kattisUrl.ts';

/** Determine the problem source using the exact-host Kattis URL validator. */
export function getProblemSource(url: string): 'codeforces' | 'kattis' {
return parseKattisProblemId(url) ? 'kattis' : 'codeforces';
}
24 changes: 24 additions & 0 deletions tests/problem-source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { getProblemSource } from '../src/lib/services/problemSource.ts';

test('classifies canonical Kattis problem URLs', () => {
assert.equal(getProblemSource('https://open.kattis.com/problems/hello'), 'kattis');
assert.equal(getProblemSource('https://kattis.com/problems/hello'), 'kattis');
});

test('classifies ordinary Codeforces problem URLs', () => {
assert.equal(getProblemSource('https://codeforces.com/problemset/problem/1/A'), 'codeforces');
});

test('does not classify URLs by an untrusted hostname substring', () => {
for (const url of [
'https://open.kattis.com.evil.example/problems/hello',
'https://evil.example/open.kattis.com/problems/hello',
'https://open.kattis.com@evil.example/problems/hello',
'https://evil.example/?next=https://open.kattis.com/problems/hello',
'not-a-url-containing-kattis.com'
]) {
assert.equal(getProblemSource(url), 'codeforces');
}
});
Loading