Skip to content
Snippets Groups Projects
Unverified Commit dd889ed2 authored by Rafael Tapia's avatar Rafael Tapia Committed by GitHub
Browse files

fix: url not being properly mounted (#35115)

parent a49eb988
No related merge requests found
---
"@rocket.chat/cas-validate": patch
---
Fixes a problem with CAS when URL connection ended with `/`
import server from '@rocket.chat/jest-presets/server';
import type { Config } from 'jest';
export default {
projects: [
{
displayName: 'server',
preset: server.preset,
testMatch: ['<rootDir>/src/**/*.spec.[jt]s?(x)'],
},
],
} satisfies Config;
...@@ -11,7 +11,8 @@ ...@@ -11,7 +11,8 @@
"lint": "eslint --ext .js,.jsx,.ts,.tsx .", "lint": "eslint --ext .js,.jsx,.ts,.tsx .",
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx . --fix", "lint:fix": "eslint --ext .js,.jsx,.ts,.tsx . --fix",
"build": "rm -rf dist && tsc -p tsconfig.json", "build": "rm -rf dist && tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch --preserveWatchOutput" "dev": "tsc -p tsconfig.json --watch --preserveWatchOutput",
"testunit": "jest"
}, },
"main": "./dist/index.js", "main": "./dist/index.js",
"typings": "./dist/index.d.ts", "typings": "./dist/index.d.ts",
...@@ -19,6 +20,7 @@ ...@@ -19,6 +20,7 @@
"/dist" "/dist"
], ],
"dependencies": { "dependencies": {
"cheerio": "1.0.0" "cheerio": "1.0.0",
"jest": "^29.7.0"
} }
} }
import { getQueryPath } from './validate';
describe('getQueryPath', () => {
it('should generate the correct query path without trailing slash', () => {
const partialPathname = '/cas';
const validatePath = 'validate';
const query = { ticket: 'ST-12345', service: 'https://myapp.com' };
const result = getQueryPath(partialPathname, validatePath, query);
expect(result).toBe('/cas/validate?ticket=ST-12345&service=https%3A%2F%2Fmyapp.com');
});
it('should generate the correct query path with trailing slash', () => {
const partialPathname = '/cas/';
const validatePath = 'validate';
const query = { ticket: 'ST-12345', service: 'https://myapp.com' };
const result = getQueryPath(partialPathname, validatePath, query);
expect(result).toBe('/cas/validate?ticket=ST-12345&service=https%3A%2F%2Fmyapp.com');
});
it('should generate the correct query path with `/` partialPathname', () => {
const partialPathname = '/';
const validatePath = 'validate';
const query = { ticket: 'ST-12345', service: 'https://myapp.com' };
const result = getQueryPath(partialPathname, validatePath, query);
expect(result).toBe('/validate?ticket=ST-12345&service=https%3A%2F%2Fmyapp.com');
});
it('should generate the correct query path with empty partialPathname', () => {
const partialPathname = '';
const validatePath = 'validate';
const query = { ticket: 'ST-12345', service: 'https://myapp.com' };
const result = getQueryPath(partialPathname, validatePath, query);
expect(result).toBe('/validate?ticket=ST-12345&service=https%3A%2F%2Fmyapp.com');
});
it('should generate the correct query path with empty query', () => {
const partialPathname = '/cas';
const validatePath = 'validate';
const query = {};
const result = getQueryPath(partialPathname, validatePath, query);
expect(result).toBe('/cas/validate');
});
});
import type { IncomingMessage } from 'http'; import type { IncomingMessage } from 'http';
import https from 'https'; import https from 'https';
import type { ParsedUrlQueryInput } from 'querystring';
import url from 'url'; import url from 'url';
import type { Cheerio, CheerioAPI } from 'cheerio'; import type { Cheerio, CheerioAPI } from 'cheerio';
...@@ -149,6 +150,19 @@ function parseAttributes(elemSuccess: Cheerio<any>, cheerio: CheerioAPI): Record ...@@ -149,6 +150,19 @@ function parseAttributes(elemSuccess: Cheerio<any>, cheerio: CheerioAPI): Record
return attributes; return attributes;
} }
export function getQueryPath(
partialPathname: string,
validatePath: string,
query: string | ParsedUrlQueryInput | null | undefined,
): string {
const pathname = partialPathname?.endsWith('/') ? partialPathname + validatePath : `${partialPathname}/${validatePath}`;
return url.format({
pathname,
query,
});
}
export function validate(options: CasOptions, ticket: string, callback: CasCallback, renew = false): void { export function validate(options: CasOptions, ticket: string, callback: CasCallback, renew = false): void {
if (!options.base_url) { if (!options.base_url) {
throw new Error('Required CAS option `base_url` missing.'); throw new Error('Required CAS option `base_url` missing.');
...@@ -176,10 +190,7 @@ export function validate(options: CasOptions, ticket: string, callback: CasCallb ...@@ -176,10 +190,7 @@ export function validate(options: CasOptions, ticket: string, callback: CasCallb
...(renew ? { renew: 1 } : {}), ...(renew ? { renew: 1 } : {}),
}; };
const queryPath = url.format({ const queryPath = getQueryPath(pathname ?? '/', validatePath, query);
pathname: `${pathname}/${validatePath}`,
query,
});
const req = https.get( const req = https.get(
{ {
......
...@@ -7566,6 +7566,7 @@ __metadata: ...@@ -7566,6 +7566,7 @@ __metadata:
dependencies: dependencies:
cheerio: "npm:1.0.0" cheerio: "npm:1.0.0"
eslint: "npm:~8.45.0" eslint: "npm:~8.45.0"
jest: "npm:^29.7.0"
typescript: "npm:~5.7.2" typescript: "npm:~5.7.2"
languageName: unknown languageName: unknown
linkType: soft linkType: soft
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment