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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.1] - 2026-08-20

### Fixed

- Fixed ReDoS vulnerability in `parseContributionCalendar` by using isolated, non-nested regex execution and simpler loops instead of complex groups. (PR #25 merged via SNAKE-001)
- Resolved GitHub's HTML contribution scraping edge case algorithms reducing parsing times.
- Resolved Biome configuration inconsistencies and updated syntax to 2.5.9 schemas.

### Changed

- Bumped all project dependencies via audit to address medium/high security vulnerabilities across astero, elysia, sharp and dompurify.
- Added comprehensive unit tests for theming engine logic with valid assertions. (PR #26 merged via SNAKE-001)

## [1.2.4] - 2025-12-22

### Security
Expand Down
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/api",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@snake-evolution/web",
"type": "module",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"scripts": {
"dev": "astro dev",
Expand Down
17 changes: 9 additions & 8 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "snake-evolution",
"private": true,
"version": "1.3.0",
"version": "1.3.1",
"description": "Transform your GitHub contribution graph into an epic snake animation",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/biome-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/biome-config",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"license": "MIT",
"description": "Shared Biome configuration for snake-evolution monorepo",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/cli",
"version": "1.3.0",
"version": "1.3.1",
"description": "Generate animated snake eating your GitHub contributions",
"license": "MIT",
"author": "Miccy <support@miccy.dev>",
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/engine",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"type": "module",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/github/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/github",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"type": "module",
"main": "./src/index.ts",
Expand Down
188 changes: 101 additions & 87 deletions packages/github/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,101 @@ function levelToNumber(level: ContributionDay["contributionLevel"]): 0 | 1 | 2 |
// HTML Scraping (public, no auth)
// ============================================

/** @internal */
export function parseContributionCalendar(
html: string,
username: string,
year: number,
): ContributionGrid {
const weeks: GitHubContribution[][] = [];
let currentWeek: GitHubContribution[] = [];
let totalContributions = 0;

// Extremely safe split-based parsing to avoid any ReDoS possibilities
const daysMap = new Map<string, { date: string; level: number; count?: number }>();

// Split the HTML by '<td' to isolate table cells, which avoids regex backtracking on large documents
const tdChunks = html.split("<td");

for (let i = 1; i < tdChunks.length; i++) {
const chunk = tdChunks[i];

// Only process chunks that are actually data cells (contain data-date)
if (!chunk.includes("data-date=")) continue;

// Extract date (safe regex on a small substring)
const dateMatch = /data-date="(\d{4}-\d{2}-\d{2})"/.exec(chunk);
if (!dateMatch) continue;

const date = dateMatch[1];

// Extract level
const levelMatch = /data-level="(-?\d+)"/.exec(chunk);
if (!levelMatch) continue;

const level = Number.parseInt(levelMatch[1], 10);

// Extract optional count
let count: number | undefined;
const countMatch = /data-count="(\d+)"/.exec(chunk);
if (countMatch) {
count = Number.parseInt(countMatch[1], 10);
}

if (!daysMap.has(date)) {
daysMap.set(date, { date, level, count });
}
}

const days = Array.from(daysMap.values());

// Sort
days.sort((a, b) => a.date.localeCompare(b.date));

// Build grid with Sunday alignment
if (days.length > 0) {
const [y, m, d] = days[0].date.split("-").map(Number);
const dayOfWeek = new Date(Date.UTC(y, m - 1, d)).getUTCDay(); // 0 = Sunday

// Pad beginning
for (let i = 0; i < dayOfWeek; i++) {
currentWeek.push({ date: "", count: 0, level: 0 });
}
}

for (const day of days) {
const level = Math.max(0, Math.min(4, day.level)) as 0 | 1 | 2 | 3 | 4;
const count = day.count ?? contributionCountFromLevel(level);

totalContributions += count;
currentWeek.push({
date: day.date,
count,
level,
});

if (currentWeek.length === 7) {
weeks.push(currentWeek);
currentWeek = [];
}
}

if (currentWeek.length > 0) {
// Pad end
while (currentWeek.length < 7) {
currentWeek.push({ date: "", count: 0, level: 0 });
}
weeks.push(currentWeek);
}
Comment thread
miccy marked this conversation as resolved.
Resolved

return {
username,
year,
totalContributions,
weeks,
};
}

// Helper to fetch a specific calendar year
async function fetchYear(username: string, year: number): Promise<ContributionGrid> {
const url = `https://github.com/users/${username}/contributions?from=${year}-01-01&to=${year}-12-31`;
Expand Down Expand Up @@ -242,9 +337,12 @@ async function fetchContributionsHTML(username: string, year?: number): Promise<
allDays.sort((a, b) => a.date.localeCompare(b.date));

// 3. Remove duplicates (overlap at year boundary?)
const uniqueDays = allDays.filter(
(day, index, self) => index === self.findIndex((d) => d.date === day.date),
);
const seenDates = new Set<string>();
const uniqueDays = allDays.filter((day) => {
if (seenDates.has(day.date)) return false;
seenDates.add(day.date);
return true;
});

// 4. Slice last 365 days (approx 53 weeks)
// We want the graph to end TODAY.
Expand Down Expand Up @@ -321,90 +419,6 @@ function contributionCountFromLevel(level: number): number {
return level * 3;
}

function parseContributionCalendar(html: string, username: string, year: number): ContributionGrid {
const weeks: GitHubContribution[][] = [];
let currentWeek: GitHubContribution[] = [];
let totalContributions = 0;

// Match all contribution day cells
// GitHub uses: <td ... data-date="2025-01-01" data-count="4" data-level="3" ...>
// We use a robust regex that captures date, count (optional), and level
const dayPattern =
/<td[^>]*data-date="(\d{4}-\d{2}-\d{2})"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-level="(-?\d+)"[^>]*>/g;
const days: Array<{ date: string; level: number; count?: number }> = [];

for (const match of html.matchAll(dayPattern)) {
days.push({
date: match[1],
level: Number.parseInt(match[3], 10),
count: match[2] ? Number.parseInt(match[2], 10) : undefined,
});
}

// Also try the alternate pattern (data-level before data-date)
const altPattern =
/<td[^>]*data-level="(-?\d+)"[^>]*?(?:data-count="(\d+)"[^>]*?)?data-date="(\d{4}-\d{2}-\d{2})"[^>]*>/g;
for (const match of html.matchAll(altPattern)) {
// Avoid duplicates
// Capturing groups: 1=level, 2=count(opt), 3=date
const date = match[3];
if (!days.some((d) => d.date === date)) {
days.push({
date,
level: Number.parseInt(match[1], 10),
count: match[2] ? Number.parseInt(match[2], 10) : undefined,
});
}
}

// Sort
days.sort((a, b) => a.date.localeCompare(b.date));

// Build grid with Sunday alignment
if (days.length > 0) {
// Parse date in UTC to ensure consistent day of week
const [y, m, d] = days[0].date.split("-").map(Number);
const dayOfWeek = new Date(Date.UTC(y, m - 1, d)).getUTCDay(); // 0 = Sunday

// Pad beginning
for (let i = 0; i < dayOfWeek; i++) {
currentWeek.push({ date: "", count: 0, level: 0 });
}
}

for (const day of days) {
const level = Math.max(0, Math.min(4, day.level)) as 0 | 1 | 2 | 3 | 4;
const count = day.count ?? contributionCountFromLevel(level);

totalContributions += count;
currentWeek.push({
date: day.date,
count,
level,
});

if (currentWeek.length === 7) {
weeks.push(currentWeek);
currentWeek = [];
}
}

if (currentWeek.length > 0) {
// Pad end
while (currentWeek.length < 7) {
currentWeek.push({ date: "", count: 0, level: 0 });
}
weeks.push(currentWeek);
}

return {
username,
year,
totalContributions,
weeks,
};
}

// ============================================
// Exports
// ============================================
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/renderer",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"type": "module",
"main": "./src/index.ts",
Expand Down
57 changes: 57 additions & 0 deletions packages/renderer/src/themes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, test } from "bun:test";
import type { ColorPalette, GlassTheme } from "./themes";
import { getGlassFilterDefs, getTheme, githubDark, glass, isGlassTheme } from "./themes";

describe("Themes", () => {
describe("isGlassTheme", () => {
test("should return true for the official glass theme", () => {
expect(isGlassTheme(glass)).toBe(true);
});

test("should return true for a custom glass theme object", () => {
const customGlass: GlassTheme = {
...githubDark,
glass: {
blur: 10,
opacity: 0.5,
borderOpacity: 0.1,
gradientStart: "white",
gradientEnd: "black",
},
};
expect(isGlassTheme(customGlass)).toBe(true);
});

test("should return false for github-dark theme", () => {
expect(isGlassTheme(githubDark)).toBe(false);
});

test("should return false for a regular palette without glass property", () => {
const regular: ColorPalette = { ...githubDark };
expect(isGlassTheme(regular)).toBe(false);
});
});

describe("getTheme", () => {
test("should return the requested theme", () => {
expect(getTheme("glass")).toBe(glass);
expect(getTheme("github-dark")).toBe(githubDark);
});

test("should return github-dark as fallback for unknown themes", () => {
expect(getTheme("non-existent")).toBe(githubDark);
});
});

describe("getGlassFilterDefs", () => {
test("should return SVG string containing theme values", () => {
const defs = getGlassFilterDefs(glass);
expect(defs).toContain('id="glass-blur"');
expect(defs).toContain('id="glass-gradient"');
expect(defs).toContain('id="glass-glow"');
expect(defs).toContain(`stdDeviation="${glass.glass.blur}"`);
expect(defs).toContain(`stop-color="${glass.glass.gradientStart}"`);
expect(defs).toContain(`stop-color="${glass.glass.gradientEnd}"`);
});
});
});
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/types",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"type": "module",
"main": "./src/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snake-evolution/typescript-config",
"version": "1.3.0",
"version": "1.3.1",
"private": true,
"license": "MIT",
"publishConfig": {
Expand Down
Loading
Loading