Skip to content

Commit 491f680

Browse files
committed
fix(api): validate cookie session data
1 parent 4c0fa3d commit 491f680

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
unraid_login|i:1736523078;unraid_user|s:4:"root";locale|s:0:"";buildDate|s:8:"20241202";

api/src/unraid-api/auth/cookie.service.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { Test, type TestingModule } from '@nestjs/testing';
1+
import type { TestingModule } from '@nestjs/testing';
2+
import { Test } from '@nestjs/testing';
3+
import { writeFile } from 'node:fs/promises';
4+
5+
import { emptyDir } from 'fs-extra';
6+
import { afterAll, beforeAll, describe, it } from 'vitest';
7+
28
import { CookieService, SESSION_COOKIE_CONFIG } from './cookie.service';
3-
import { describe, it, beforeAll, afterAll } from 'vitest';
4-
import { emptyDir, ensureFile } from 'fs-extra';
59

610
describe.concurrent('CookieService', () => {
711
let service: CookieService;
@@ -10,7 +14,11 @@ describe.concurrent('CookieService', () => {
1014
// helper to create a session file
1115
function makeSession(sessionId: string, cookieService: CookieService = service) {
1216
const path = cookieService.getSessionFilePath(sessionId);
13-
return ensureFile(path);
17+
return writeFile(
18+
path,
19+
`unraid_login|i:1736523078;unraid_user|s:4:"root";locale|s:0:"";buildDate|s:8:"20241202";`,
20+
'ascii'
21+
);
1422
}
1523

1624
beforeAll(async () => {

api/src/unraid-api/auth/cookie.service.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Inject, Injectable } from '@nestjs/common';
1+
import { Inject, Injectable, Logger } from '@nestjs/common';
2+
import { readFile } from 'fs/promises';
23
import { join } from 'path';
34

45
import { fileExists } from '@app/core/utils/files/file-exists';
@@ -18,6 +19,7 @@ type SessionCookieConfig = {
1819

1920
@Injectable()
2021
export class CookieService {
22+
private readonly logger = new Logger(CookieService.name);
2123
constructor(
2224
@Inject(SESSION_COOKIE_CONFIG) readonly opts: SessionCookieConfig = CookieService.defaultOpts()
2325
) {}
@@ -60,10 +62,17 @@ export class CookieService {
6062
*/
6163
private async isValidAuthCookie(cookieName: string, cookieValue: string): Promise<boolean> {
6264
const { namePrefix } = this.opts;
63-
if (!cookieName.startsWith(namePrefix)) {
65+
const sessionFile = this.getSessionFilePath(cookieValue);
66+
if (!cookieName.startsWith(namePrefix) || !(await fileExists(sessionFile))) {
67+
return false;
68+
}
69+
try {
70+
const sessionData = await readFile(sessionFile, 'ascii');
71+
return sessionData.includes('unraid_login') && sessionData.includes('unraid_user');
72+
} catch (e) {
73+
this.logger.error(e, 'Error reading session file');
6474
return false;
6575
}
66-
return fileExists(this.getSessionFilePath(cookieValue));
6776
}
6877

6978
/**

0 commit comments

Comments
 (0)