Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/helpers/global.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const parseIdFromUrl = (url: string): number => {
if (/^\d+-/.test(p)) {
return +p.split('-')[0] || null;
}
// Also support pure numbers in URL parts (e.g., /uzivatel/1000/)
if (/^\d+$/.test(p)) {
return +p || null;
}
Comment on lines +19 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Normalize path segments before numeric ID matching.

Line 20 fails for URLs where the numeric segment includes query/hash suffixes (e.g., /uzivatel/1000/?foo=bar). Strip ?/# first so numeric URLs are parsed consistently.

Suggested fix
-    const p = parts[i];
+    const p = parts[i]?.split(/[?#]/)[0] ?? '';
     if (/^\d+-/.test(p)) {
       return +p.split('-')[0] || null;
     }
     // Also support pure numbers in URL parts (e.g., /uzivatel/1000/)
     if (/^\d+$/.test(p)) {
       return +p || null;
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/helpers/global.helper.ts` around lines 19 - 22, Normalize the path
segment before checking for a pure numeric ID: in the logic that tests the
variable p with /^\d+$/ (the numeric ID branch), first strip any query or hash
suffix by taking the substring up to the first '?' or '#' (e.g.,
p.split(/[?#]/)[0]) then run the /^\d+$/ test and return the numeric value
(+parsed || null); update the numeric branch that references p so it uses the
cleaned segment.

}

// Fallback
Expand Down
11 changes: 8 additions & 3 deletions src/services/user-ratings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement, parse } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDUserRatingConfig, CSFDUserRatings } from '../dto/user-ratings';
import { fetchPage } from '../fetchers';
import { sleep } from '../helpers/global.helper';
import { extractId, sleep } from '../helpers/global.helper';
import {
getUserRating,
getUserRatingColorRating,
Expand All @@ -22,9 +22,14 @@ export class UserRatingsScraper {
config?: CSFDUserRatingConfig,
options?: CSFDOptions
): Promise<CSFDUserRatings[]> {
const id = extractId(user);
if (id === null || isNaN(id)) {
throw new Error('node-csfd-api: user must be a valid number or URL');
}
Comment on lines +25 to +28

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Strengthen user ID guard to positive integers only.

Line 26 still allows values like 0, negative, decimal, or Infinity for numeric inputs. Reject those early to keep request URLs valid and behavior consistent.

Suggested fix
     const id = extractId(user);
-    if (id === null || isNaN(id)) {
+    if (id === null || !Number.isInteger(id) || id <= 0) {
       throw new Error('node-csfd-api: user must be a valid number or URL');
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const id = extractId(user);
if (id === null || isNaN(id)) {
throw new Error('node-csfd-api: user must be a valid number or URL');
}
const id = extractId(user);
if (id === null || !Number.isInteger(id) || id <= 0) {
throw new Error('node-csfd-api: user must be a valid number or URL');
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/services/user-ratings.service.ts` around lines 25 - 28, The current guard
using extractId(user) allows 0, negatives, decimals, and Infinity; update the
check after calling extractId to ensure id is a finite positive integer (id > 0
&& Number.isInteger(id) && Number.isFinite(id)) and reject anything else by
throwing the same Error('node-csfd-api: user must be a valid number or URL');
modify the validation logic in the user-ratings.service.ts around the extractId
call to enforce these constraints (reference function/variable: extractId and
id).


let allMovies: CSFDUserRatings[] = [];
const pageToFetch = config?.page || 1;
const url = userRatingsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userRatingsUrl(id, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -40,7 +45,7 @@ export class UserRatingsScraper {
if (config?.allPages) {
for (let i = 2; i <= pages; i++) {
config.onProgress?.(i, pages);
const url = userRatingsUrl(user, i, { language: options?.language });
const url = userRatingsUrl(id, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down
11 changes: 8 additions & 3 deletions src/services/user-reviews.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HTMLElement, parse } from 'node-html-parser';
import { CSFDColorRating, CSFDFilmTypes, CSFDStars } from '../dto/global';
import { CSFDUserReviews, CSFDUserReviewsConfig } from '../dto/user-reviews';
import { fetchPage } from '../fetchers';
import { sleep } from '../helpers/global.helper';
import { extractId, sleep } from '../helpers/global.helper';
import {
getUserReviewColorRating,
getUserReviewDate,
Expand All @@ -24,9 +24,14 @@ export class UserReviewsScraper {
config?: CSFDUserReviewsConfig,
options?: CSFDOptions
): Promise<CSFDUserReviews[]> {
const id = extractId(user);
if (id === null || isNaN(id)) {
throw new Error('node-csfd-api: user must be a valid number or URL');
}

let allReviews: CSFDUserReviews[] = [];
const pageToFetch = config?.page || 1;
const url = userReviewsUrl(user, pageToFetch > 1 ? pageToFetch : undefined, {
const url = userReviewsUrl(id, pageToFetch > 1 ? pageToFetch : undefined, {
language: options?.language
});
const response = await fetchPage(url, { ...options?.request });
Expand All @@ -42,7 +47,7 @@ export class UserReviewsScraper {
if (config?.allPages) {
for (let i = 2; i <= pages; i++) {
config.onProgress?.(i, pages);
const url = userReviewsUrl(user, i, { language: options?.language });
const url = userReviewsUrl(id, i, { language: options?.language });
const response = await fetchPage(url, { ...options?.request });

const items = parse(response);
Expand Down