refactor: deduplicate shared utilities + fix cookie store#21
Conversation
- Extract `getSesskey()` into `services/sesskey.ts` (was duplicated in scraper.ts and dashboard.ts) - Extract `isLoginHtml()` into `utils/moodle-url.ts` (was copy-pasted across 4 service files) - Fix cookie store: validate Date before assigning expires (malformed Set-Cookie headers created Invalid Date objects that were never cleaned up) No behavioral changes — all imports updated, TypeScript compiles clean.
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR consolidates duplicate utility functions across multiple service files. The Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR refactors duplicated Moodle session helpers into shared utilities and hardens cookie parsing to avoid persisting invalid expiry dates, reducing repeated logic across multiple LMS service modules.
Changes:
- Extract shared
getSesskey()intosrc/services/sesskey.tsand update callers. - Extract shared
isLoginHtml()intosrc/utils/moodle-url.tsand replace copy-pasted implementations across services. - Validate parsed
expiresdates in the cookie store before assigning them.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/utils/moodle-url.ts | Adds shared isLoginHtml(html) helper for detecting Moodle login pages. |
| src/services/sesskey.ts | Introduces shared getSesskey() implementation for sesskey extraction. |
| src/services/scraper.ts | Switches from local sesskey extraction to shared getSesskey(). |
| src/services/resources-scraper.ts | Replaces local isLoginHtml with shared utility import. |
| src/services/lms-download.ts | Replaces local isLoginHtml with shared utility import. |
| src/services/feedback-autofill.ts | Replaces local isLoginHtml with shared utility import. |
| src/services/dashboard.ts | Switches from local sesskey extraction to shared getSesskey(). |
| src/services/cookie-store.ts | Guards against assigning Invalid Date to cookie expires. |
| src/services/assignment.ts | Imports and uses shared isLoginHtml, removing local duplicate. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Preview |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/utils/moodle-url.ts (1)
5-10: Make login marker matching resilient to case/quote variations.Line 5–Line 10 currently depend on exact substrings (double quotes + exact casing). A small regex-based check would avoid false negatives on equivalent HTML.
♻️ Suggested hardening
export const isLoginHtml = (html: string): boolean => { - const normalized = html.replace(/\s+/g, " "); + const normalized = html.toLowerCase().replace(/\s+/g, " "); return ( - normalized.includes('name="logintoken"') || - normalized.includes('id="login"') || + /name\s*=\s*["']logintoken["']/.test(normalized) || + /id\s*=\s*["']login["']/.test(normalized) || normalized.includes("/login/index.php") ); };🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/moodle-url.ts` around lines 5 - 10, The current checks on normalized (the string produced by normalized = html.replace(/\s+/g, " ")) use exact substring matches and fail for different casing or single quotes; replace the three includes checks with case-insensitive regex tests that allow optional whitespace and either single or double quotes (e.g., test for name\s*=\s*['"]logintoken['"] and id\s*=\s*['"]login['"], and test for /login/index.php with an i flag) so the return expression uses those regex.test(normalized) calls instead of includes to make login marker matching resilient to case/quote variations.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/services/sesskey.ts`:
- Around line 9-10: The debug.scraper call is logging the raw sesskey
(match[1]), which leaks an authentication token; remove or replace that
raw-value log so you do not emit the full sesskey. Update the debug.scraper
invocation that currently references match[1] to either log a non-sensitive
message like "sesskey found" or a masked version (e.g., show only a fixed
prefix/suffix or fixed placeholder) while leaving the return of match[1]
unchanged; also scan the sesskey helper for any other logs that reference
match[1] and remove or mask them similarly.
---
Nitpick comments:
In `@src/utils/moodle-url.ts`:
- Around line 5-10: The current checks on normalized (the string produced by
normalized = html.replace(/\s+/g, " ")) use exact substring matches and fail for
different casing or single quotes; replace the three includes checks with
case-insensitive regex tests that allow optional whitespace and either single or
double quotes (e.g., test for name\s*=\s*['"]logintoken['"] and
id\s*=\s*['"]login['"], and test for /login/index.php with an i flag) so the
return expression uses those regex.test(normalized) calls instead of includes to
make login marker matching resilient to case/quote variations.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 760973b0-b3dc-47f1-a3cd-3e0b1c9e93cd
📒 Files selected for processing (9)
src/services/assignment.tssrc/services/cookie-store.tssrc/services/dashboard.tssrc/services/feedback-autofill.tssrc/services/lms-download.tssrc/services/resources-scraper.tssrc/services/scraper.tssrc/services/sesskey.tssrc/utils/moodle-url.ts
Summary
getSesskey()into sharedservices/sesskey.ts(was identical inscraper.tsanddashboard.ts)isLoginHtml()intoutils/moodle-url.ts(was copy-pasted across 4 service files:assignment.ts,resources-scraper.ts,feedback-autofill.ts,lms-download.ts)Datebefore assigningexpires— malformedSet-Cookieheaders createdInvalid Dateobjects that could never be garbage-collectedNet: -29 lines. No behavioral changes. TypeScript compiles clean.
Test plan
🤖 Generated with Claude Code