How do you escape round brackets in passwords for bitbucket-api in node.js - node.js

I am trying to access a particular repository on BitBucket using bitbucket-api in node.js and my password contains funny characters (round brackets and spaces). It doesn't throw any useful errors or let me get the data.
I happen to like my password so I don't want to change it. I know that removing the round brackets and spaces from the password fixes the issue.
What can I do?

After plenty of searching and stepping into the implementation for curl-transport.js, I have found a way to work around this issue. It relies on understanding of what curl-transport passes to require('child_process').exec.
Essentially the mechanism works by starting a new process for CURL and passing in command line arguments. By having round brackets, it confuses CURL and by having spaces it confuses the command line argument parsing. So to work around it, simply add a double quote as the first character of the username and an ending double quote as the last character of the password; so when the two strings get concatenated (ie: username+":"+password) then the final string will be "USERNAME:PASSWORD" which get's passed as one argument to the process.
My node code looks like this:
var bitbucket = require('bitbucket-api');
var credentials = { username: '"USERNAME', password: 'PASSWORD With Spaces and ()s"' };
var client = bitbucket.createClient(credentials);
var repository = client.getRepository({ slug: 'repoName', owner: 'USERNAME' }, function (err, repo) {
//Code to access the repo object.
console.log(repo);
});
Notice how username has an additional double quote at the beginning of the value (this is the hack). I have added spaces and emphasised it below because it's not obvious::
username: ' " USERNAME'
Notice how password has an additional double quote at the end of the value (this is the hack). I have added spaces and emphasised it below because it's not obvious:
password: 'PASSWORD With Spaces and ()s " '
The call will then succeed and you will get back the details for your repository.
BTW, this fix worked for me in V0.0.6 of bitbucket-api on Windows 8.
A helpful note:
On windows, please remember to put the following into your path so that it can find CURL. You can do this through [Win8: Windows-X]->System->Advanced System Settings->Advanced->System Variables->Path->Edit...
Make sure git's binaries are in the path for CURL:
C:\Program Files (x86)\Git\bin
Also, if you are trying to get it to work on heroku then you might need to work around pathing issues by re-installing the Heroku toolbelt under C:\Heroku (see other posts for why) and adding the following to the path:
C:\Heroku\ruby-1.9.2\bin;C:\Heroku\bin;C:\Program Files (x86)\Git\bin

Related

In Freeplane Scripts, how to deal with special characters in node links to operating system commands

I'm trying to create a link to a shell script in a Node in Freeplane.
When I monitor the text in the node node.link.text I get execute_:script.sh%20/home/dir/file.ext
Ok, It works, but when my path has special characters link spaces or ( ), it isn't able to open. I already tried to use URLEncoder from java.net:
filePath='/home/user/Books/Author Name/File (231)/Book - Author.pdf'
urlEncoder = new URLEncoder()
def urlEncode(String s) {
urlEncoder.encode(s).replace("+", "%20");
}
fileLink = 'execute:_docreader.sh%20-p%20' + page + '%20' + urlEncode(filePath)
createdNode.link.setText(fileLink)
But I couldn't execute any command with files as arguments whose path have special characters. I even tried to put ' or " in the path manually but it didn't work too.
If filePath are without special characters, like /home/user/Books/AuthorName/file.pdf it works fine.
I look into Freeplane wiki and Freeplane API, looked into the examples but havent found any clue about that.

What is the correct method to determine if a system user exists locally on windows?

I am working on an authentication system for a local server jupyterhub that relies on OAuth protocol. Additionally, it creates a local system user on windows, in case it does not exist.
What is the correct way to check whether a user exists on windows platforms using python?
This would include cases in which the system uses LDAP authentication and the user logged in the machine at least once.
I am looking for the correct windows alternative to the unix-like:
import pwd
try:
pwd.getpwnam(user.name)
except Exception as e:
print(repr(e))
My current solution is to check for the existence of the f"os.environ["SystemDrive"]\Users\{username}" folder. Side question, is there any drawback with the current method?
Here's a solution to checking if a local Windows user exists using python:
import subprocess
def local_user_exists_windows(username):
r = subprocess.run("net user",stdout=subprocess.PIPE)
#look for username in the output. Return carriage followed by line break followed by name, then space
return f"\\r\\n{username.lower()} " in str(r.stdout).lower()
Alternative is to use a regular expression to find username match (^ is regex for beginning of line if used in conjunction with multiline, \b for word boundary):
import re
re.findall(rf"^{username}\b", out,flags=re.MULTILINE | re.IGNORECASE)
Note that the \b could be replaced with \s+ meaning a space character one or more times and yield similar results. The function above will return True if given user name is an exact match with local username on Windows.
Again, my reason for this solution is there might be drawback to checking whether the path f"os.environ["SystemDrive"]\Users\{username}" exists. For example, I have a case where a Local User (e.g,local_username) exists via the net user command or via looking at "Local Users and Groups" control panel, but there is no C:\Users\local_user_name folder. One reason for this I can think of off the top of my head is perhaps the user switched from logging in as a Local User to using a Domain Account, and their User folder was deleted to save space, so the User exists, but the folder does not, etc.)
The call to net user gets local users - and the output looks something like this:
User accounts for \\SOME-WINDOWS-COMPUTER
-------------------------------------------------------------------------------
SomeUser Administrator DefaultAccount
Guest local_admin WDAGUtilityAccount
Notice how the SomeUser in this example is preceded by a \r\n followed by multiple spaces, hence looking for a username string inside this string could yield a false positive if the string you are searching is contained inside another string.
The solution above works for me, but has been tested all of ten minutes, and there might be some other simpler or more pythonic way of doing this.

Installation block a determined username

once again I appeal to your help. More to the experts in the Inno Setup code.
I've tried several ways. But without success.
I need to block the installation of my application to users (entering in session of the operating system) with the username: EX?????
Ie. if the username is:
Ennnnnn -> OK
EXnnnnn -> No permission
(n is a number)
Can you help me. Thank you.
I've tried several ways. But without success.
Show us some code. What have you tried so far? Anyway..
How to block installation, in case a certain username is used?
In order to get the username:
The manual lists the available constants http://www.jrsoftware.org/ishelp/index.php?topic=consts You will find {username} there, which is the name of the user who is running Setup or the Uninstall program.
You can also return the username by using the GetUserNameString() function.
It returns the name of the user currently logged onto the system.
http://www.jrsoftware.org/ishelp/topic_isxfunc_getusernamestring.htm
For the comparison:
You might work with the string functions to make sure, that a username does not start with "EX".
The function Pos() might help you http://www.jrsoftware.org/ishelp/topic_isxfunc_pos.htm .
And you could also use Copy(), to copy the first two chars and compare them
Prefix := Copy(GetUserNameString(), 0, 2);

IMAP fetch() returns command error: BAD [b' Command Argument Error. 12']

I'm having trouble finding examples/troubleshooting tips online, and am not quite sure that I'm interpreting the documentation correctly. Any assistance would be greatly appreciated.
I'm connecting to an e-mail server, and want to read the e-mail subjects, and bodies. I first make my connection like so:
import imaplib
c = imaplib.IMAP4_SSL(hostname, port)
c.login(username, password)
foldername = 'INBOX/SSR'
c.select(str.encode(foldername), readonly = True)
today = datetime.date.today().strftime('%d-%b-%Y')
searchcriteria = '(SENTON '{}')'.format(today)
typ, msg_ids = c.search(None, searchcriteria)
msg_ids = [s.decode('ascii') for s in msg_ids]
for idnumber in msg_ids:
print(c.fetch(idnumber, "(BODY.PEEK[HEADER])"))
The code and works and output looks as expected, up until the last line, at which point, I get
imaplib.error: FETCH command error: BAD [b' Command Argument Error. 12']
My line of thought, and subsequent testing examined the following possible issues:
bytes vs. string. I converted input back to bytes, but the error remained constant
improper syntax: I tried other commands, such as BODY, SUBJECT, and ENVELOPE but still got the same message.
I'm not sure how to interpret the error, and don't really know where to start. Referencing https://www.rfc-editor.org/rfc/rfc3501.html from pp. 102+, I noticed that the values are labeled differently, but don't understand what the issue is with my implementation. How should I interpret the error? What is wrong with my syntax?
P.S. Correct me if I'm wrong, but the c.search shouldn't change my directory, yes? As in, by selecting foldername, I "navigate" to the selected folder, but just searching only returns values and shouldn't change my location?
I encountered the same problem while I tried to list or select a new mailbox - BAD [b' Command Argument Error. 12'], in my case, it didn't work with “Sent Box”, but it worked well with “Outbox”, so the space symbol is the point.
So it worked with c.select('"{}"'.format("Sent Box")...
Hope this information could help you.
Your last line is not correct msg_ids = [s.decode('ascii') for s in msg_ids]
msg_ids is a list with bytes string, not with elements of a list - example: [b'123 124 125']
Change the last line into msg_ids = msg_ids[0].split(b' ') and it will work as expected.

Catch Error In informix

This program is using Informix 4GL. The purpose is to delete the report. Well actually it works well, but the problem is that only a specific user can delete the physical file of the report, which means the admin. Other user can delete the report in database but not the physical path. So I need to catch the error in syntax command rm -f. I tried to use WHENEVER ERROR but it couldn't catch it. Does anyone know how to do it? This is the code for deleting the report:
DELETE FROM sysrpt
WHERE srpt_seq_no = p_sysrpt.srpt_seq_no
LET sel_rpt_id = ""
LET sel_rpt_id = p_sysrpt.srpt_pgm_id CLIPPED, ".",
p_sysrpt.srpt_seq_no USING "<<<<<<"
LET sel_rpt_id = sel_rpt_id CLIPPED
LET prt_comand = "\\rm -f ", rpt_path CLIPPED, sel_rpt_id CLIPPED
LET prt_comand = prt_comand CLIPPED
RUN prt_comand
I don't think you'll be able to capture the full error message unless you redirect stdout/stderr to another file as part of the rm command. You can get the return code from the shell command back into the 4gl program by using the RETURNING clause of the RUN statement, e.g. you could add the following after "RUN prt_command":
RUN prt_comand RETURNING p_return_code
From memory, I believe you need to MOD 256 the return code.
This sounds like a classic X-Y problem, where the real issue is the access permissions of the report as originally generated.
Perhaps the report file should be chmod-ed after generation to allow others to delete it, or the app users should have a correctly set umask.
Note that rm -f returns success even when it fails to remove the file, or the file does not exist. If you want rm to return an error, you must not tell it not to return an error.
Otherwise, the advice to use:
RUN prt_command RETURNING p_retcode
is correct. If the value of p_retcode is not zero, the command failed in some way.
The line copied below is superfluous:
LET prt_comand = prt_comand CLIPPED
If prt_command is a CHAR variable, then the stored result is blank-padded again (all CHAR variables are stored blank-padded to full length). If prt_command is a VARCHR variable, the previous assignment with CLIPPED already removed trailing blanks.
It is not clear to me why you are using a backslash before the rm command. Are you avoiding aliases? If so, time to get rid of the alias for rm; it is lethal in the long term. Learn to use the raw command correctly. If you ever get to a machine without the alias, you won't be protected and you will misuse the command without whatever safety net the alias purports to provide.

Resources