Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL incorrectly interpreted as Regex? #464

Closed
firecrauter opened this issue Oct 28, 2024 · 1 comment · Fixed by #465
Closed

URL incorrectly interpreted as Regex? #464

firecrauter opened this issue Oct 28, 2024 · 1 comment · Fixed by #465

Comments

@firecrauter
Copy link

invalid-regex-test.ts

import { CookieJar } from 'tough-cookie';

async function testInvalidRegex() {
    try {
        const cookieJar = new CookieJar();

        //const urlWithInvalidPath = 'https://www.google.com/Antonov++Andrii/';
        const urlWithInvalidPath = 'https://www.google.com/Y+C++S+T+U+D+I+O/';

        // Add a cookie to this URL
        cookieJar.setCookieSync('test_cookie=value', urlWithInvalidPath);

        // Try to retrieve the cookies for this URL
        const cookies = cookieJar.getCookieStringSync(urlWithInvalidPath);
        console.log(`Cookies for ${urlWithInvalidPath}:`, cookies);
    } catch (error) {
        console.error('Error:', error);
    }
}

testInvalidRegex();

package.json

  "scripts": {
    "build": "tsc",
    "test-invalid-regex": "ts-node src/invalid-regex-test.ts"
  },

npm run test-invalid-regex
output:

Error: SyntaxError: Invalid regular expression: /^/Y+C++S+T+U+D+I+O/: Nothing to repeat
  at new RegExp (<anonymous>)
  at pathMatch (D:\fire\test\node_modules\tough-cookie\dist\pathMatch.js:35:13)
  at matchRFC (D:\fire\test\node_modules\tough-cookie\dist\memstore.js:68:51)
  at D:\fire\test\node_modules\tough-cookie\dist\memstore.js:87:13
  at Array.forEach (<anonymous>)
  at MemoryCookieStore.findCookies (D:\fire\test\node_modules\tough-cookie\dist\memstore.js:82:17)
  at CookieJar.getCookies (D:\fire\test\node_modules\tough-cookie\dist\cookie\cookieJar.js:536:15)
  at CookieJar.getCookieString (D:\fire\test\node_modules\tough-cookie\dist\cookie\cookieJar.js:597:14)
  at CookieJar.callSync (D:\fire\test\node_modules\tough-cookie\dist\cookie\cookieJar.js:168:16)
  at CookieJar.getCookieStringSync (D:\fire\test\node_modules\tough-cookie\dist\cookie\cookieJar.js:610:22)

apify/crawlee#2724

@firecrauter firecrauter changed the title URL incorrectly interpreted as Regex URL incorrectly interpreted as Regex? Oct 28, 2024
@firecrauter
Copy link
Author

firecrauter commented Oct 29, 2024

Resolved by keeping only the domain

        const urlWithInvalidPath = 'https://www.google.com/Y+C++S+T+U+D+I+O/';
        const urlWithoutPath = new URL(urlWithInvalidPath);
        urlWithoutPath.pathname = '/';
        const url = urlWithoutPath.toString();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant