0

I have the following problem:

I am using NodeJS with Express and MongoDB to query my database.

I have a document in the collection of "domains" containing the field "domain".

For example:

{ 
  "domain" : "mydomain.com, www.mydomain.com, beta.mydomain.com, *.beta.mydomain.com",
  "APIKeys" : [ "Public" : 111111 ]
}

Or another document:

{ 
  "domain" : "example.com, *.example.com",
  "APIKeys" : [ "Public" : 222222 ]
}

I would like to query the database and return the result if extractHostname(req.get('Referrer')) matches any of the domains in the field.

var collection = 'domains';
var query = { $and: [ { 'APIKeys.Public' : req.query.APIKey }, {'domain' : extractHostname(req.get('Referrer')) } ] };
var projection = { '_id' : 1 , 'playerPref' : 1 };

For example: extractHostname(req.get('Referrer')) = beta.mydomain.com it should return true, since it matches the regex of beta.mydomain.com.

'test.beta.mydomain.com' should return true since it matches the regex of *.beta.mydomain.com.

'test.www.mydomain.com' should return false.

'www.mydomain.com.maliciousdomain.com' should return false.

Any idea how I can make a query like this to check if the Referrer is in matching conditions?

The problem I am facing is that any of the strings in the field need to match the query, and not the other way around. Keeping in mind the wildcard in the field as opposed in the search string. (It is like a reserve regex?)

Kind regards, Hugo

2
  • If you want it to include beta.mydomain.com then just use that as your regular expression Commented Jul 5, 2018 at 15:27
  • I updated my question to reflect the problem better. The input is the Hostname of the Referrer. This should be checked against the database to see if it matches any of the domains in the field, including the wildcard. Commented Jul 5, 2018 at 15:35

1 Answer 1

1

After seeing your updated requirements I've created a pattern that will match any of your various domain types. Here it is

(?(?=.* .*)(([^ \n>]*)(?:.*))|([^\w\W]))

It will always match mydomain.com, with an optional www., optional beta. and optional wildcard.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer, but the subdomains can be different per document and is just an example how it should match. I updated my question again to better reflect this.
So if someone stores for example sub.domain.com in the domain field, this query will still work and return true if the search contains sub.domain.com then?
I receive a pattern error when trying to set it in Javascript (nodeJS). Could you please explain to me how it works and where it checks against? I have no idea how to use this with user string input and checking against the string in the domain field. Sorry
var query = { $and: [ {'domain' : extractHostname(req.get('Referrer')) }, { 'APIKeys.Public' : req.query.APIKey } ] }; <-- This hostname should be checked if it is matching any of the options in the domain field stored in the document, including wildcard option.
I should probably first make it search for the APIKeys.Public before I let it do regex on all domain fields in all the documents.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.